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

    Caocao's Bridges

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 2616    Accepted Submission(s): 849

    Problem Description
    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

    这题的意思就是求出所有的桥,然后输出桥的权值的最小值。

    但是坑点比较多。

    如果一开始是不连通的,输出0.

    图有重边,需要处理。

    还有如果取到的最小值是0的话,要输出1,表示要派一个人过去。

      1 #include <iostream>
      2 #include <queue>
      3 #include <cstdio>
      4 #include <cstring>
      5 #include <algorithm>
      6 #include <cmath>
      7 #include <cstdlib>
      8 #include <limits>
      9 #include <stack>
     10 #include <vector>
     11 #include <map>
     12 
     13 using namespace std;
     14 
     15 #define N 1350
     16 #define INF 0xfffffff
     17 #define PI acos (-1.0)
     18 #define EPS 1e-83
     19 
     20 struct node
     21 {
     22     int v, next, flow;
     23 }edge[N*N];
     24 
     25 int n, m, Time, minx, cnt;
     26 int low[N], dfn[N], f[N], head[N];
     27 
     28 vector<vector<int> > G;
     29 
     30 void init()
     31 {
     32     cnt = Time = 0;
     33     memset(low, 0, sizeof(low));
     34     memset(dfn, 0, sizeof(dfn));
     35     memset(f, 0, sizeof(f));
     36     memset(head, -1, sizeof(head));
     37 }
     38 
     39 void addedge(int u, int v, int flow)   // 邻接表存边
     40 {
     41     edge[cnt].v = v;
     42     edge[cnt].flow = flow;
     43     edge[cnt].next = head[u];
     44     head[u] = cnt;
     45     cnt++;
     46 }
     47 
     48 void Tarjan(int u, int fa)
     49 {
     50     f[u] = fa;
     51     low[u] = dfn[u] = ++Time;
     52     int k = 0;     // 去重边
     53 
     54     for(int i = head[u]; i != -1; i = edge[i].next)
     55     {
     56         int v = edge[i].v;
     57         if(fa == v && !k)
     58         {
     59             k++;
     60             continue;
     61         }
     62         if(!low[v])
     63         {
     64             Tarjan(v, u);
     65             low[u] = min(low[u], low[v]);
     66             if(low[v] > dfn[u])
     67                 minx = min(minx, edge[i].flow);  // 求桥的最小值
     68         }
     69         else
     70             low[u] = min(low[u], dfn[v]);
     71     }
     72 }
     73 
     74 int main()
     75 {
     76     int u, v, flow, k;
     77 
     78     while(scanf("%d%d", &n, &m), n+m)
     79     {
     80         init();
     81 
     82         while(m--)
     83         {
     84             scanf("%d%d%d", &u, &v, &flow);
     85             addedge(u, v, flow);
     86             addedge(v, u, flow);
     87         }
     88         k = 0, minx = INF;
     89 
     90         for(int i = 1; i <= n; i++)
     91         {
     92             if(!low[i])
     93             {
     94                 Tarjan(i, -1);
     95                 k++;   // 判断有几个连通图,
     96             }
     97         }
     98         if(k > 1) // 如果连通图大于1,输出0
     99         {
    100             puts("0");
    101             continue;
    102         }
    103         if(!minx)  // 桥最小值是0,需要派一个区攻击
    104             minx++;
    105         else if(minx == INF)  // 没有桥
    106             minx = -1;
    107         printf("%d
    ", minx);
    108     }
    109     return 0;
    110 }
    让未来到来 让过去过去
  • 相关阅读:
    Android自己定义组件系列【1】——自己定义View及ViewGroup
    LeetCode60:Permutation Sequence
    GitHub 优秀的 Android 开源项目
    view变化监听器ViewTreeObserver介绍
    android中ImageView的ScaleType属性
    Android静态图片人脸识别的完整demo(附完整源码)
    理解Android的手势识别
    Android浏览图片,点击放大至全屏效果
    Android中如何实现文件下载
    Android语音识别
  • 原文地址:https://www.cnblogs.com/Tinamei/p/4731947.html
Copyright © 2011-2022 走看看