HDU——T 4738 Caocao's Bridges
http://acm.hdu.edu.cn/showproblem.php?pid=4738
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 5067 Accepted Submission(s): 1589
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.
1 2 7
2 3 4
3 1 4
3 2
1 2 7
2 3 4
0 0
4
#include <algorithm>
#include <cstring>
#include <cstdio> using namespace std; const int INF(0x7fffffff);
const int N(+);
const int M(N*N);
int n,m,u,v,w,ans,num;
int head[N],sumedge;
struct Edge
{
int to,next,w;
Edge(int to=,int next=,int w=) :
to(to),next(next),w(w){}
}edge[M<<]; void ins(int from,int to,int w)
{
edge[++sumedge]=Edge(to,head[from],w);
head[from]=sumedge;
} int dfn[N],low[N],tim;
void DFS(int now,int pre)
{
low[now]=dfn[now]=++tim;
int sumtredge=;
for(int i=head[now];i!=-;i=edge[i].next)
if((i^)!=pre)
{
int go=edge[i].to;
if(!dfn[go])
{
sumtredge++;
DFS(go,i);
if(low[go]>dfn[now])
ans=min(ans,edge[i].w);
low[now]=min(low[now],low[go]);
}
else low[now]=min(low[now],dfn[go]);
}
} int main()
{
for(;;)
{
scanf("%d%d",&n,&m);
if(!n&&!m) break;
sumedge=-;num=;ans=INF;tim=;
memset(head,-,sizeof(head));
memset(low,,sizeof(low));
memset(dfn,,sizeof(dfn));
for(int i=;i<=m;i++)
{
scanf("%d%d%d",&u,&v,&w);
ins(u,v,w); ins(v,u,w);
}
for(int i=;i<=n;i++)
if(!dfn[i]) DFS(i,-),num++;
if(num>) puts("");
else if(!ans) puts("");
else if(ans==INF) puts("-1");
else printf("%d\n",ans);
}
return ;
}
HDU——T 4738 Caocao's Bridges的更多相关文章
- Hdu 4738 Caocao's Bridges (连通图+桥)
题目链接: Hdu 4738 Caocao's Bridges 题目描述: 有n个岛屿,m个桥,问是否可以去掉一个花费最小的桥,使得岛屿边的不连通? 解题思路: 去掉一个边使得岛屿不连通,那么去掉的这 ...
- HDU 4738 Caocao's Bridges(Tarjan求桥+重边判断)
Caocao's Bridges Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- HDU 4738——Caocao's Bridges——————【求割边/桥的最小权值】
Caocao's Bridges Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u S ...
- hdu 4738 Caocao's Bridges 图--桥的判断模板
Caocao's Bridges Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- HDU 4738 Caocao's Bridges
Caocao's Bridges Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- HDU 4738 Caocao's Bridges (2013杭州网络赛1001题,连通图,求桥)
Caocao's Bridges Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- HDU——4738 Caocao's Bridges
Caocao's Bridges Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- hdoj 4738 Caocao's Bridges【双连通分量求桥】
Caocao's Bridges Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- hdu 4738 Caocao's Bridges(桥的最小权值+去重)
http://acm.hdu.edu.cn/showproblem.php?pid=4738 题目大意:曹操有一些岛屿被桥连接,每座都有士兵把守,周瑜想把这些岛屿分成两部分,但他只能炸毁一条桥,问最少 ...
随机推荐
- bzoj2763 [JLOI]飞行路线 分层图最短路
问题描述 Alice和Bob现在要乘飞机旅行,他们选择了一家相对便宜的航空公司.该航空公司一共在n个城市设有业务,设这些城市分别标记为0到n-1,一共有m种航线,每种航线连接两个城市,并且航线有一定的 ...
- [洛谷P1156][codevs1684]垃圾陷阱
题目大意:一头奶牛掉进了深度为d的坑里,现在有g个垃圾在特定时刻ti扔进来.奶牛可以吃垃圾以获得体力,吃第i个垃圾能获得mi的体力,也可以堆放垃圾以逃生,第i个垃圾高度为hi.当高度≥d时奶牛成功逃生 ...
- python之禅---对象与元类
众所周知,python是一门面向对象的编程语言,python中一切皆对象,那么我们先探讨一下什么是对象. 一.对象 在生活中一个事物就是一个对象,比如:一只猫就是一个对象,猫的体型.猫毛的颜色等是它的 ...
- Unity shader 代码高亮+提示
Shader Unity Support This is Unity CG Shaders Support. It has code completion support and uses C/C++ ...
- photoshop快捷键汇总
图层应用相关快捷键: 复制图层:Ctrl+j 盖印图层:Ctrl+Alt+Shift+E 向下合并图层:Ctrl+E 合并可见图层:Ctrl+Shift+E 激活上一图层:Alt+中括号(]) 激活下 ...
- 【cdoj 1544】当咸鱼也要按照基本法
[题目链接]:http://acm.uestc.edu.cn/#/problem/show/1544 [题意] [题解] 容斥原理题; 1..(2^m)-1枚举 设k为其二进制形式中1的个数; k为奇 ...
- 洛谷——P1966 火柴排队
https://www.luogu.org/problem/show?pid=1966 题目描述 涵涵有两盒火柴,每盒装有 n 根火柴,每根火柴都有一个高度. 现在将每盒中的火柴各自排成一列, 同一列 ...
- [Recompose] Handle React Events as Streams with RxJS and Recompose
Events are the beginning of most every stream. Recompose provides a createEventHandler function to h ...
- 在Qt 4.4中,Alien Widget诞生了(Window负责与窗口系统的联系。Alien被号称是所有闪烁的终结者)
2011年09月29日 23:47:46 阅读数:7269 Qt 4.0 automatically double-buffers Qt 4.1 QWidget::autoFillBackground ...
- 识别CentOS和Ubuntu的系统版本
识别CentOS和Ubuntu的系统版本1.用 lsb-release#!/bin/bashInstall_LSB(){ if [ "$PM" = "yum ...