标签:bsp hidden code pen float other overflow mem solution
机房里面有
最多传送多少数据?
第1行: 两个用空格分开的整数
第二行到第
输出一个整数。即排水的最大流量。
Sample Input | Sample Output |
---|---|
5 4 1 2 40 1 4 20 2 4 20 2 3 30 3 4 10 |
50 |
#include <iostream> #include <cstdio> #include <cstring> #include <queue> using namespace std; const int inf = 1000000000; const int maxn = 200, maxm = 200; //the max number of vertices and edges struct Edge { int v, f, nxt; }; int n, src, sink; int g[maxn+8]; int nume; Edge e[maxm*2+8]; void addedge(int u, int v, int c) { e[++nume].v = v; e[nume].f = c; e[nume].nxt = g[u]; g[u] = nume; e[++nume].v = u; e[nume].f = 0; e[nume].nxt = g[v]; g[v] = nume; } void init() { memset(g, 0, sizeof(g)); nume = 1; } queue<int> que; bool vis[maxn+8]; int dist[maxn+8]; //distance void bfs() { memset(dist, 0, sizeof(dist)); while(!que.empty()) que.pop(); vis[src] = true; que.push(src); while(!que.empty()){ int u = que.front(); que.pop(); for(int i = g[u]; i; i = e[i].nxt) { if(e[i].f && !vis[e[i].v]) { que.push(e[i].v); dist[e[i].v] = dist[u] + 1; vis[e[i].v] = true; } } } } int dfs(int u, int delta) { if(u == sink){ return delta; } else{ int ret = 0; for(int i = g[u]; delta && i; i = e[i].nxt) { if(e[i].f && dist[e[i].v] == dist[u] + 1) { int dd = dfs(e[i].v, min(e[i].f, delta)); e[i].f -= dd; e[i^1].f += dd; delta -= dd; ret += dd; } } return ret; } } int maxflow() { int ret = 0; while(true) { memset(vis, 0, sizeof(vis)); bfs(); if(!vis[sink]) return ret; ret += dfs(src, inf); } } int main() { int N, M, S, E, C; scanf("%d%d", &N, &M); src = 1;sink = M; while(N--){ scanf("%d%d%d", &S, &E, &C); addedge(S, E, C); } printf("%d", maxflow()); return 0; }
标签:bsp hidden code pen float other overflow mem solution
原文地址:http://www.cnblogs.com/cxchanpin/p/7066295.html