564

# 题目

## 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.

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

50

# 代码

/*
By:OhYee
Github:OhYee
Blog:http://www.oyohyee.com/
Email:oyohyee@oyohyee.com

かしこいかわいい？
エリーチカ！

*/
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <set>
#include <list>
#include <queue>
#include <stack>
#include <string>
#include <vector>
#include <bitset>
#include <functional>

using namespace std;

const int INF = 0x7FFFFFFF / 2;
const double eps = 1e-10;

const int maxn = 205;
const int maxm = 205*2;

struct Edge {
int u,v,w;
Edge():u(0),v(0),w(0) {}
Edge(int a,int b,int c):u(a),v(b),w(c) {}
};

int pos;
Edge edge[maxm];
list<int> L[maxn];
queue<int> Q;
int dist[maxn];

inline void add(int u,int v,int w) {
edge[pos] = Edge(u,v,w);
L[u].push_back(pos);
pos++;
}

bool bfs(int s,int t) {
memset(dist,0,sizeof(dist));
while(!Q.empty())
Q.pop();
Q.push(s);
dist[s] = 1;
while(!Q.empty()) {
int u = Q.front();
Q.pop();
for(list<int>::iterator it = L[u].begin();it != L[u].end();it++) {
int v = edge[*it].v;
if(!dist[v] && edge[*it].w) {
dist[v] = dist[u] + 1;
if(v == t)
return true;
Q.push(v);
}
}
}
return false;
}
int dfs(int u,int t,int flow) {
if(u == t)
return flow;
int remain = flow;
for(list<int>::iterator it = L[u].begin();it != L[u].end() && remain;it++) {
int v = edge[*it].v;
if(dist[v] == dist[u] + 1 && edge[*it].w) {
int k = dfs(v,t,min(remain,edge[*it].w));
if(!k)
dist[v] = 0;
edge[*it].w -= k;
edge[(*it) ^ 1].w += k;
remain -= k;
}
}
return flow - remain;
}
int Dinic(int u,int v) {
int ans = 0;
while(bfs(u,v))
ans += dfs(u,v,INF);
return ans;
}

bool Do() {
int n,m;
if(!(cin >> n >> m))
return false;

for(int i = 1;i <= m;i++)
L[i].clear();
pos = 0;

for(int i = 0;i < n;i++) {
int u,v,w;
cin >> u >> v >> w;
}
cout << Dinic(1,m) << endl;
return true;
}

int main() {
cin.tie(0);
cin.sync_with_stdio(false);

while(Do());

return 0;
}

• 点击查看/关闭被识别为广告的评论