zoukankan      html  css  js  c++  java
  • HDU-6214 Smallest Minimum Cut(最少边最小割)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6214

    Problem Description
    Consider a network G=(V,E) with source s and sink t. An s-t cut is a partition of nodes set V into two parts such that s and t belong to different parts. The cut set is the subset of E with all edges connecting nodes in different parts. A minimum cut is the one whose cut set has the minimum summation of capacities. The size of a cut is the number of edges in the cut set. Please calculate the smallest size of all minimum cuts.
     
    Input
    The input contains several test cases and the first line is the total number of cases T (1T300).
    Each case describes a network G, and the first line contains two integers n (2n200) and m (0m1000) indicating the sizes of nodes and edges. All nodes in the network are labelled from 1 to n.
    The second line contains two different integers s and t (1s,tn) corresponding to the source and sink.
    Each of the next m lines contains three integers u,v and w (1w255) describing a directed edge from node u to v with capacity w.
     
    Output
    For each test case, output the smallest size of all minimum cuts in a line.
     
    Sample Input
    2 4 5 1 4 1 2 3 1 3 1 2 3 1 2 4 1 3 4 2 4 5 1 4 1 2 3 1 3 1 2 3 1 2 4 1 3 4 3
     
    Sample Output
    2 3
    题意:求最小边数最小割
    解题思路:如果一个网络的边权全部为1,那么这个网络的最小割的最小边数就是最大流的流量,那么我们把原先的边变成“1”,需要将原边权v变为v*hash+1,hash>m,最大流mod hash即为最少边最小割
      1 #include<bits/stdc++.h>
      2 using namespace std;
      3 typedef long long ll;
      4 typedef unsigned long long ull;
      5 #define INF 0x3f3f3f3f
      6 const ll MAXN = 200 + 7;
      7 const ll MAXM = 1e3 + 7;
      8 const ll MOD = 1e9 + 7;
      9 const double pi = acos(-1);
     10 int cnt = -1, head[MAXM], dis[MAXN], cur[MAXM];
     11 int n, m;
     12 int s,t;
     13 struct edge
     14 {
     15     int to, value, net;
     16 } e[MAXM << 1]; ///共有n*2条边
     17 void add(int from, int to, int value)
     18 { ///链式前向星
     19     cnt++;
     20     e[cnt].to = to;
     21     e[cnt].value = value;
     22     e[cnt].net = head[from];
     23     head[from] = cnt;
     24 }
     25 int bfs(int st, int ed)
     26 { ///建立层次图
     27     queue<int> que;
     28     memset(dis, -1, sizeof(dis));
     29     dis[st] = 0;
     30     que.push(st);
     31     while (!que.empty())
     32     {
     33         int x = que.front();
     34         que.pop();
     35         for (int i = head[x]; i > -1; i = e[i].net)
     36         {
     37             int now = e[i].to;
     38             if (dis[now] == -1 && e[i].value)
     39             {
     40                 que.push(now);
     41                 dis[now] = dis[x] + 1;
     42             }
     43         }
     44     }
     45     return dis[ed] != -1;
     46 }
     47 int dfs(int x, int t, int maxflow)
     48 {
     49     if (x == t)
     50         return maxflow;
     51     int ans = 0;
     52     for (int i = cur[x]; i > -1; i = e[i].net)
     53     { ///当前弧优化
     54         int now = e[i].to;
     55         if (dis[now] != dis[x] + 1 || e[i].value == 0 || ans >= maxflow)
     56             continue;
     57         cur[x] = i;
     58         int f = dfs(now, t, min(e[i].value, maxflow - ans));
     59         e[i].value -= f;
     60         e[i ^ 1].value += f; ///反向边加流量
     61         ans += f;
     62     }
     63     if (!ans)
     64         dis[x] = -1; ///炸点优化
     65     return ans;
     66 }
     67 int Dinic(int st, int ed)
     68 {
     69     int ans = 0;
     70     while (bfs(st, ed))
     71     {
     72         memcpy(cur, head, sizeof(head));
     73         int k;
     74         while ((k = dfs(st, ed, INF)))
     75             ans += k;
     76     }
     77     return ans;
     78 }
     79 void init()
     80 {
     81     cnt=-1;
     82     memset(head,-1,sizeof(head));
     83 }
     84 int main()
     85 {
     86     int temp;
     87     scanf("%d", &temp);
     88     while (temp--)
     89     {
     90         scanf("%d%d", &n, &m);
     91         scanf("%d%d", &s, &t);
     92         init();
     93         int hash=m+1;
     94         for (int i = 0; i < m; i++)
     95         {
     96             int u, v, w;
     97             scanf("%d%d%d", &u, &v, &w);
     98             add(u, v, w*hash+1);
     99             add(v, u, 0);
    100         }
    101         printf("%d
    ",Dinic(s,t)%(hash));
    102     }
    103     return 0;
    104 }
     
  • 相关阅读:
    js浏览器之浏览器对象
    js浏览器
    js面向对象编程之创建对象
    js面向对象编程
    STS清理
    使用STS新的工作空间无需再配置
    去掉文本框中的空格
    MySQL轻量版使用,无需安装,无脑操作
    Eclipse&STS常用小技巧
    如何配置上传虚拟路径问题?
  • 原文地址:https://www.cnblogs.com/graytido/p/10849052.html
Copyright © 2011-2022 走看看