标签:oca port var center const esc uri using panel
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3577 Accepted Submission(s): 1503
顶点上有容量限制则进行拆点。把u点拆成u和u+n,若<u,v>有边,则u+n和v加边,v+n和u加边。
1 //2017-08-24 2 #include <cstdio> 3 #include <cstring> 4 #include <iostream> 5 #include <algorithm> 6 #include <queue> 7 #pragma comment(linker, "/STACK:1024000000,1024000000") 8 9 using namespace std; 10 11 const int N = 1010; 12 const int M = 210010; 13 const int INF = 0x3f3f3f3f; 14 int head[N], tot; 15 struct Edge{ 16 int next, to, w; 17 }edge[M]; 18 19 void add_edge(int u, int v, int w){ 20 edge[tot].w = w; 21 edge[tot].to = v; 22 edge[tot].next = head[u]; 23 head[u] = tot++; 24 } 25 26 struct Dinic{ 27 int level[N], S, T; 28 void init(int _S, int _T){ 29 S = _S; 30 T = _T; 31 tot = 0; 32 memset(head, -1, sizeof(head)); 33 } 34 bool bfs(){ 35 queue<int> que; 36 memset(level, -1, sizeof(level)); 37 level[S] = 0; 38 que.push(S); 39 while(!que.empty()){ 40 int u = que.front(); 41 que.pop(); 42 for(int i = head[u]; i != -1; i = edge[i].next){ 43 int v = edge[i].to; 44 int w = edge[i].w; 45 if(level[v] == -1 && w > 0){ 46 level[v] = level[u]+1; 47 que.push(v); 48 } 49 } 50 } 51 return level[T] != -1; 52 } 53 int dfs(int u, int flow){ 54 if(u == T)return flow; 55 int ans = 0, fw; 56 for(int i = head[u]; i != -1; i = edge[i].next){ 57 int v = edge[i].to, w = edge[i].w; 58 if(!w || level[v] != level[u]+1) 59 continue; 60 fw = dfs(v, min(flow-ans, w)); 61 ans += fw; 62 edge[i].w -= fw; 63 edge[i^1].w += fw; 64 if(ans == flow)return ans; 65 } 66 if(ans == 0)level[u] = -1; 67 return ans; 68 } 69 int maxflow(){ 70 int flow = 0, f; 71 while(bfs()) 72 while((f = dfs(S, INF)) > 0) 73 flow += f; 74 return flow; 75 } 76 }dinic; 77 78 char str[N]; 79 80 int main() 81 { 82 std::ios::sync_with_stdio(false); 83 //freopen("inputI.txt", "r", stdin); 84 int n, m; 85 while(cin>>n>>m){ 86 int s, t; 87 cin>>s>>t; 88 dinic.init(s, t+n); 89 int u, v, w; 90 for(int i = 1; i <= n; i++){ 91 cin>>w; 92 add_edge(i, i+n, w); 93 add_edge(i+n, i, 0); 94 } 95 while(m--){ 96 cin>>u>>v; 97 add_edge(u+n, v, INF); 98 add_edge(v, u+n, 0); 99 add_edge(v+n, u, INF); 100 add_edge(u, v+n, 0); 101 } 102 printf("%d\n", dinic.maxflow()); 103 } 104 return 0; 105 }
标签:oca port var center const esc uri using panel
原文地址:http://www.cnblogs.com/Penn000/p/7423487.html