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
  • 相关阅读:
    Eclipse与Tomcat
    乱入Spring+Mybatis
    windows一次无线网卡被关闭事件
    数列的考查角度收集整理2[三轮总结]
    数列的考查角度收集整理1[三轮总结]
    求函数的解析式
    不等式证明的那些事
    高中数学中最值素材整理【待编辑】
    函数与导数中常用的函数和不等关系
    坐标系与参数方程的考向整理[三轮总结]
  • 原文地址:https://www.cnblogs.com/crackpotisback/p/4003897.html
Copyright © 2011-2022 走看看