HDU3549:Flow Problem(最大流入门EK)
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <queue>
#include <math.h>
#define inf 0x3f3f3f3f
using namespace std;
int map[][],v[],pre[];
int n,m;
int bfs(int s,int t)
{
memset(v,,sizeof(v));
memset(pre,-,sizeof(pre));
pre[s]=s;
queue<int>q;
q.push(s);
v[s]=;
while(!q.empty())
{
int tt=q.front();
q.pop();
for(int i=; i<=n; i++)
{
if(map[tt][i]&&v[i]==)
{
v[i]=;
pre[i]=tt;
q.push(i);
if(i==t)
{
return ;
}
}
}
}
return ;
}
int EK(int s,int t)
{
int ans=;
while(bfs(s,t)==)
{
int min1=inf;
for(int i=t; i!=s; i=pre[i])
{
if(min1>map[pre[i]][i])
{
min1=map[pre[i]][i];
}
}
for(int i=t; i!=s; i=pre[i])
{
map[pre[i]][i]-=min1;
map[i][pre[i]]+=min1;
}
ans+=min1;
}
return ans;
}
int main()
{
int T,x,y,z;
scanf("%d",&T);
for(int i=; i<=T; i++)
{
scanf("%d%d",&n,&m);
memset(map,,sizeof(map));
while(m--)
{
scanf("%d%d%d",&x,&y,&z);
map[x][y]+=z;
}
printf("Case %d: %d\n",i,EK(,n));
EK(,n);
}
return ;
}
#include <iostream>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <queue>
#include<algorithm>
#define inf 0x3f3f3f3f
using namespace std;
int n,m,tt;
struct node
{
int x,y,c;
int next;
}edge[10001];
int head[101],dis[101];
void init()
{
memset(head,-1,sizeof(head));
tt=0;
}
void add(int xx,int yy,int zz)
{
edge[tt].x=xx;
edge[tt].y=yy;
edge[tt].c=zz;
edge[tt].next=head[xx];
head[xx]=tt++;
edge[tt].x=yy;
edge[tt].y=xx;
edge[tt].c=0;
edge[tt].next=head[xx];
head[xx]=tt++;
}
int bfs(int s,int t)
{
queue<int>q;
memset(dis,-1,sizeof(dis));
q.push(s);
dis[s]=0;
while(!q.empty())
{
int w=q.front();
q.pop();
for(int i=head[w];i!=-1;i=edge[i].next)
{
if(dis[edge[i].y]==-1&&edge[i].c>0)
{
dis[edge[i].y]=dis[w]+1;
q.push(edge[i].y);
}
}
}
if(dis[t]>=0) return 1;
return 0;
}
int dinic(int x,int maxt)
{
int a;
if(x==n) return maxt;
for(int i=head[x];i!=-1;i=edge[head[x]].next)
{
if(dis[edge[i].y]==x+1&&edge[i].c>0&&(a==min(maxt,edge[i].c)))
{
,mhgr
}
}
}
int main()
{
int T,xx,yy,zz,ans;
scanf("%d",&T);
for(int i=1;i<=T;i++)
{
init();
ans=0;
scanf("%d%d",&n,&m);
while(m--)
{
scanf("%d%d%d",&xx,&yy,&zz);
add(xx,yy,zz);
}
while(bfs(1,n)==1)
{
ans+=dinic(1,inf);
}
printf("Case %d: %d\n",i,ans);
}
return 0;
}
HDU3549:Flow Problem(最大流入门EK)的更多相关文章
- [hdu3549]Flow Problem(最大流模板题)
解题关键:使用的挑战程序设计竞赛上的模板,第一道网络流题目,效率比较低,且用不习惯的vector来建图. 看到网上其他人说此题有重边,需要注意下,此问题只在邻接矩阵建图时会出问题,邻接表不会存在的,也 ...
- Hdu3549 Flow Problem 2017-02-11 16:24 58人阅读 评论(0) 收藏
Flow Problem Problem Description Network flow is a well-known difficult problem for ACMers. Given a ...
- Flow Problem(最大流模板)
Flow Problem Time Limit: 5000/5000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)Tota ...
- HDU3549 Flow Problem 【最大流量】
Flow Problem Time Limit: 5000/5000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others) Tot ...
- POJ1273:Drainage Ditches(最大流入门 EK,dinic算法)
http://poj.org/problem?id=1273 Description Every time it rains on Farmer John's fields, a pond forms ...
- hdu-3549 Flow Problem---最大流模板题(dinic算法模板)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3549 题目大意: 给有向图,求1-n的最大流 解题思路: 直接套模板,注意有重边 传送门:网络流入门 ...
- hdu 3549 Flow Problem (最大流)
裸最大流,做模板用 m条边,n个点,求最大流 #include <iostream> #include <cstdio> #include <cstring> #i ...
- HDU3549 Flow Problem(网络流增广路算法)
题目链接. 分析: 网络流增广路算法模板题.http://www.cnblogs.com/tanhehe/p/3234248.html AC代码: #include <iostream> ...
- hdu 3549 Flow Problem 最大流 Dinic
题目链接 题意 裸的最大流. 学习参考 http://www.cnblogs.com/SYCstudio/p/7260613.html Code #include <bits/stdc++.h& ...
随机推荐
- 介绍两个好玩的和Github相关的Chrome扩展
1. Octotree 默认的github网页里的代码显示没有我们在IDE里看到的直观,即代码文件所在的文件夹无法以树形层级结构显示在屏幕左边. 安装Octotree之后: 方便多了. 2. Isom ...
- SpringBoot+MyBatis+Mysql 6.X 版本日期型数据获,时间错乱,jason序列化时间相差8小时问题
新项目是用的springboot+mybatis+mysql 6.0.6版本的驱动包来搭建的,在使用的过程中遇到以下2个问题 从mysql取的的数据日期时间,与真实的时间往后错乱了14个小时. spr ...
- 记录一次ABP下载模板的坑
1.拉取ABP官网的模板的最新代码,我的代码结构是这样的 https://aspnetboilerplate.com/Templates 环境安装的部分我就不说明了.node.js npm 等等部分 ...
- vue 之this.$router.push、replace、go的区别
一.this.$router.push 说明:跳转到指定URL,向history栈添加一个新的记录,点击后退会返回至上一个页面 使用: this.$router.push('/index') this ...
- 如何解决Bat脚本中包含中文,运行乱码
bat文件右键用“ 编辑” 打开, 另存为时,UTF-8保存为ANSI 格式.即可解决运行是乱码问题, 有notepad++更好 修改直接保存,也不用另存为.
- Mac下使用Charles抓包https接口
1 官方网站下载,安装好Charles https://www.charlesproxy.com/download/ 2 安装ssl证书 3 信任证书 4 手机iPhone配置 ,获取证书url 5 ...
- Linux磁盘及文件系统管理2
创建文件系统: 格式化:低级格式化(分区之前进行,划分磁道).高级格式化(分区之后对分区进行,创建文件系统) 元数据区,数据区 元数据区: 文件元数据:inode(index node) 大小.权限. ...
- ubuntu系统---切换Py2.X与Py3.X版本
ubuntu系统---切换Python2.X与Python3.X版本 Python3.X将成为以后的趋势,Python2.X当前用的稍多的版本,但现在不再更新了.因此,小主电脑里也安装了好两个版本的p ...
- UUID值
生成这种值:b28043c8-fdb7-4c9e-8df5-b869d38f829d
- CCPC 2017 哈尔滨 L. Color a Tree && HDU 6241(二分+树形DP)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6241 题意:给你一棵有 n 个结点的树,每个结点初始颜色都为白色,有 A 个条件:结点 x_i 的黑色 ...