| Time Limit: 1000MS | Memory Limit: 10000K | |
| Total Submissions: 67615 | Accepted: 26138 |
Description
Farmer John knows not only how many gallons of water each ditch can transport per minute but also the exact layout of the ditches, which feed out of the pond and into each other and stream in a potentially complex network.
Given all this information, determine the maximum rate at which water can be transported out of the pond and into the stream. For any given ditch, water flows in only one direction, but there might be a way that water can flow in a circle.
Input
Output
Sample Input
5 4 1 2 40 1 4 20 2 4 20 2 3 30 3 4 10
Sample Output
50
先来说说dinic,dinic算法可以分为三步,第一步用bfs构建出层次图,第二步用dfs求出一条路的最大流,第三步加和重复以上两步。
其次是关于反向边的理解,当一条边已经被完全占用的时候,可以利用反向边使得另外一条边的流可以从这条边的终点流向起点,其实就是一种替换的意思,用反向边的流替换本不应该从这里流过的流。这里感觉有点说不清楚了,其实本身也不是很明白,虽然看不懂不影响套模板……详细见这个博客https://comzyh.com/blog/archives/568/,对于反相弧说的很好。
贴代码
#include<stdio.h>
#include<string.h>
#include<queue>
#include<algorithm>
using namespace std;
struct node
{
int c,f;
};
int INF=10000001;
int d[220],vis[220];
int st,et,i,j,k,m,n;
node map[220][220];
int bfs()
{
memset(d,0,sizeof(d));
queue<int> q;
q.push(st);
d[st]=1;
while(!q.empty())
{ int pos=q.front();
q.pop();
for(int i=1;i<=n;i++)
{
if((map[pos][i].c-map[pos][i].f) && d[i]==0)
{
d[i]=d[pos]+1;
q.push(i);
}
}
}
if(d[et]==0)return 0;
else return 1;
}
int dinic(int pos,int flow)
{ int ff=flow;
if(pos==et)return flow;
int sum=0;
for(int i=1;i<=n;i++)
{
if(d[pos]+1==d[i] && (map[pos][i].c-map[pos][i].f))
{
int a=map[pos][i].c-map[pos][i].f;
int t=dinic(i,min(a,flow));
map[pos][i].f+=t;
map[i][pos].f-=t; //注意反向弧
//printf("liu:%d->%d:%d\n",pos,i,t);
flow-=t;
sum+=t;
}
}
return ff-flow;
}
int main()
{
while(~scanf("%d%d",&m,&n))
{ memset(map,0,sizeof(map));
et=n;
st=1;
for(int i=1;i<=m;i++)
{ int x,y,s;
scanf("%d%d%d",&x,&y,&s);
map[x][y].c+=s; //注意两点间的重边,用加法
}
int Sum=0;
while(bfs())
{
Sum+=dinic(st,INF);
}
printf("%d\n",Sum);
}
return 0;
} 依旧不懂邻接表,依旧不懂前向星,真的要挂了……
1908



被折叠的 条评论
为什么被折叠?



