POJ2914 Minimum Cut 【全域最小割】(Stoer_Wagner)

來源:互聯網
上載者:User

標籤:poj2914

Minimum Cut
Time Limit: 10000MS   Memory Limit: 65536K
Total Submissions: 7610   Accepted: 3203
Case Time Limit: 5000MS

Description

Given an undirected graph, in which two vertices can be connected by multiple edges, what is the size of the minimum cut of the graph? i.e. how many edges must be removed at least to disconnect the graph into two subgraphs?

Input

Input contains multiple test cases. Each test case starts with two integers N and M (2 ≤ N ≤ 500, 0 ≤ M ≤ N × (N ? 1) ? 2) in one line, where N is the number of vertices. Following are M lines, each line contains M integersA, B and C (0 ≤ A, B < N, A ≠ B, C > 0), meaning that there C edges connecting vertices A and B.

Output

There is only one line for each test case, which contains the size of the minimum cut of the graph. If the graph is disconnected, print 0.

Sample Input

3 30 1 11 2 12 0 14 30 1 11 2 12 3 18 140 1 10 2 10 3 11 2 11 3 12 3 14 5 14 6 14 7 15 6 15 7 16 7 14 0 17 3 1

Sample Output

212

Source

Baidu Star 2006 Semifinal 
Wang, Ying (Originator) 
Chen, Shixi (Test cases)題意:求裸的最小割。題解:過程有點像求“最大產生樹”,每次找到將最後的點(即割點值最小的點)隔離出去的最小割大小nowcut,然後用nowcut更新結果mincut,然後將最後兩個點合并,方式是將最後一個點合并到倒數第二個點,邊也都合并過去,就這樣迴圈下去直到只剩下一個頂點,此時mincut即為所求。

#include <stdio.h>#include <string.h>#define inf 0x7fffffff#define maxn 502int map[maxn][maxn], W[maxn], hash[maxn];bool vis[maxn];void getMap(int n, int m){    int i, u, v, c;    memset(map, 0, sizeof(map));    for(i = 0; i < m; ++i){        scanf("%d%d%d", &u, &v, &c);        map[u][v] += c; map[v][u] += c;    }}int Stoer_Wagner(int n){    int minCut = inf, nowCut, now, pre, i, j;    for(i = 0; i < n; ++i) hash[i] = i;    while(n > 1){        nowCut = -1; now = 1; vis[hash[0]] = 1; pre = 0;        for(i = 1; i < n; ++i){            W[hash[i]] = map[hash[0]][hash[i]];            vis[hash[i]] = 0;            if(W[hash[i]] > nowCut){                nowCut = W[hash[i]]; now = i;            }        }        for(j = 1; j < n; ++j){            vis[hash[now]] = 1;            if(j == n - 1){                if(nowCut < minCut) minCut = nowCut;                for(i = 0; i < n; ++i){                    map[hash[pre]][hash[i]] += map[hash[now]][hash[i]];                    map[hash[i]][hash[pre]] += map[hash[now]][hash[i]];                }                hash[now] = hash[--n];                break;            }            pre = now; nowCut = -1;            for(i = 1; i < n; ++i){                if(!vis[hash[i]]){                    W[hash[i]] += map[hash[pre]][hash[i]];                    if(W[hash[i]] > nowCut){                        nowCut = W[hash[i]]; now = i;                    }                }            }        }    }    return minCut;}void solve(int n){    printf("%d\n", Stoer_Wagner(n));}int main(){    freopen("stdin.txt", "r", stdin);    int n, m;    while(scanf("%d%d", &n, &m) == 2){        getMap(n, m);        solve(n);    }    return 0;}


POJ2914 Minimum Cut 【全域最小割】(Stoer_Wagner)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.