Caocao's Bridges

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

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
 
#include <iostream>
#include <stdio.h>
#include <queue>
#include <stdio.h>
#include <string.h>
#include <vector>
#include <queue>
#include <set>
#include <algorithm>
#include <map>
#include <stack>
#include <math.h>
#define Max(a,b) ((a)>(b)?(a):(b))
#define Min(a,b) ((a)<(b)?(a):(b))
#pragma comment(linker, "/STACK:10240000000000,10240000000000")
using namespace std;
typedef long long LL ;
const int Max_N= ;
struct Edge{
int v ;
int w ;
int next ;
};
Edge edge[Max_N*Max_N] ;
int id ,indx ;
int vec[Max_N] ,dfn[Max_N] ,low[Max_N] ;
bool visited[Max_N];
int grid[Max_N][Max_N] ;
vector<int>brige ;
void add_edge(int u ,int v ,int w){
edge[id].v=v ;
edge[id].w=w ;
edge[id].next=vec[u] ;
vec[u]=id++ ;
} void init(){
id= ;
indx= ;
memset(vec,-,sizeof(vec)) ;
memset(visited,,sizeof(visited)) ;
memset(grid,,sizeof(grid)) ;
memset(dfn,,sizeof(dfn)) ;
brige.clear() ;
} void dfs(int u ,int father){
dfn[u]=low[u]=++indx ;
for(int e=vec[u];e!=-;e=edge[e].next){
int v=edge[e].v ;
int w=edge[e].w ;
if(!dfn[v]){
dfs(v,u) ;
low[u]=Min(low[u],low[v]) ;
if(low[v]>dfn[u]&&grid[u][v]==)
brige.push_back(w) ;
}
else if(v!=father)
low[u]=Min(low[u],dfn[v]) ;
}
} int Dot ;
void dfs(int u){
visited[u]= ;
Dot++ ;
for(int e=vec[u];e!=-;e=edge[e].next){
int v=edge[e].v ;
if(!visited[v])
dfs(v) ;
}
} int main(){
int N ,M ,u ,v ,w ;
while(scanf("%d%d",&N,&M)!=EOF){
if(N==&&M==)
break ;
init() ;
while(M--){
scanf("%d%d%d",&u,&v,&w) ;
add_edge(u,v,w) ;
add_edge(v,u,w) ;
grid[u][v]++ ;
grid[v][u]++ ;
}
Dot= ;
dfs() ;
if(Dot!=N){
puts("") ;
continue ;
}
for(int i=;i<=N ;i++){
if(!dfn[i])
dfs(i,-) ;
}
if(brige.size()==){
puts("-1") ;
continue ;
}
sort(brige.begin(),brige.end()) ;
printf("%d\n",brige[]==?:brige[]) ;
}
return ;
}

HDU 4738 Caocao's Bridges的更多相关文章

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

    题目链接: Hdu 4738 Caocao's Bridges 题目描述: 有n个岛屿,m个桥,问是否可以去掉一个花费最小的桥,使得岛屿边的不连通? 解题思路: 去掉一个边使得岛屿不连通,那么去掉的这 ...

  2. HDU 4738 Caocao's Bridges(Tarjan求桥+重边判断)

    Caocao's Bridges Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  3. HDU 4738——Caocao's Bridges——————【求割边/桥的最小权值】

     Caocao's Bridges Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u S ...

  4. hdu 4738 Caocao's Bridges 图--桥的判断模板

    Caocao's Bridges Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  5. HDU 4738 Caocao's Bridges (2013杭州网络赛1001题,连通图,求桥)

    Caocao's Bridges Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  6. HDU——4738 Caocao's Bridges

    Caocao's Bridges Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  7. hdu 4738 Caocao's Bridges(桥的最小权值+去重)

    http://acm.hdu.edu.cn/showproblem.php?pid=4738 题目大意:曹操有一些岛屿被桥连接,每座都有士兵把守,周瑜想把这些岛屿分成两部分,但他只能炸毁一条桥,问最少 ...

  8. hdu 4738 Caocao's Bridges (tarjan求桥)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4738 题目大意:给一些点,用一些边把这些点相连,每一条边上有一个权值.现在要你破坏任意一个边(要付出相 ...

  9. 【HDU 4738 Caocao's Bridges】BCC 找桥

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4738 题意:给定一个n个节点m条边的无向图(可能不连通.有重边),每条边有一个权值.判断其连通性,若双 ...

随机推荐

  1. php日期时间函数

    1,年-月-日echo date('Y-m-j');2007-02-6echo date('y-n-j');07-2-6大写Y表示年四位数字,而小写y表示年的两位数字:小写m表示月份的数字(带前导), ...

  2. 剑指offer系列53---字符串转化成整数

    [题目]将一个字符串转换成一个整数,要求不能使用字符串转换整数的库函数 * []整数(32位)最大值:0X7fff ffff( 0111 1111 1111 1111 1111 1111 1111 1 ...

  3. C#语法杂谈

    1. 值类型和引用类型 1.1 值类型 比如int,float,struct等,和C/C++中的变量差不多,但编译器会强制你必须先初始化再使用,避免一不小心使用了未初始化的变量. 1.2 引用类型 c ...

  4. find_in_set mysql

    有个文章表里面有个type字段,他存储的是文章类型,有 1头条,2推荐,3热点,4图文 .....11,12,13等等 现在有篇文章他既是 头条,又是热点,还是图文, type中以 1,3,4的格式存 ...

  5. android学习笔记28——Activity生命周期

    Activity生命周期 Activity的活动状态由android已Activity栈的形式管理,当前活动的Activity位于栈顶.随着不同应用的运行,每个Activity都有可能从活动状态转入非 ...

  6. 什么是SQLCLR与使用

    原帖地址:http://www.cnblogs.com/hsrzyn/archive/2013/05/28/1976555.html 什么是SQLCLR SQL CLR (SQL Common Lan ...

  7. 无法找到类:java.lang.ClassNotFoundException: com.mysql.jdbc.driver

    转载自:http://blog.csdn.net/huangbiao86/article/details/6428608 问题描述:连接数据库,而明明已经将mysql-connector-java-5 ...

  8. 嵌入式设计模式:有限状态自动机的C语言实现

    转自:http://www.cnblogs.com/autosar/archive/2012/06/22/2558604.html 状态机模式是一种行为模式,在<设计模式>这本书中对其有详 ...

  9. windows7使用Source insight上远程修改ubuntu共享内核源码

    由于本人阅读喜欢使用source insight.前段时间接触了linux核代码,而这份代码只能放在ubuntu服务器上编译,刚开始的时候是在windows上修改,完了之后再copy到服务器上去编译, ...

  10. 使用mongo-java-driver3.0.2.jar和mongodb3.0在java代码中的用户验证4

    以下是使用mongo-java-driver3.0.2.jar和mongodb3.0.4在java代码中的用户验证: ServerAddress sa = new ServerAddress(host ...