zoukankan      html  css  js  c++  java
  • HDU 2435 There is a war

    There is a war

    Time Limit: 1000ms
    Memory Limit: 32768KB
    This problem will be judged on HDU. Original ID: 2435
    64-bit integer IO format: %I64d      Java class name: Main
    There is a sea.
    There are N islands in the sea.
    There are some directional bridges connecting these islands.
    There is a country called Country One located in Island 1.
    There is another country called Country Another located in Island N.
    There is a war against Country Another, which launched by Country One.
    There is a strategy which can help Country Another to defend this war by destroying the bridges for the purpose of making Island 1 and Island n disconnected.
    There are some different destroying costs of the bridges.
    There is a prophet in Country Another who is clever enough to find the minimum total destroying costs to achieve the strategy.
    There is an architecture in Country One who is capable enough to rebuild a bridge to make it unbeatable or build a new invincible directional bridge between any two countries from the subset of island 2 to island n-1.
    There is not enough time for Country One, so it can only build one new bridge, or rebuild one existing bridge before the Country Another starts destroying, or do nothing if happy.
    There is a problem: Country One wants to maximize the minimum total destroying costs Country Another needed to achieve the strategy by making the best choice. Then what’s the maximum possible result?

    Input

    There are multiple cases in this problem.
    There is a line with an integer telling you the number of cases at the beginning.
    The are two numbers in the first line of every case, N(4<=N<=100) and M(0<=M<=n*(n-1)/2), indicating the number of islands and the number of bridges.
    There are M lines following, each one of which contains three integers a, b and c, with 1<=a, b<=N and 1<=c<=10000, meaning that there is a directional bridge from a to b with c being the destroying cost.
    There are no two lines containing the same a and b.

    Output

    There is one line with one integer for each test case, telling the maximun possible result.

    Sample Input

    4
    4 0
    4 2
    1 2 2
    3 4 2
    4 3
    1 2 1
    2 3 1
    3 4 10
    4 3
    1 2 5
    2 3 2
    3 4 3

    Sample Output

    0
    2
    1
    3

    Source

     
    解题:暴力枚举 + 最小割
     1 #include <bits/stdc++.h>
     2 using namespace std;
     3 const int INF = ~0U>>2;
     4 const int maxn = 200;
     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],d[maxn],gap[maxn],tot,S,T;
    14 void add(int u,int v,int flow) {
    15     e[tot] = arc(v,flow,head[u]);
    16     head[u] = tot++;
    17     e[tot] = arc(u,0,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 = T - 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(e[i].flow,low));
    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] >= T) return tmp;
    33             }
    34         }
    35         if(e[i].flow) minH = min(minH,d[e[i].to]);
    36     }
    37     if(!tmp) {
    38         if(--gap[d[u]] == 0) d[S] = T;
    39         ++gap[d[u] = minH + 1];
    40     }
    41     return tmp;
    42 }
    43 int sap(int ret = 0) {
    44     memset(gap,0,sizeof gap);
    45     memset(d,0,sizeof d);
    46     gap[S] = T;
    47     while(d[S] < T) ret += dfs(S,INF);
    48     return ret;
    49 }
    50 bool vis[maxn];
    51 void dfs(int u) {
    52     vis[u] = true;
    53     for(int i = head[u]; ~i; i = e[i].next)
    54         if(e[i].flow && !vis[e[i].to]) dfs(e[i].to);
    55 }
    56 int a[maxn*maxn],b[maxn*maxn],c[maxn*maxn];
    57 int main() {
    58     int n,m,kase;
    59     scanf("%d",&kase);
    60     while(kase--) {
    61         scanf("%d%d",&n,&m);
    62         memset(head,-1,sizeof head);
    63         for(int i = tot = 0; i < m; ++i) {
    64             scanf("%d%d%d",a + i,b + i,c + i);
    65             add(a[i],b[i],c[i]);
    66         }
    67         S = 1;
    68         T = n;
    69         int ret = sap();
    70         memset(vis,false,sizeof vis);
    71         dfs(S);
    72         for(int i = 2; i < n; ++i) {
    73             if(!vis[i]) continue;
    74             for(int j = 2; j < n; ++j) {
    75                 if(vis[j]) continue;
    76                 memset(head,-1,sizeof head);
    77                 for(int k = tot = 0; k < m; ++k)
    78                     add(a[k],b[k],c[k]);
    79                 add(i,j,INF);
    80                 ret = max(ret,sap());
    81             }
    82         }
    83         printf("%d
    ",ret);
    84     }
    85     return 0;
    86 }
    View Code
  • 相关阅读:
    获取各种屏幕宽度、高度
    java中获取本地文件的编码
    线程通信与进程通信的区别
    女性长期没有性生活有什么危害?
    面试中你必须要知道的语言陷阱
    当项目出现空闲时候,如何开展软件测试工作?
    同样是做鸭的 绝味与周黑鸭的区别咋那么大?!
    javaIO(二)
    (原)代码块
    this的使用
  • 原文地址:https://www.cnblogs.com/crackpotisback/p/4971479.html
Copyright © 2011-2022 走看看