网络流 小白推荐:https://blog.csdn.net/no_o_ac/article/details/81263016
Drainage DitchesTime Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 21724 Accepted Submission(s): 10345 Problem 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.
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 |
题意: 给定一张有向图, i 到 j 代表 水流 从i 流到 j的最大流速, 求从起点(为1) 能够流出的最大流速。(网络流 最大流 模版题)
AC代码:
#include<cstdio>
#include<queue>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn = 210;
const int inf = 0x3f3f3f3f;
int lef[maxn][maxn]; //i到j 的剩余流量
int flow[maxn]; //一条路径中的最小 剩余流量
int pre[maxn]; //pre[i] 记录访问i 之前访问的结点
int n,m;
int BFS(int s,int t){ //s为源点, t 为汇点
memset(pre,-1,sizeof(pre));
flow[s] = inf;
pre[s] = 0;
queue<int>Q;
Q.push(s);
while(!Q.empty()){
int now = Q.front();
Q.pop();
if(now == t) break;
for(int i = 1;i <= m;i ++){
if(i != s && pre[i] == -1 && lef[now][i] > 0){
flow[i] = min(flow[now],lef[now][i]);
pre[i] = now;
Q.push(i);
}
}
}
if(pre[t] == -1)
return -1;
return flow[t];
}
int maxflow(int s,int t){
int sum = 0;
int addflow;
while((addflow = BFS(s,t)) != -1){
int k = t;
while(k != s){
int last = pre[k];
lef[k][last] += addflow; //反向边增加
lef[last][k] -= addflow; //正向边减少
k = last;
}
sum += addflow;
}
return sum;
}
int main()
{
while(~scanf("%d%d",&n,&m)){
memset(lef,0,sizeof(lef));
memset(flow,0,sizeof(flow));
int s,e,c;
for(int i = 0;i < n;i ++){
scanf("%d%d%d",&s,&e,&c);
if(s == e) continue;
lef[s][e] += c;
}
printf("%d\n",maxflow(1,m));
}
return 0;
}