zoukankan      html  css  js  c++  java
  • HDU 4738 Caocao's Bridges

    Caocao's Bridges

    Time Limit: 1000ms
    Memory Limit: 32768KB
    This problem will be judged on HDU. Original ID: 4738
    64-bit integer IO format: %I64d      Java class name: Main
     
    Caocao was defeated by Zhuge Liang and Zhou Yu in the battle of Chibi. But he wouldn't give up. Caocao's army still was not good at water battles, so he came up with another idea. He built many islands in the Changjiang river, and based on those islands, Caocao's army could easily attack Zhou Yu's troop. Caocao also built bridges connecting islands. If all islands were connected by bridges, Caocao's army could be deployed very conveniently among those islands. Zhou Yu couldn't stand with that, so he wanted to destroy some Caocao's bridges so one or more islands would be seperated from other islands. But Zhou Yu had only one bomb which was left by Zhuge Liang, so he could only destroy one bridge. Zhou Yu must send someone carrying the bomb to destroy the bridge. There might be guards on bridges. The soldier number of the bombing team couldn't be less than the guard number of a bridge, or the mission would fail. Please figure out as least how many soldiers Zhou Yu have to sent to complete the island seperating mission.
     

    Input

    There are no more than 12 test cases.

    In each test case:

    The first line contains two integers, N and M, meaning that there are N islands and M bridges. All the islands are numbered from 1 to N. ( 2 <= N <= 1000, 0 < M <= N2 )

    Next M lines describes M bridges. Each line contains three integers U,V and W, meaning that there is a bridge connecting island U and island V, and there are W guards on that bridge. ( U ≠ V and 0 <= W <= 10,000 )

    The input ends with N = 0 and M = 0.
     

    Output

    For each test case, print the minimum soldier number Zhou Yu had to send to complete the mission. If Zhou Yu couldn't succeed any way, print -1 instead.
     

    Sample Input

    3 3
    1 2 7
    2 3 4
    3 1 4
    3 2
    1 2 7
    2 3 4
    0 0

    Sample Output

    -1
    4

    Source

     
    解题:注意存在不连通情况。。。草草的桥。。。
     
     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <cmath>
     5 #include <algorithm>
     6 #include <climits>
     7 #include <vector>
     8 #include <queue>
     9 #include <cstdlib>
    10 #include <string>
    11 #include <set>
    12 #include <stack>
    13 #define LL long long
    14 #define INF 0x3f3f3f3f
    15 #define pii pair<int,int>
    16 using namespace std;
    17 const int maxn = 1005;
    18 struct arc {
    19     int to,id,next;
    20     arc(int x = 0,int y = 0,int z = -1) {
    21         to = x;
    22         id = y;
    23         next = z;
    24     }
    25 };
    26 arc e[maxn*maxn*2];
    27 int head[maxn],dfn[maxn],low[maxn],uf[maxn];
    28 int tot,idx,ans,p[maxn*maxn*2],n,m;
    29 int Find(int x){
    30     if(x == uf[x]) return uf[x];
    31     return uf[x] = Find(uf[x]);
    32 }
    33 void add(int u,int v,int id) {
    34     e[tot] = arc(v,id,head[u]);
    35     head[u] = tot++;
    36     e[tot] = arc(u,id,head[v]);
    37     head[v] = tot++;
    38 }
    39 void tarjan(int u,int fa) {
    40     dfn[u] = low[u] = ++idx;
    41     bool flag = true;
    42     for(int i = head[u]; ~i; i = e[i].next) {
    43         if(e[i].to == fa && flag) {
    44             flag = false;
    45             continue;
    46         }
    47         if(!dfn[e[i].to]) {
    48             tarjan(e[i].to,u);
    49             low[u] = min(low[u],low[e[i].to]);
    50             if(low[e[i].to] > dfn[u] && p[e[i].id] < ans)
    51                 ans = p[e[i].id];
    52         } else low[u] = min(low[u],dfn[e[i].to]);
    53     }
    54 }
    55 void init() {
    56     for(int i = 0; i < maxn; ++i) {
    57         head[i] = -1;
    58         dfn[i] = low[i] = 0;
    59         uf[i] = i;
    60     }
    61     idx = tot = 0;
    62     ans = INF;
    63 }
    64 int main() {
    65     int u,v;
    66     while(scanf("%d %d",&n,&m),n||m) {
    67         init();
    68         for(int i = 1; i <= m; ++i) {
    69             scanf("%d %d %d",&u,&v,p+i);
    70             add(u,v,i);
    71             int tx = Find(u);
    72             int ty = Find(v);
    73             if(tx != ty) uf[tx] = ty;
    74         }
    75         int root = 0;
    76         for(int i = 1; i <= n; ++i) if(uf[i] == i) root++;
    77         if(root > 1){
    78             printf("%d
    ",0);
    79             continue;
    80         }
    81         for(int i = 1; i <= n; ++i)
    82             if(!dfn[i]) tarjan(i,-1);
    83         if(ans == INF) puts("-1");
    84         else if(ans) printf("%d
    ",ans);
    85         else puts("1");
    86     }
    87     return 0;
    88 }
    View Code
  • 相关阅读:
    java之大文件断点续传
    Html5大文件断点续传
    前端js怎么实现大文件G级的断点续传(分块上传)和分段下载
    HTML5解决大文件断点续传
    完整版断点续传、秒传,支持超大大大文件_支持重定义文件名和路径
    起来吧!不要做奴隶的ITproject师们!
    Oracle PL/SQL 编程基础 实例
    C语言高速入门系列(八)
    POJ 3253-Fence Repair(堆)
    抽屉式导航可能减少产品一半的用户參与度
  • 原文地址:https://www.cnblogs.com/crackpotisback/p/4106089.html
Copyright © 2011-2022 走看看