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. Spark(四): Spark-sql 读hbase

    SparkSQL是指整合了Hive的spark-sql cli, 本质上就是通过Hive访问HBase表,具体就是通过hive-hbase-handler, 具体配置参见:Hive(五):hive与h ...

  2. ORACLE的表被 另一个用户锁定,如何解除..

    SELECT object_name, machine, s.sid, s.serial# FROM gv$locked_object l, dba_objects o, gv$session s W ...

  3. 在VS2008环境下的C++异常处理

    在写DAServer的过程中,一直在重视报文逻辑处理,却没有认认真真地去思考异常处理的问题.曾经我发现我在所有的报文处理函数中均没有考虑报文长度的问题,让我内心不由地捏了一把冷汗.我在新增的故障录波及 ...

  4. Debian 环境下安装Tomcat记录

    1.安装JAVA运行环境 Debian默认带了OpenJDK,有人说不好用,我没有验证就从ORACLE官网上下载了最新的JDK安装包,直接解压并设置环境变量就行了: # tar zxvf jdk-8u ...

  5. bzoj2537: [neerc2007]Language Recognition

    Description DFA(确定性有限状态自动机)是一个有向图,顶点称为状态,边称为转移.每个转移用一个字母标记.对于每个状态s和每个转移l,至多有一个转移从s出发且标记为l.DFA有一个初始状态 ...

  6. Python 派生类子类继承类

    1.创建list类的子类Namedlist,初始化新类,创建新对象实例johnny,检查对象类型,并使用list的一些功能来存储数据 >>> class Namedlist(list ...

  7. 求最长回文子串——Manacher算法

    回文串包括奇数长的和偶数长的,一般求的时候都要分情况讨论,这个算法做了个简单的处理把奇偶情况统一了.算法的基本思路是这样的,把原串每个字符中间用一个串中没出现过的字符分隔开来(统一奇偶),用一个数组p ...

  8. Java事务处理全解析(一)——Java事务处理的基本问题

    Java中的事务处理有多简单?在使用EJB时,事务在我们几乎察觉不到的情况下发挥着作用:而在使用Spring时,也只需要配置一个TransactionManager,然后在需要事务的方法上加上Tran ...

  9. iphone dev 入门实例7:How to Add Splash Screen in Your iOS App

    http://www.appcoda.com/how-to-add-splash-screen-in-your-ios-app/ What’s Splash Screen? For those who ...

  10. 在指定路径下查找并打印mdb类型文件

    1 #encoding:utf8 import os fpath = 'D:\Download\LP传奇-麒麟传说\Date' rfile = '' files = [] mdbFiles = [] ...