Hdu 4738 Caocao's Bridges (连通图+桥)

题目链接:

  Hdu 4738 Caocao‘s Bridges

题目描述:

  有n个岛屿,m个桥,问是否可以去掉一个花费最小的桥,使得岛屿边的不连通?

解题思路:

  去掉一个边使得岛屿不连通,那么去掉的这个边一定是一个桥,所以我们只需要求出来所有的桥,然后比较每个桥的花费,选取最小的那个就好。

看起来很简单的样子哦!但是这个题目有很多的细节:

  A:题目中有重边,以后写Tarjan还是清一色判断重边吧。(除非题目特别要求)

  B:m个桥有可能连通不了这n个桥,这个时候不需要花费。

  C:当最小花费桥的花费是0的话,应该输出1。

  1 #include <cstdio>
  2 #include <cstring>
  3 #include <iostream>
  4 #include <algorithm>
  5 using namespace std;
  6
  7 typedef long long LL;
  8 const int maxn = 1005;
  9 const int INF = 0x3f3f3f3f;
 10 struct node
 11 {
 12     int to, next, num;
 13 } edge[maxn*maxn];
 14 int head[maxn], low[maxn], dfn[maxn], stack[maxn];
 15 int id[maxn], cnt, tot, top, ntime;
 16
 17 void init ()
 18 {
 19     cnt = tot = top = ntime = 0;
 20     memset (id, 0, sizeof(id));
 21     memset (low, 0, sizeof(low));
 22     memset (dfn, 0, sizeof(dfn));
 23     memset (head, -1, sizeof(head));
 24     memset (stack, 0, sizeof(stack));
 25 }
 26 void Add (int from, int to, int num)
 27 {
 28     edge[tot].to = to;
 29     edge[tot].num = num;
 30     edge[tot].next = head[from];
 31     head[from] = tot ++;
 32 }
 33 void Tarjan (int u, int father)
 34 {
 35     int k = 0;
 36     low[u] = dfn[u] = ++ntime;
 37     stack[top++] = u;
 38     for (int i=head[u]; i!=-1; i=edge[i].next)
 39     {
 40         int v = edge[i].to;
 41         if (v == father && !k)
 42         {
 43             k ++;
 44             continue;
 45         }
 46         if (!dfn[v])
 47         {
 48             Tarjan (v, u);
 49             low[u] = min (low[u], low[v]);
 50         }
 51         else
 52             low[u] = min (low[u], dfn[v]);
 53     }
 54     if (low[u] == dfn[u])
 55     {
 56         cnt ++;
 57         while (1)
 58         {
 59             int v = stack[--top];
 60             id[v] = cnt;
 61             if (v == u)
 62                 break;
 63         }
 64     }
 65 }
 66 void solve (int n)
 67 {
 68     int mini = INF, k = 0;
 69     for (int i=1; i<=n; i++)
 70         if (!dfn[i])
 71         {
 72             k ++;
 73             Tarjan(i, 0);
 74         }
 75     if (cnt == 1)
 76     {
 77         printf ("-1\n");
 78         return ;
 79     }
 80     if (k > 1)
 81     {
 82         printf ("0\n");
 83         return ;
 84     }
 85     for (int i=1; i<=n; i++)
 86         for (int j=head[i]; j!=-1; j=edge[j].next)
 87         {
 88             int u = id[i];
 89             int v = id[edge[j].to];
 90             if (u != v && mini > edge[j].num)
 91                 mini = edge[j].num;
 92         }
 93     if (mini == 0)
 94         mini ++;
 95     printf("%d\n", mini);
 96 }
 97 int main ()
 98 {
 99     int n, m;
100     while (scanf ("%d %d", &n, &m), n+m)
101     {
102         int u, v, num;
103         init ();
104         while (m --)
105         {
106             scanf ("%d %d %d", &u, &v, &num);
107             Add (u, v, num);
108             Add (v, u, num);
109         }
110         solve (n);
111     }
112     return 0;
113 }

Hdu 4738 Caocao's Bridges (连通图+桥)

时间: 2024-10-10 18:03:33

Hdu 4738 Caocao's Bridges (连通图+桥)的相关文章

hdu 4738 Caocao&#39;s Bridges(桥的最小权值+去重)

http://acm.hdu.edu.cn/showproblem.php?pid=4738 题目大意:曹操有一些岛屿被桥连接,每座都有士兵把守,周瑜想把这些岛屿分成两部分,但他只能炸毁一条桥,问最少需要派几个士兵去;如果不能完成输出-1 1:如果这些岛屿不连通,则不需要派人前去 2:如果桥的守卫是0的话也得派一人去炸毁 3:如果不能完成输出-1 4:输出最少需派的人数 #include<stdio.h> #include<string.h> #include<math.h&

HDU 4738 Caocao&#39;s Bridges tarjan求桥

Caocao's Bridges 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 Chan

kuangbin专题 专题九 连通图 HDU 4738 Caocao&#39;s Bridges

题目链接:https://vjudge.net/problem/HDU-4738 题目:tarjan求桥,坑点: 题目说是分岛任务...如果所有岛之间没有完全连通,就不需要执行任务了...答案直接是0... 桥上可能没人,但是,炸弹需要一个人去送,所以至少1个人. 1 #include <iostream> 2 #include <cstdio> 3 #include <algorithm> 4 using namespace std; 5 6 const int N

HDU 4738——Caocao&#39;s Bridges——————【求割边/桥的最小权值】

Caocao's Bridges Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Practice HDU 4738 Description Caocao was defeated by Zhuge Liang and Zhou Yu in the battle of Chibi. But he wouldn't give up. Caocao's army st

HDU 4738 Caocao&#39;s Bridges(求价值最小的桥)

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

HDU 4738 Caocao&#39;s Bridges ——(找桥,求联通块)

题意:给你一个无向图,给你一个炸弹去炸掉一条边,使得整个图不再联通,你需要派人去安置炸弹,且派去的人至少要比这条边上的人多.问至少要派去多少个,如果没法完成,就输出-1. 分析:如果这个图是已经是多个联通块了,那么一个人都不用去,如果不是,那么只要找出这个无向图上的桥并且哨兵数量最少的那座把它炸了就行(输出这条边上的哨兵数量即可).直接tarjan就可以写. 注意点:1.可能有重边,所以用手写邻接表的方式存图:2.如果一座桥上没有哨兵,那么你也得至少派去一个人去安置炸弹(因为炸弹不会自己飞过去啊

HDU 4738 --Caocao&#39;s Bridges 【无向图边双联通 &amp;&amp; 求权值最小的桥 &amp;&amp; 模板】

Caocao's Bridges Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2641    Accepted Submission(s): 855 Problem Description Caocao was defeated by Zhuge Liang and Zhou Yu in the battle of Chibi. B

HDU 4738 Caocao&#39;s Bridges(割边)

乍一看一个模板题,仔细一看还是模板题,但是三个坑.1,不是连通图,放0个.2 守卫为0,放1个. 3注意重边. #include<iostream> #include<cstdio> #include<vector> #include<queue> #include<algorithm> #include<stack> #include<cstring> using namespace std; #define maxn

hdu 4738 Caocao&#39;s Bridges tarjan

Caocao's Bridges Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=4738 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