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.
InputThe 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.
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.
OutputFor 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 10Sample Output
50
// 网络流 Ford-Fulkerson算法
#include <cstdio>
#include <vector>
#include <string.h>
#include <queue>
#define MAX 500
#define INF 0x3fffffff
using namespace std;
typedef struct edge {
edge( int _to, int _cap, int _rev ) {
to = _to;
cap = _cap;
rev = _rev;
}
int to; // 终点
int cap; // 容量
int rev; // 反向边
} Edge;
vector<Edge> MGraph[MAX];
int visit[MAX];
int n, m;
void init() {
memset( visit, 0, sizeof( visit ) );
for( int i = 0; i < MAX; i++ ) MGraph[i].clear();
}
//向图中增加一条从from到to容量为cap的边和反向边
void add_edge( int from, int to, int cap ) {
MGraph[from].push_back( Edge( to, cap, MGraph[to].size() ) );
MGraph[to].push_back( Edge( from, 0, MGraph[from].size() - 1 ) );
}
// 通过DFS寻找增广路
int dfs( int v, int t, int f ) {
if( v == t ) return f;
visit[v] = 1;
for( int i = 0; i < MGraph[v].size(); i++ ) {
Edge &e = MGraph[v][i];
if( !visit[e.to] && e.cap > 0 ) {
int d = dfs( e.to, t, min( f, e.cap ) );
if( d > 0 ) {
e.cap -= d;
MGraph[e.to][e.rev].cap += d;
return d;
}
}
}
return 0;
}
// 求解从s到t的最大流
int max_flow( int s, int t ) {
int flow = 0;
while( 1 ) {
memset( visit, 0, sizeof( visit ) );
int f = dfs( s, t, INF );
if( f == 0 ) break;
flow += f;
}
return flow;
}
int main() {
while( scanf( "%d%d", &n, &m ) != EOF ) {
init();
for( int i = 0; i < n; i++ ) {
int a, b, v;
scanf( "%d%d%d", &a, &b, &v );
if( a == b ) continue;
add_edge( a, b, v );
}
printf( "%d\n", max_flow( 1, m ) );
}
return 0;
}
// 网络流 Dinic算法
#include <cstdio>
#include <vector>
#include <string.h>
#include <queue>
#define MAX 500
#define INF 0x3fffffff
using namespace std;
typedef struct edge {
edge( int _to, int _cap, int _rev ) {
to = _to;
cap = _cap;
rev = _rev;
}
int to; // 终点
int cap; // 容量
int rev; // 反向边
} Edge;
vector<Edge> MGraph[MAX];
int level[MAX];
int n, m;
void init() {
for( int i = 0; i < MAX; i++ ) MGraph[i].clear();
}
//向图中增加一条从from到to容量为cap的边和反向边
void add_edge( int from, int to, int cap ) {
MGraph[from].push_back( Edge( to, cap, MGraph[to].size() ) );
MGraph[to].push_back( Edge( from, 0, MGraph[from].size() - 1 ) );
}
// 通过BFS计算从源点出发的距离编号
void bfs( int s ) {
memset( level, -1, sizeof( level ) );
queue<int> q;
level[s] = 0;
q.push( s );
while( !q.empty() ) {
int v = q.front();
q.pop();
for( int i = 0; i < MGraph[v].size(); i++ ) {
Edge &e = MGraph[v][i];
if( e.cap > 0 && level[e.to] < 0 ) {
level[e.to] = level[v] + 1;
q.push( e.to );
}
}
}
}
// 通过DFS寻找增广路
int dfs( int v, int t, int f ) {
if( v == t ) return f;
for( int i = 0; i < MGraph[v].size(); i++ ) {
Edge &e = MGraph[v][i];
if( e.cap > 0 && level[v] < level[e.to] ) {
int d = dfs( e.to, t, min( f, e.cap ) );
if( d > 0 ) {
e.cap -= d;
MGraph[e.to][e.rev].cap += d;
return d;
}
}
}
return 0;
}
// 求解从s到t的最大流
int max_flow( int s, int t ) {
int flow = 0;
for( ; ; ) {
bfs( s );
if( level[t] < 0 ) break;
int f;
while( ( f = dfs( s, t, INF ) ) > 0 ) {
flow += f;
}
}
return flow;
}
int main() {
while( scanf( "%d%d", &n, &m ) != EOF ) {
init();
for( int i = 0; i < n; i++ ) {
int a, b, v;
scanf( "%d%d%d", &a, &b, &v );
if( a == b ) continue;
add_edge( a, b, v );
}
printf( "%d\n", max_flow( 1, m ) );
}
return 0;
}