Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 61113 | Accepted: 23467 |
Description
Input
Output
Sample Input
5 4 1 2 40 1 4 20 2 4 20 2 3 30 3 4 10
Sample Output
50
题意:把池塘(编号1)里面的水通过若干个渠沟排到小溪(编号n)里面,每个渠沟都有最大容量,求能从池塘排出来的最大水量。 最大流模版
dinic:
#include <cstdio> #include <cstring> #include <vector> #include <queue> #include <algorithm> #define MAXN 200+10 #define MAXM 500+10 #define INF 2000000000+10 using namespace std; struct Edge { int from, to, cap, flow, next; }edge[MAXM]; int dist[MAXN], cur[MAXN], vis[MAXN], head[MAXN], top; int n, m; void init() { top = 0; memset(head, -1, sizeof(head)); } void addedge(int a, int b, int c) { Edge E1 = {a, b, c, 0, head[a]}; edge[top] = E1; head[a] = top++; Edge E2 = {b, a, 0, 0, head[b]}; edge[top] = E2; head[b] = top++; } void getmap() { int a, b, c; while(m--) { scanf("%d%d%d", &a, &b, &c); addedge(a, b, c); } } bool BFS(int start, int end) { memset(dist, -1, sizeof(dist)); memset(vis, 0, sizeof(vis)); queue<int> Q; while(!Q.empty()) Q.pop(); Q.push(start); vis[start] = 1; dist[start] = 0; while(!Q.empty()) { int u = Q.front(); Q.pop(); for(int i = head[u]; i != -1; i = edge[i].next) { Edge E = edge[i]; if(!vis[E.to] && E.cap > E.flow) { dist[E.to] = dist[u] + 1; vis[E.to] = 1; if(E.to == end) return true; Q.push(E.to); } } } return false; } int DFS(int x, int a, int end) { if(x == end || a == 0) return a; int flow = 0, f; for(int& i = cur[x]; i != -1; i = edge[i].next) { Edge& E = edge[i]; if(dist[E.to] == dist[x]+1 && (f = DFS(E.to, min(a, E.cap-E.flow), end)) > 0) { E.flow += f; edge[i^1].flow -= f; flow += f; a -= f; if(a == 0) break; } } return flow; } int Maxflow(int start, int end) { int flow = 0; while(BFS(start, end)) { memcpy(cur, head, sizeof(head)); flow += DFS(start, INF, end); } return flow; } int main() { while(scanf("%d%d", &m, &n) != EOF) { init(); getmap(); printf("%d\n", Maxflow(1, n)); } return 0; }