Caocao's Bridges

Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

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 <= N 2 )

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
 
题目大意:曹操占领了n个岛,同时修建了m条大桥,每个桥上有wi个士兵守卫。周瑜要炸掉一个大桥,让这n个岛不再成为连通的的,如果要炸掉某个大桥,则必须最少派遣人数应大于等于该大桥上的守卫个数。问你需要最少派去多少人。
 
解题思路:很明显的是无向图求割边/桥,更准确得说要求的是割边的边权最小。模板题难度。但是这个题目比较坑:1.如果这n个岛本身就是不连通的  2.如果某条大桥上的士兵为0,但是仍需要派1人去炸毁
 
#include<stdio.h>
#include<algorithm>
#include<string.h>
#include<queue>
#include<vector>
using namespace std;
const int maxn = 1010;
const int INF = 0x3f3f3f3f;
struct Edge{
int from,to,dist,next;
Edge(){}
Edge(int _to,int _next,int _dist):to(_to),next(_next),dist(_dist){}
}edges[maxn*maxn*2];
int tot , head[maxn];
int dfn[maxn], bridge[maxn], dfs_clock, low[maxn], brinum;
int Mins;
void init(){
tot = 0;
dfs_clock = 0;
brinum = 0;
Mins = INF;
memset(head,-1,sizeof(head));
memset(dfn,0,sizeof(dfn));
memset(bridge,0,sizeof(bridge));
}
void AddEdge(int _u,int _v,int _w){
edges[tot] = Edge(_v,head[_u],_w);
head[_u] = tot++;
}
int dfs(int u,int fa){
int lowu = dfn[u] = ++dfs_clock;
for(int i = head[u]; i != -1; i = edges[i].next){
int v = edges[i].to;
if(!dfn[v]){
int lowv = dfs(v,i);
lowu = min(lowu,lowv);
if(lowv > dfn[u]){
bridge[v] = 1;
brinum++;
Mins = min(Mins , edges[i].dist);
}
}else if(dfn[v] < dfn[u] && (fa^1) != i){
lowu = min(dfn[v],lowu);
}
}
low[u] = lowu;
return lowu;
}
int main(){
int n,m;
while(scanf("%d%d",&n,&m)!=EOF&&(n+m)){
int a,b,c;
init();
for(int i = 0; i <m ;i++){
scanf("%d%d%d",&a,&b,&c);
AddEdge(a,b,c);
AddEdge(b,a,c);
}
int times = 0;
for(int i = 1; i <= n; i++){
if(!dfn[i]){
times++;
dfs(i,-1);
}
}
if(times > 1){
puts("0");
continue;
}
if(brinum == 0){
puts("-1");
continue;
}
printf("%d\n",Mins == 0? 1:Mins);
}
return 0;
}

  

 
 
 

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】

    <题目链接> 题目大意: 曹操在长江上建立了一些点,点之间有一些边连着.如果这些点构成的无向图变成了连通图,那么曹操就无敌了.周瑜为了防止曹操变得无敌,就打算去摧毁连接曹操的点的桥.但是诸 ...

  3. HDU 4738 Caocao's Bridges ——(找桥,求联通块)

    题意:给你一个无向图,给你一个炸弹去炸掉一条边,使得整个图不再联通,你需要派人去安置炸弹,且派去的人至少要比这条边上的人多.问至少要派去多少个,如果没法完成,就输出-1. 分析:如果这个图是已经是多个 ...

  4. HDU 4738 Caocao's Bridges(割边)

    乍一看一个模板题,仔细一看还是模板题,但是三个坑.1,不是连通图,放0个.2 守卫为0,放1个. 3注意重边. #include<iostream> #include<cstdio& ...

  5. hdu 4738 Caocao's Bridges(割边)

    题目链接 用tarjan求桥上的最小权值 #include<bits/stdc++.h> #define ll long long int using namespace std; inl ...

  6. hdu 4738 Caocao's Bridges(2013杭州网络赛丶神坑)

    就是求最小权值的桥..不过有好几个坑... 1:原图不连通,ans=0. 2: m<=n^2 显然有重边,重边必然不是桥,处理重边直接add(u, v, INF). 3:   最小桥边权为0的时 ...

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

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

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

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

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

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

随机推荐

  1. 正经学C#_位移与其位移运算符[c#入门经典]

    在c#入门经典一书中,最为糟糕的一节就是位移了,完全没有讲明白,也没有说全,似乎只是轻轻点了一下何为位移,带了两次原码和补码,完全不理会是否明白不明白.这一点这本书很差.因为此书说了,在大多数应用开发 ...

  2. [SinGuLaRiTy] 2017-07-26 综合性测试

    [SinGuLaRiTy-1032] Copyright (c) SinGuLaRiTy 2017. All Rights Reserved.               单词 (word) 题目描述 ...

  3. web安全-点击劫持

    web安全-点击劫持 opacity=0 iframe是目标网站 被内嵌了 1.用户亲手操作 盗取用户 视频 2.用户不知情 >* 引导点击 其实点击的是覆盖在下面opacity=0的ifram ...

  4. JavaScript——原生js实现瀑布流

    瀑布流介绍及实现原理: 瀑布流是一种页面布局,页面上也有多等宽的块(块就页面内容),每一块都是绝对定位(absolute),每个块排列的方式如下:寻找现在高度最小的列,把该块定位到该列下方.需要知道, ...

  5. 平衡树学习笔记(2)-------Treap

    Treap 上一篇:平衡树学习笔记(1)-------简介 Treap是一个玄学的平衡树 为什么说它玄学呢? 还记得上一节说过每个平衡树都有自己的平衡方式吗? 没错,它平衡的方式是......rand ...

  6. freemarker常用标签解释

    标签一: if else 你可以使用if,elseif和else指令来条件判断是否越过模板的一个部分.这些condition-s必须计算成布尔值,否则错误将会中止模板处理.elseif-s和else- ...

  7. 类关系/self/特殊成员

    1.依赖关系 在方法中引入另一个类的对象 2.关联关系.聚合关系.组合关系 #废话少说 直接上代码===>选课系统 # coding:utf-8 class Student(object): d ...

  8. Kibana6.x.x源码分析--启动时basePath

    每次启动Kibana时,发现URL都会随机的生成三位字母,比如:[http://localhost:5601/abc/][http://localhost:5601/dzf/]等等,那么这些随机的字符 ...

  9. Android + Appium 自动化测试完整的环境配置及代码详解

    环境的的搭建 参考大神博客:https://www.cnblogs.com/fnng/p/4540731.html 该博客有一套详细的入门教程,奈何时间有点久远有些东西不能用了,但是参考价值还是有滴. ...

  10. 简单的Web日志分析脚本

    前言 长话短说,事情的起因是这样的,由于工作原因需要分析网站日志,服务器是windows,iis日志,在网上找了找,github找了找,居然没找到,看来只有自己动手丰衣足食. 那么分析方法我大致可分为 ...