POJ 1459:Power Network(最大流)

來源:互聯網
上載者:User

標籤:push   isa   tor   容量   include   log   bre   using   最大   

http://poj.org/problem?id=1459

題意:有np個發電站,nc個消費者,m條邊,邊有容量限制,發電站有產能上限,消費者有需求上限問最大流量。

思路:S和發電站相連,邊權是產能上限,消費者和T相連,邊權是需求上限,邊的話就按題意加就好了。痛點更覺得在於輸入。。加個空格。。邊數組要*2,因為有反向邊。

  1 #include <cstdio>  2 #include <algorithm>  3 #include <iostream>  4 #include <cstring>  5 #include <string>  6 #include <cmath>  7 #include <queue>  8 #include <vector>  9 #include <map> 10 #include <set> 11 using namespace std; 12 #define INF 0x3f3f3f3f 13 #define N 110 14 typedef long long LL; 15 struct Edge { 16     int v, cap, nxt; 17     Edge () {} 18     Edge (int v, int cap, int nxt) : v(v), cap(cap), nxt(nxt) {} 19 }edge[N*N*2]; 20 int tot, cur[N], dis[N], pre[N], head[N], gap[N], S, T; 21  22 void Add(int u, int v, int cap) { 23     edge[tot] = Edge(v, cap, head[u]); head[u] = tot++; 24     edge[tot] = Edge(u, 0, head[v]); head[v] = tot++; 25 } 26  27 void BFS() { 28     queue<int> que; 29     while(!que.empty()) que.pop(); 30     memset(dis, -1, sizeof(dis)); 31     memset(gap, 0, sizeof(gap)); 32     dis[T] = 0; gap[0]++; que.push(T); 33     while(!que.empty()) { 34         int u = que.front(); que.pop(); 35         for(int i = head[u]; ~i; i = edge[i].nxt) { 36             int v = edge[i].v; 37             if(dis[v] == -1) continue; 38             dis[v] = dis[u] + 1; 39             gap[dis[v]]++; 40             que.push(v); 41         } 42     } 43 } 44  45 int ISAP(int n) { 46     memcpy(cur, head, sizeof(cur)); 47     BFS(); 48     int u = pre[S] = S, ans = 0, i; 49     while(dis[S] < n) { 50         if(u == T) { 51             int flow = INF, index; 52             for(int i = S; i != T; i = edge[cur[i]].v) { 53                 Edge& e = edge[cur[i]]; 54                 if(e.cap < flow) { 55                     flow = e.cap; index = i; 56                 } 57             } 58             for(int i = S; i != T; i = edge[cur[i]].v) { 59                 edge[cur[i]].cap -= flow; 60                 edge[cur[i]^1].cap += flow; 61             } 62             ans += flow; u = index; 63         } 64         for(i = cur[u]; ~i; i = edge[i].nxt) 65             if(dis[edge[i].v] == dis[u] - 1 && edge[i].cap > 0) 66                 break; 67         if(~i) { 68             cur[u] = i; 69             pre[edge[i].v] = u; 70             u = edge[i].v; 71         } else { 72             int md = n; 73             if(--gap[dis[u]] == 0) break; 74             for(int i = head[u]; ~i; i = edge[i].nxt) { 75                 if(md > dis[edge[i].v] && edge[i].cap > 0) { 76                     md = dis[edge[i].v]; cur[u] = i; 77                 } 78             } 79             ++gap[dis[u] = md + 1]; 80             u = pre[u]; 81         } 82     } 83     return ans; 84 } 85  86 int main() { 87     int n, nc, np, m; 88     while(~scanf("%d%d%d%d", &n, &np, &nc, &m)) { 89         tot = 0; memset(head, -1, sizeof(head)); 90         S = n, T = n + 1; 91         int a, b, c; 92         for(int i = 1; i <= m; i++) { 93             scanf(" (%d,%d)%d", &a, &b, &c); 94             Add(a, b, c); 95         } 96         for(int i = 1; i <= np; i++) { 97             scanf(" (%d)%d", &a, &b); 98             Add(S, a, b); 99         }100         for(int i = 1; i <= nc; i++) {101             scanf(" (%d)%d", &a, &b);102             Add(a, T, b);103         }104         printf("%d\n", ISAP(T+1));105     }106     return 0;107 }

 

POJ 1459:Power Network(最大流)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.