UVa1660 Cable TV Network (無向圖,點連通度,最大流)

來源:互聯網
上載者:User

標籤:

連結:http://bak3.vjudge.net/problem/UVA-1660

分析:這篇部落格講的很詳細。http://www.cnblogs.com/xcw0754/p/4662429.html

 1 #include <cstdio> 2 #include <queue> 3 #include <cstring> 4 #include <vector> 5 using namespace std; 6  7 const int maxn = 100 + 5; 8 const int INF = 0x3f3f3f3f; 9 10 struct Edge {11     int from, to, cap, flow;12     Edge(int u, int v, int c, int f):from(u), to(v), cap(c), flow(f) {}13 };14 15 struct EdmondsKarp {16     int n, m;17     vector<Edge> edges;18     vector<int> G[maxn];19     int a[maxn];20     int p[maxn];21 22     void init(int n) {23         for (int i = 0; i < n; i++) G[i].clear();24         edges.clear();25     }26 27     void AddEdge(int from, int to, int cap) {28         edges.push_back(Edge(from, to, cap, 0));29         edges.push_back(Edge(to, from, 0, 0));30         m = edges.size();31         G[from].push_back(m - 2);32         G[to].push_back(m - 1);33     }34 35     int Maxflow(int s, int t) {36         int flow = 0;37         for (;;) {38             memset(a, 0, sizeof(a));39             queue<int> Q;40             Q.push(s);41             a[s] = INF;42             while (!Q.empty()) {43                 int x = Q.front(); Q.pop();44                 for (int i = 0; i < G[x].size(); i++) {45                     Edge& e = edges[G[x][i]];46                     if (!a[e.to] && e.cap > e.flow) {47                         p[e.to] = G[x][i];48                         a[e.to] = min(a[x], e.cap - e.flow);49                         Q.push(e.to);50                     }51                 }52                 if (a[t]) break;53             }54             if (!a[t]) break;55             for (int u = t; u != s; u = edges[p[u]].from) {56                 edges[p[u]].flow += a[t];57                 edges[p[u] ^ 1].flow -= a[t];58             }59             flow += a[t];60         }61         return flow;62     }63 };64 65 EdmondsKarp g;66 vector<Edge> bak;67 68 int main() {69     int n, m;70     while (scanf("%d%d", &n, &m) == 2) {71         g.init(n * 2);72         for (int i = 0; i < n; i++) g.AddEdge(i, i + n, 1);73         for (int i = 0, u, v; i < m; i++) {74             scanf(" (%d,%d)", &u, &v);75             g.AddEdge(u + n, v, INF);76             g.AddEdge(v + n, u, INF);77         }78         int s = n, flow = n; bak = g.edges;79         for (int i = 1; i < n; i++) {80                 g.edges = bak;81                 flow = min(flow, g.Maxflow(s, i));82         }83         printf("%d\n", flow);84     }85     return 0;86 }

 

UVa1660 Cable TV 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.