Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
到此一游#include<cstdio> #include<queue> #include<cstring> using namespace std; #define ll long long const int MAX_V = 20002; struct edge { int to, cap, rev; edge (int t, int c, int r): to(t), cap(c), rev(r){} }; vector<edge> G[MAX_V]; int level[MAX_V], iter[MAX_V]; inline void add_edge(int from, int to, int cap) { G[from].push_back(edge(to, cap, G[to].size())); G[to].push_back(edge(from, 0, G[from].size() - 1)); } void bfs(int s) { queue<int> que; que.push(s); memset(level, -1, sizeof(level)); level[s] = 0; for (int v; !que.empty(); que.pop()) { v = que.front(); for (int i = 0; i < G[v].size(); ++i) { edge& e = G[v][i]; if (e.cap > 0 && level[e.to] < 0) { level[e.to] = level[v] + 1; que.push(e.to); } } } } int dfs(int v, int t, int f) { if (v == t) return f; for (int& i = iter[v]; i < G[v].size(); ++i) { edge& e = G[v][i]; if (e.cap > 0 && level[v] < level[e.to]) { int d = dfs(e.to, t, min(e.cap, f)); if (d > 0) { e.cap -= d, G[e.to][e.rev].cap += d; return d; } } } return 0; } int max_flow(int s, int t) { int res = 0; for (int f; ; ) { bfs(s); if (level[t] < 0) return res; memset(iter, 0, sizeof(iter)); while (f = dfs(s, t, 1 << 30)) res += f; } } int main() { int n, m, s = 0, t; scanf("%d%d", &n, &m); t = n + 1; for (int a, b, i = 1; i <= n; ++i) { scanf("%d%d", &a, &b); add_edge(i, t, a); add_edge(s, i, b); } for (int a, b, w; m--; ) { scanf("%d%d%d", &a, &b, &w); add_edge(a, b, w); add_edge(b, a, w); } printf("%d\n", max_flow(s, t)); return 0; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator