zoukankan      html  css  js  c++  java
  • UVA 10480 Sabotage

    Sabotage

    Time Limit: 3000ms
    Memory Limit: 131072KB
    This problem will be judged on UVA. Original ID: 10480
    64-bit integer IO format: %lld      Java class name: Main

    The regime of a small but wealthy dictatorship has been abruptly overthrown by an unexpected rebellion. Because of the enormous disturbances this is causing in world economy, an imperialist military super power has decided to invade the country and reinstall the old regime.

    For this operation to be successful, communication between the capital and the largest city must be completely cut. This is a difficult task, since all cities in the country are connected by a computer network using the Internet Protocol, which allows messages to take any path through the network. Because of this, the network must be completely split in two parts, with the capital in one part and the largest city in the other, and with no connections between the parts.

    There are large differences in the costs of sabotaging different connections, since some are much more easy to get to than others.

    Write a program that, given a network specification and the costs of sabotaging each connection, determines which connections to cut in order to separate the capital and the largest city to the lowest possible cost.

    Input 

    Input file contains several sets of input. The description of each set is given below.

    The first line of each set has two integers, separated by a space: First one the number of cities, n in the network, which is at most 50. The second one is the total number of connections, m, at most 500.

    The following m lines specify the connections. Each line has three parts separated by spaces: The first two are the cities tied together by that connection (numbers in the range 1 - n). Then follows the cost of cutting the connection (an integer in the range 1 to 40000000). Each pair of cites can appear at most once in this list.

    Input is terminated by a case where values of n and m are zero. This case should not be processed. For every input set the capital is city number 1, and the largest city is number 2.

    Output 

    For each set of input you should produce several lines of output. The description of output for each set of input is given below:

    The output for each set should be the pairs of cities (i.e. numbers) between which the connection should be cut (in any order), each pair on one line with the numbers separated by a space. If there is more than one solution, any one of them will do.

    Print a blank line after the output for each set of input.

    Sample Input 

    5 8
    1 4 30
    1 3 70
    5 3 20
    4 3 5
    4 5 15
    5 2 10
    3 2 25
    2 4 50
    5 8
    1 4 30
    1 3 70
    5 3 20
    4 3 5
    4 5 15
    5 2 10
    3 2 25
    2 4 50
    0 0
    

    Sample Output 

    4 1
    3 4
    3 5
    3 2
    
    4 1
    3 4
    3 5
    3 2

    解题:最小割
     1 #include <bits/stdc++.h>
     2 using namespace std;
     3 const int INF = ~0U>>2;
     4 const int maxn = 110;
     5 struct arc {
     6     int to,flow,next;
     7     arc(int x = 0,int y = 0,int z = -1) {
     8         to = x;
     9         flow = y;
    10         next = z;
    11     }
    12 } e[maxn*maxn];
    13 int head[maxn],gap[maxn],d[maxn],tot,S = 1,T = 2,n,m;
    14 void add(int u,int v,int w) {
    15     e[tot] = arc(v,w,head[u]);
    16     head[u] = tot++;
    17     e[tot] = arc(u,w,head[v]);
    18     head[v] = tot++;
    19 }
    20 int dfs(int u,int low) {
    21     if(u == T) return low;
    22     int tmp = 0,minH = n - 1;
    23     for(int i = head[u]; ~i; i = e[i].next) {
    24         if(e[i].flow) {
    25             if(d[u] == d[e[i].to] + 1) {
    26                 int a = dfs(e[i].to,min(low,e[i].flow));
    27                 e[i].flow -= a;
    28                 e[i^1].flow += a;
    29                 tmp += a;
    30                 low -= a;
    31                 if(!low) break;
    32                 if(d[S] >= n) return tmp;
    33             }
    34             if(e[i].flow) minH = min(minH,d[e[i].to]);
    35         }
    36     }
    37     if(!tmp) {
    38         if(--gap[d[u]] == 0) d[S] = n;
    39         ++gap[d[u] = minH + 1];
    40     }
    41     return tmp;
    42 }
    43 bool vis[maxn];
    44 void dfs(int u) {
    45     vis[u] = true;
    46     for(int i = head[u]; ~i; i = e[i].next) {
    47         if(vis[e[i].to]) continue;
    48         if(e[i].flow) dfs(e[i].to);
    49     }
    50 }
    51 int main() {
    52     int u,v,w;
    53     while(scanf("%d%d",&n,&m),n||m) {
    54         memset(head,-1,sizeof head);
    55         memset(gap,0,sizeof gap);
    56         memset(d,0,sizeof d);
    57         for(int i = tot = 0; i < m; ++i) {
    58             scanf("%d%d%d",&u,&v,&w);
    59             add(u,v,w);
    60         }
    61         gap[S] = n;
    62         int ret = 0;
    63         while(d[S] < n) ret += dfs(S,INF);
    64         memset(vis,false,sizeof vis);
    65         dfs(S);
    66         for(int i = 1; i <= n; ++i)
    67         for(int j = head[i]; ~j; j = e[j].next){
    68             if(!e[j].flow && vis[i] != vis[e[j].to])
    69                 printf("%d %d
    ",i,e[j].to);
    70         }
    71         puts("");
    72     }
    73     return 0;
    74 }
    View Code
  • 相关阅读:
    oracle外连接 (+)
    Struts 2的架构和运行流程
    struts2.3.15.1 中jsp:include与jsp:forward的用法
    tomcat6.0添加ssi(*.shtml)配置
    DWZ 刷新 dialog
    java将多个连续的空格转化成一个空格
    IE下不支持option的onclick事件
    struts2中的常量
    java.imageIo给图片添加水印
    java集合的互转
  • 原文地址:https://www.cnblogs.com/crackpotisback/p/4966750.html
Copyright © 2011-2022 走看看