Network flow is a well-known difficult problem for ACMers. Given a graph, your task is to find out the maximum flow for the weighted directed graph.
InputThe first line of input contains an integer T, denoting the number of test cases. For each test case, the first line contains two integers N and M, denoting the number of vertexes and edges in the graph. (2 <= N <= 15, 0 <= M <= 1000)
Next M lines, each line contains three integers X, Y and C, there is an edge from X to Y and the capacity of it is C. (1 <= X, Y <= N, 1 <= C <= 1000)OutputFor each test cases, you should output the maximum flow from source 1 to sink N.Sample Input
2 3 2 1 2 1 2 3 1 3 3 1 2 1 2 3 1 1 3 1Sample Output
Case 1: 1 Case 2: 2
套个模板A,暂时熟悉一下模板。
#include <iostream>
#include <cstring>
#include <queue>
#include <set>
#include <vector>
#include <cmath>
#include <stack>
#include <string>
#include <queue>
#include <algorithm>
#include <cstdio>
#include <map>
using namespace std;
#define INF 0x3f3f3f3f
struct edge {
int cap;
int to;
int rev;
};
vector<edge> G[15];
bool flag[15];
void addedge(int x,int y,int cap) {
G[x].push_back(edge{ cap,y,(int)G[y].size() });
G[y].push_back(edge{0,x,(int)G[x].size() - 1});
}
int dfs(int k,int t,int f) {
if (k == t) {
return f;
}
int s = G[k].size();
flag[k] = true;
for (int i = 0; i < s; ++i) {
if (!flag[G[k][i].to] && G[k][i].cap > 0) {
int d = dfs(G[k][i].to, t, min(f, G[k][i].cap));
if (d > 0) {
G[k][i].cap -= d;
G[G[k][i].to][G[k][i].rev].cap += d;
return d;
}
}
}
return 0;
}
int ful(int s, int t){
int flow = 0;
while (1) {
memset(flag, false, sizeof(flag));
int f = dfs(s,t,INF);
if (f == 0) {
return flow;
}
flow += f;
}
return flow;
}
int main(){
int n, m, T, t = 0;
scanf("%d", &T);
while (T--){
t++;
scanf("%d%d", &n, &m);
int x, y, cap;
for (int i = 1; i <= m; ++i) {
scanf("%d%d%d", &x, &y,&cap);
addedge(x, y, cap);
}
printf("Case %d: %d\n", t,ful(1,n));
for (int i = 1; i <= n; ++i) {
G[i].clear();
}
}
return 0;
}