POJ1273(最大流)

时间:2023-01-24 14:20:32

Drainage Ditches

Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 70451   Accepted: 27391

Description

Every time it rains on Farmer John's fields, a pond forms over Bessie's favorite clover patch. This means that the clover is covered by water for awhile and takes quite a long time to regrow. Thus, Farmer John has built a set of drainage ditches so that Bessie's clover patch is never covered in water. Instead, the water is drained to a nearby stream. Being an ace engineer, Farmer John has also installed regulators at the beginning of each ditch, so he can control at what rate water flows into that ditch. 
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

The input includes several cases. For each case, the first line contains two space-separated integers, N (0 <= N <= 200) and M (2 <= M <= 200). N is the number of ditches that Farmer John has dug. M is the number of intersections points for those ditches. Intersection 1 is the pond. Intersection point M is the stream. Each of the following N lines contains three integers, Si, Ei, and Ci. Si and Ei (1 <= Si, Ei <= M) designate the intersections between which this ditch flows. Water will flow through this ditch from Si to Ei. Ci (0 <= Ci <= 10,000,000) is the maximum rate at which water will flow through the ditch.

Output

For each case, output a single integer, the maximum rate at which water may emptied from the pond.

Sample Input

5 4
1 2 40
1 4 20
2 4 20
2 3 30
3 4 10

Sample Output

50

最大流入门题,裸的Edmonds-Karp。
 //2016.9.22
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#define N 205 using namespace std; const int inf = 0x3f3f3f3f; struct Edge
{
int from, to , cap, flow;//分别为边的两个端点、容量、边上的流
Edge(int u, int v, int c, int f):from(u), to(v), cap(c), flow(f){}
}; struct EdmondsKarp//Edmonds-Karp算法
{
int n, m;//结点编号0——n-1
vector<Edge> edges;//存边与反向弧
vector<int> G[N];//邻接表,G[i][j]表示结点i的第j条边在数组edges中的序号
int a[N];//当起点到i的可改进量
int p[N];//最短路树上p的入弧编号 void init(int n)//初始化
{
for(int i = ; i < n; i++)G[i].clear();
edges.clear();
} void addEdge(int from, int to, int cap)//添加边
{
edges.push_back(Edge(from, to, cap, ));
edges.push_back(Edge(to, from, , ));//反向弧
m = edges.size();
G[from].push_back(m-);//加入编号
G[to].push_back(m-);
} int maxFlow(int s, int t)//最大流,s为源点,t为汇点
{
int flow = ;
while()
{
memset(a, , sizeof(a));
queue<int> Q;
Q.push(s);
a[s] = inf;
while(!Q.empty())
{
int x = Q.front(); Q.pop();
for(int i = ; i < G[x].size(); i++)
{
Edge& e = edges[G[x][i]];
if(!a[e.to] && e.cap>e.flow)
{
p[e.to] = G[x][i];
a[e.to] = min(a[x], e.cap-e.flow);
Q.push(e.to);
}
}
if(a[t])break;
}
if(!a[t])break;//残余网络中不存在增广路,当前流是最大流
for(int u = t; u != s; u = edges[p[u]].from)//找到一条增广路
{
edges[p[u]].flow += a[t];
edges[p[u]^].flow -= a[t];//p[u]^1为p[u]的反向边
}
flow += a[t];
}
return flow;
}
}; int main()
{
int n, m;
EdmondsKarp e;
while(scanf("%d%d", &m, &n)!=EOF)
{
int u, v, c;
e.init(n);
for(int i = ; i < m; i++)
{
scanf("%d%d%d", &u, &v, &c);
u--, v--;
e.addEdge(u, v, c);
}
printf("%d\n", e.maxFlow(, n-));
} return ;
}