zoukankan      html  css  js  c++  java
  • POJ 2914 Minimum Cut

    Minimum Cut

    Time Limit: 10000ms
    Memory Limit: 65536KB
    This problem will be judged on PKU. Original ID: 2914
    64-bit integer IO format: %lld      Java class name: Main
     

    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 areM lines, each line contains M integers AB and C (0 ≤ AB < NA ≠ BC > 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 3
    0 1 1
    1 2 1
    2 0 1
    4 3
    0 1 1
    1 2 1
    2 3 1
    8 14
    0 1 1
    0 2 1
    0 3 1
    1 2 1
    1 3 1
    2 3 1
    4 5 1
    4 6 1
    4 7 1
    5 6 1
    5 7 1
    6 7 1
    4 0 1
    7 3 1

    Sample Output

    2
    1
    2

    Source

     
    解题:无向图的最小割。Stoer-Wagner 算法
     
     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <cmath>
     5 #include <algorithm>
     6 #include <climits>
     7 #include <vector>
     8 #include <queue>
     9 #include <cstdlib>
    10 #include <string>
    11 #include <set>
    12 #include <stack>
    13 #define LL long long
    14 #define pii pair<int,int>
    15 #define INF 0x3f3f3f3f
    16 using namespace std;
    17 const int maxn = 510;
    18 int e[maxn][maxn],n,m;
    19 bool comb[maxn];
    20 int Find(int &s,int &t){
    21     bool vis[maxn];
    22     int w[maxn];
    23     memset(vis,false,sizeof(vis));
    24     memset(w,0,sizeof(w));
    25     int tmp = INF;
    26     for(int i = 0; i < n; ++i){
    27         int theMax = -INF;
    28         for(int j = 0; j < n; j++)
    29             if(!vis[j] && !comb[j] && w[j] > theMax)
    30                 theMax = w[tmp = j];
    31         if(t == tmp) break;
    32         s = t;
    33         vis[t = tmp] = true;
    34         for(int j = 0; j < n; j++)
    35             if(!vis[j] && !comb[j])
    36                 w[j] += e[t][j];
    37     }
    38     return w[t];
    39 }
    40 int solve(){
    41     int ans = INF,s,t;
    42     memset(comb,0,sizeof(comb));
    43     for(int i = 1; i < n; i++){
    44         s = t = -1;
    45         ans = min(ans,Find(s,t));
    46         for(int j = 0; j < n; j++){
    47             e[s][j] += e[t][j];
    48             e[j][s] += e[j][t];
    49         }
    50         comb[t] = true;
    51     }
    52     return ans;
    53 }
    54 int main() {
    55     int u,v,w;
    56     while(~scanf("%d %d",&n,&m)){
    57         memset(e,0,sizeof(e));
    58         while(m--){
    59             scanf("%d %d %d",&u,&v,&w);
    60             e[u][v] += w;
    61             e[v][u] += w;
    62         }
    63         printf("%d
    ",solve());
    64     }
    65     return 0;
    66 }
    View Code
  • 相关阅读:
    在CSS中,让页面里的字体变清晰,变细
    前端路由优缺点
    HBuilder和HBuilderX有什么区别?
    HTML5有哪些新特性,移除了那些元素?如何处理HTML5新标签的浏览器兼容问题?如何区分HTML和HTML5?
    js中判断奇数或偶数
    遍历数组的方法
    数组的方法
    免费搜索引擎提交(登录)入口大全
    Vue.js详解
    简述JavaScript模块化编程(二)
  • 原文地址:https://www.cnblogs.com/crackpotisback/p/4003897.html
Copyright © 2011-2022 走看看