kuangbin专题 专题九 连通图 HDU 4738 Caocao'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 = (int)1e3+10;
 7 int n,m,tot,tim,solders;
 8 int head[N],low[N],dfn[N];
 9 struct node{
10     int to,nxt,w;
11 }e[(N*N)<<1];
12
13 void init(){
14     for(int i = 0; i <= n; ++i){
15         head[i] = -1; dfn[i] = 0;
16     }
17     tot = tim = 0;
18     solders = (int)1e6;
19 }
20
21 inline void add(int u,int v,int w){
22     e[tot].to = v; e[tot].w = w; e[tot].nxt = head[u]; head[u] = tot++;
23 }
24
25 void tarjan(int now,int pre){
26     dfn[now] = low[now] = ++tim;
27     int to,pre_cnt = 0;
28     for(int o = head[now]; ~o; o = e[o].nxt){
29         to = e[o].to;
30         if(to == pre && pre_cnt == 0){ pre_cnt = 1; continue; }//重边处理
31         if(!dfn[to]){
32             tarjan(to,now);
33             low[now] = min(low[now],low[to]);
34             if(dfn[now] < low[to]) solders = min(solders,e[o].w);
35         }else low[now] = min(low[now],dfn[to]);
36     }
37 }
38
39 int main(){
40
41     int u,v,w;
42     while(~scanf("%d%d",&n,&m) && (n+m)){
43         init();
44         for(int i = 0;i < m; ++i){
45             scanf("%d%d%d",&u,&v,&w);
46             add(u,v,w); add(v,u,w);
47         }
48         tarjan(1,1);
49         for(int i = 1;i  <= n; ++i){
50             if(!dfn[i]){ solders = -2; break; }//岛本来就是分的
51         }
52         if(solders == -2) printf("0\n");
53         else{
54             if(solders == (int)1e6) solders = -1;//没法完成任务
55             printf("%d\n",!solders ? 1:solders);
56         }
57     }
58
59     return 0;
60 }

kuangbin专题 专题九 连通图 HDU 4738 Caocao's Bridges

原文地址:https://www.cnblogs.com/SSummerZzz/p/12206757.html

时间: 2024-11-05 15:52:54

kuangbin专题 专题九 连通图 HDU 4738 Caocao's Bridges的相关文章

Hdu 4738 Caocao&#39;s Bridges (连通图+桥)

题目链接: Hdu 4738 Caocao's Bridges 题目描述: 有n个岛屿,m个桥,问是否可以去掉一个花费最小的桥,使得岛屿边的不连通? 解题思路: 去掉一个边使得岛屿不连通,那么去掉的这个边一定是一个桥,所以我们只需要求出来所有的桥,然后比较每个桥的花费,选取最小的那个就好. 看起来很简单的样子哦!但是这个题目有很多的细节: A:题目中有重边,以后写Tarjan还是清一色判断重边吧.(除非题目特别要求) B:m个桥有可能连通不了这n个桥,这个时候不需要花费. C:当最小花费桥的花费

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

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

HDU 4738 Caocao's Bridges 题目链接 注意几个坑,可能重边,至少要派一个人去炸,没有连通的时候就不用炸了 代码: #include <cstdio> #include <cstring> #include <vector> #include <algorithm> using namespace std; const int N = 1005; const int INF = 0x3f3f3f3f; int pre[N], low[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 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

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

Caocao's Bridges Time Limit: 1000ms Memory Limit: 32768KB This problem will be judged on HDU. Original ID: 473864-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 wo

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

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