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


  • 相关阅读:
    iptables防火墙(RHEL6)
    漏洞扫描与网络抓包
    服务安全与监控
    Typecho反序列化漏洞
    python类,魔术方法等学习&&部分ssti常见操作知识点复习加深
    PHAR伪协议&&[CISCN2019 华北赛区 Day1 Web1]Dropbox
    [GXYCTF2019]禁止套娃 1 &无参数RCE
    PHP代码审计学习(1)
    Yii2安装完kartik组件后,使用时报错
    收藏博客
  • 原文地址:https://www.cnblogs.com/DOLFAMINGO/p/7538608.html
Copyright © 2011-2022 走看看