zoukankan      html  css  js  c++  java
  • HDU 4005 The war

    The war

    Time Limit: 1000ms
    Memory Limit: 65768KB
    This problem will be judged on HDU. Original ID: 4005
    64-bit integer IO format: %I64d      Java class name: Main
     
    In the war, the intelligence about the enemy is very important. Now, our troop has mastered the situation of the enemy's war zones, and known that these war zones can communicate to each other directly or indirectly through the network. We also know the enemy is going to build a new communication line to strengthen their communication network. Our task is to destroy their communication network, so that some of their war zones can't communicate. Each line has its "cost of destroy". If we want to destroy a line, we must spend the "cost of destroy" of this line. We want to finish this task using the least cost, but our enemy is very clever. Now, we know the network they have already built, but we know nothing about the new line which our enemy is going to build. In this condition, your task is to find the minimum cost that no matter where our enemy builds the new line, you can destroy it using the fixed money. Please give the minimum cost. For efficiency, we can only destroy one communication line.
     

    Input

    The input contains several cases. For each cases, the first line contains two positive integers n, m (1<=n<=10000, 0<=m<=100000) standing for the number of the enemy's war zones (numbered from 1 to n), and the number of lines that our enemy has already build. Then m lines follow. For each line there are three positive integer a, b, c (1<=a, b<=n, 1<=c<=100000), meaning between war zone A and war zone B there is a communication line with the "cost of destroy " c.
     

    Output

    For each case, if the task can be finished output the minimum cost, or output ‐1.
     

    Sample Input

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

    Sample Output

    -1
    3
    Hint
    For the second sample input: our enemy may build line 2 to 3, 2 to 4, 3 to 4. If they build line 2 to 3, we will destroy line 1 to 4, cost 3. If they build line 2 to 4, we will destroy line 1 to 3, cost 2. If they build line 3 to 4, we will destroy line 1 to 2, cost 1. So, if we want to make sure that we can destroy successfully, the minimum cost is 3.

    Source

     
    解题:边双连通分量+dp
     1 #include <bits/stdc++.h>
     2 using namespace std;
     3 const int maxn = 10010;
     4 const int INF = 0x3f3f3f3f;
     5 struct arc{
     6     int to,w,next;
     7     arc(int x = 0,int y = 0,int z = -1){
     8         to = x;
     9         w = y;
    10         next = z;
    11     }
    12 }e[500000];
    13 int hd[maxn],hd2[maxn],low[maxn],dfn[maxn],belong[maxn],clk,bcc,tot;
    14 void add(int *head,int u,int v,int w){
    15     e[tot] = arc(v,w,head[u]);
    16     head[u] = tot++;
    17 }
    18 stack<int>stk;
    19 void tarjan(int u,int fa){
    20     dfn[u] = low[u] = ++clk;
    21     stk.push(u);
    22     bool flag = false;
    23     for(int i = hd[u]; ~i; i = e[i].next){
    24         if(e[i].to == fa && !flag){
    25             flag = true;
    26             continue;
    27         }
    28         if(!dfn[e[i].to]){
    29             tarjan(e[i].to,u);
    30             low[u] = min(low[u],low[e[i].to]);
    31         }else low[u] = min(low[u],dfn[e[i].to]);
    32     }
    33     if(low[u] == dfn[u]){
    34         int v;
    35         ++bcc;
    36         do{
    37           v = stk.top();
    38           stk.pop();
    39           belong[v] = bcc;
    40         }while(v != u);
    41     }
    42 }
    43 
    44 int ret = INF,dp[maxn];
    45 void dfs(int u,int fa){
    46     for(int i = hd2[u]; ~i; i = e[i].next){
    47         if(e[i].to == fa) continue;
    48         dfs(e[i].to,u);
    49         dp[e[i].to] = min(dp[e[i].to],e[i].w);
    50         if(dp[u] > dp[e[i].to]){
    51             ret = min(ret,dp[u]);
    52             dp[u] = dp[e[i].to];
    53         }else ret = min(ret,dp[e[i].to]);
    54     }
    55 }
    56 void init(){
    57     for(int i = 0; i < maxn; ++i){
    58         hd[i] = hd2[i] = -1;
    59         dfn[i] = belong[i] = 0;
    60         dp[i] = INF;
    61     }
    62     tot = bcc = clk = 0;
    63     while(!stk.empty()) stk.pop();
    64 }
    65 int main(){
    66     int n,m,u,v,w;
    67     while(~scanf("%d%d",&n,&m)){
    68         init();
    69         for(int i = 0; i < m; ++i){
    70             scanf("%d%d%d",&u,&v,&w);
    71             add(hd,u,v,w);
    72             add(hd,v,u,w);
    73         }
    74         for(int i = 1; i <= n; ++i)
    75             if(!dfn[i]) tarjan(i,-1);
    76         if(bcc == 1){
    77             puts("-1");
    78             continue;
    79         }
    80         int minW = INF,id = 0;
    81         for(int tt = tot, i = 0; i < tt; i += 2){
    82             if(belong[e[i].to] == belong[e[i^1].to]) continue;
    83             add(hd2,belong[e[i].to],belong[e[i^1].to],e[i].w);
    84             add(hd2,belong[e[i^1].to],belong[e[i].to],e[i].w);
    85             if(e[i].w < minW) minW = e[id = i].w;
    86         }
    87         ret = INF;
    88         dfs(belong[e[id].to],belong[e[id^1].to]);
    89         dfs(belong[e[id^1].to],belong[e[id].to]);
    90         printf("%d
    ",ret == INF?-1:ret);
    91     }
    92     return 0;
    93 }
    View Code
  • 相关阅读:
    剑指offer——关于排序算法的应用(一):归并排序
    剑指offer——关于排序算法的应用:选择排序和冒泡排序
    剑指offer:将矩阵选择、螺旋输出矩阵——Python之光
    剑指offer:链表——常见的多指针协同操作:
    剑指Offer:编程习惯篇:代码鲁棒性,代码可扩展性——防御性的编程习惯,解决问题时方法分模块考虑
    剑指offer:数字二进制含1个数,快速幂运算:二进制位运算的运用
    剑指offer:斐波那契数列,跳台阶,变态跳台阶——斐波那契数列类题目:
    回溯法实现各种组合的检索:
    剑指offer:二维数组中查找
    jdk生成https证书的方法
  • 原文地址:https://www.cnblogs.com/crackpotisback/p/4758666.html
Copyright © 2011-2022 走看看