题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1532
Drainage Ditches
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 18864 Accepted Submission(s): 8980
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.
5 4 1 2 40 1 4 20 2 4 20 2 3 30 3 4 10
代码如下:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <string>
#include <set>
#define ms(a,b) memset((a),(b),sizeof((a)))
using namespace std;
typedef long long LL;
const int INF = 2e9;
const LL LNF = 9e18;
const int mod = 1e9+7;
const int MAXN = 500+10;
struct Edge
{
int to, next, cap, flow;
}edge[MAXN*MAXN];
int tot, head[MAXN];
int gap[MAXN], dep[MAXN], pre[MAXN], cur[MAXN];
void add(int u, int v, int w)
{
edge[tot].to = v; edge[tot].cap = w; edge[tot].flow = 0;
edge[tot].next = head[u]; head[u] = tot++;
edge[tot].to = u; edge[tot].cap = 0; edge[tot].flow = 0;
edge[tot].next = head[v]; head[v] = tot++;
}
int sap(int start, int end, int n)
{
memset(gap,0,sizeof(gap));
memset(dep,0,sizeof(dep));
memcpy(cur,head,sizeof(head));
int u = start;
pre[u] = -1;
gap[0] = n;
int maxflow = 0;
while(dep[start]<n)
{
bool flag = false;
for(int i = cur[u]; i!=-1; i=edge[i].next)
{
int v = edge[i].to;
if(edge[i].cap-edge[i].flow && dep[v]+1==dep[u])
{
flag = true;
cur[u] = pre[v] = i;
u = v;
break;
}
}
if(flag)
{
if(u==end)
{
int minn = INF;
for(int i = pre[u]; i!=-1; i=pre[edge[i^1].to])
if(minn>edge[i].cap-edge[i].flow)
minn = edge[i].cap-edge[i].flow;
for(int i = pre[u]; i!=-1; i=pre[edge[i^1].to])
{
edge[i].flow += minn;
edge[i^1].flow -= minn;
}
u = start;
maxflow += minn;
}
}
else
{
int minn = n;
for(int i = head[u]; i!=-1; i=edge[i].next)
if(edge[i].cap-edge[i].flow && dep[edge[i].to]<minn)
{
minn = dep[edge[i].to];
cur[u] = i;
}
gap[dep[u]]--;
if(gap[dep[u]]==0) break;
dep[u] = minn+1;
gap[dep[u]]++;
if(u!=start) u = edge[pre[u]^1].to;
}
}
return maxflow;
}
int main()
{
int n, m;
while(scanf("%d%d",&m,&n)!=EOF)
{
tot = 0;
memset(head,-1,sizeof(head));
for(int i = 1; i<=m; i++)
{
int u, v, c;
scanf("%d%d%d",&u,&v,&c);
add(u, v, c);
}
cout<< sap(1, n, n) <<endl;
}
}
6503



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



