zoukankan      html  css  js  c++  java
  • POJ2914 Minimum Cut —— 最小割

    题目链接:http://poj.org/problem?id=2914


    Minimum Cut
    Time Limit: 10000MS   Memory Limit: 65536K
    Total Submissions: 10117   Accepted: 4226
    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 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

    Baidu Star 2006 Semifinal 
    Wang, Ying (Originator) 
    Chen, Shixi (Test cases)




    代码如下:

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <algorithm>
     5 #include <vector>
     6 #include <cmath>
     7 #include <queue>
     8 #include <stack>
     9 #include <map>
    10 #include <string>
    11 #include <set>
    12 using namespace std;
    13 typedef long long LL;
    14 const int INF = 2e9;
    15 const LL LNF = 9e18;
    16 const int mod = 1e9+7;
    17 const int MAXN = 500+10;
    18 
    19 int mp[MAXN][MAXN];
    20 bool combine[MAXN];
    21 int n, m;
    22 
    23 bool vis[MAXN];
    24 int w[MAXN];
    25 int prim(int times, int &s, int &t) //最大生成树?
    26 {
    27     memset(w,0,sizeof(w));
    28     memset(vis,0,sizeof(vis));
    29     for(int i = 1; i<=times; i++)   //times为实际的顶点个数
    30     {
    31         int k, maxx = -INF;
    32         for(int j = 0; j<n; j++)
    33             if(!vis[j] && !combine[j] && w[j]>maxx)
    34                 maxx = w[k=j];
    35 
    36         vis[k] = 1;
    37         s = t; t = k;
    38         for(int j = 0; j<n; j++)
    39             if(!vis[j] && !combine[j])
    40                 w[j] += mp[k][j];
    41     }
    42     return w[t];
    43 }
    44 
    45 int mincut()
    46 {
    47     int ans = INF;
    48     memset(combine,0,sizeof(combine));
    49     for(int i = n; i>=2; i--)   //每一次循环,就减少一个点
    50     {
    51         int s, t;
    52         int tmp = prim(i, s, t);
    53         ans = min(ans, tmp);
    54         combine[t] = 1;
    55         for(int j = 0; j<n; j++)   //把t点删掉,与t相连的边并入s
    56         {
    57             mp[s][j] += mp[t][j];
    58             mp[j][s] += mp[j][t];
    59         }
    60     }
    61     return ans;
    62 }
    63 
    64 int main()
    65 {
    66     while(scanf("%d%d",&n,&m)!=EOF)
    67     {
    68         memset(mp,0,sizeof(mp));
    69         for(int i = 1; i<=m; i++)
    70         {
    71             int u, v, w;
    72             scanf("%d%d%d",&u,&v,&w);
    73             mp[u][v] += w;
    74             mp[v][u] += w;
    75         }
    76         cout<< mincut() <<endl;
    77     }
    78     return 0;
    79 }
    View Code


  • 相关阅读:
    redis
    Ubuntu 用VSFTP搭建FTP服务器
    ADO.NET中高效地使用数据库连接
    类型后面加问号 int?
    system.data.sqlclient.sqlexception:timeout expired
    BackgroundWorker超级详细的用法
    异步调用WCF服务,并将数据填入GridLookUpEdit中
    linq to entity报"不允许启动新事务,因为有其他线程正在该会话中运行."错误解决方法
    sql2005 数据库备份 对于服务器失败
    调用WCF服务返回list集合时出错的解决方法
  • 原文地址:https://www.cnblogs.com/DOLFAMINGO/p/7538608.html
Copyright © 2011-2022 走看看