HDU 4738 Caocao’s Bridges(桥、任何位运算一定都要加括号、因为有重边所以用前向星)

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.

题意:曹操有一个无向图,图中有一些边,周瑜现在要派一些人去炸一条边,使曹操的无向图分为不同部分,曹操的每条边上都有守卫,周瑜派的人不能少于边上守卫数,问最少要派多少人。

  • 注意重边处理, 要用链式前向星。
  • 守卫为0时,也需要派一个人;
  • 如果原本图不连通(tarjan多次),就不需要派人,答案就是0。
  • 答案直接输出所有桥中的最小边权。
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
using namespace std;
const int N = 500007, M = 5000007, INF = 0x3f3f3f3f;int n, m;
int bridge[N];
int dfn[N], low[N], num;
int head[N], ver[M], nex[M], edge[M], tot;
int ans;
int dcc_cnt;
int min_bridge;void init()
{num = tot = dcc_cnt = 0;memset(head, -1, sizeof head);memset(dfn, 0, sizeof dfn);memset(low, 0, sizeof low);min_bridge = INF;
}void add(int x, int y, int z)
{ver[tot] = y;edge[tot] = z;nex[tot] = head[x];head[x] = tot ++ ;
}void tarjan(int x, int in_edge)
{dfn[x] = low[x] = ++ num;for(int i = head[x] ;~i; i = nex[i]){int y = ver[i], z = edge[i];if(!dfn[y]){tarjan(y, i);low[x] = min(low[x], low[y]);if(dfn[x] < low[y]){bridge[i] = bridge[i ^ 1] = true;min_bridge = min(min_bridge, z);}}else if(i != (in_edge ^ 1))low[x] = min(low[x], dfn[y]);}
}int main()
{while(~scanf("%d%d", &n, &m) && n ){init();for(int i = 1; i <= m; ++ i){int x, y ,z;scanf("%d%d%d", &x, &y, &z);add(x, y, z), add(y, x, z);}dcc_cnt = 0;for(int i = 1; i <= n; ++ i)if(!dfn[i])tarjan(i, 0), dcc_cnt ++ ;if(dcc_cnt > 1){//tarjan多次说明不连通,直接输出0printf("0\n");continue;}if(min_bridge == INF)min_bridge = -1;else if(min_bridge == 0)min_bridge = 1;printf("%d\n", min_bridge);}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 求桥 诸葛亮带着炸弹跑路了

    Description Caocao was defeated by Zhuge Liang and Zhou Yu in the battle of Chibi. But he wouldn't g ...

  3. hdu 4738 Caocao's Bridges 求无向图的桥【Tarjan】

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

  4. HDU - 4738 Caocao's Bridges(边双缩点)

    题目链接:点击查看 题目大意:给出一个由n个点和m条边构成的无向图,表示n个岛屿之间的m条道路,现在周瑜有一个炸药,可以炸掉任意的一条道路,不过每条道路都有一个权值,代表这条道路上防守的卫兵数量,如果 ...

  5. HDU 2276 Kiki Little Kiki 2 (位运算+矩阵快速幂)

    HDU 2276 Kiki & Little Kiki 2 (位运算+矩阵快速幂) ACM 题目地址:HDU 2276 Kiki & Little Kiki 2 题意:  一排灯,开关 ...

  6. python写整数逆位运算_位运算

    a = 60 # 0011 1100 b = 13 # 0000 1101 运算符 描述 示例 & 按位与运算符:参与运算的两个值,如果两个相应位都为1,则该位的结果为1,否则为0 a& ...

  7. hdu 4738 无向图缩点断桥 // 细节坑题

    Caocao's Bridges 题意:给个无向图,求出边权最小的桥. 一看,直接缩点,若无桥,输出-1,有桥,遍历下边,更新最小..分分钟搞定,以为IA的..一交wa... 坑点:1:若原图不连通, ...

  8. hdu 3006 位运算

    我是看题解才知道思路的. 这题充分体现了位运算的神奇. 看题目(1<=m<=14)  是不是隐隐约约有什么感觉? 对的,就是位运算暴力求解. 对于每个集合 比如 1(01) 2(10)   ...

  9. hdu 1818 It's not a Bug, It's a Feature!(位运算+bfs优先队列)

    题意:给一个长度为n的bug,和m个补丁,然后是m个补丁的描述.第一个数字是这个补丁消耗的时间. 第1个字符串是这个补丁要工作需要满足的条件,第2个字符串是这个补丁的作用 详细一点说, 对于第一个字符 ...

最新文章

  1. Intel汇编语言程序设计学习-第六章 条件处理-中
  2. web前端url传递值 js加密解密
  3. 数据仓库之电商数仓-- 3.3、电商数据仓库系统(DWT层)
  4. 从统计代码来谈JS加载的优化
  5. 离线安装PostgreSQL
  6. maven配置阿里仓库
  7. matlab latex emf 乱码,latex 使用中的一些问题
  8. 王者荣耀android换ios,王者荣耀安卓转ios教程攻略
  9. 如何准备全国计算机二级Python,二级Python考试技巧
  10. 三星a5009Android6.0,三星A5009 6.0 root教程及获取6.0的root权限
  11. 计算机的中央处理器CPU包括什么,中央处理器cpu由哪些组成 中央处理器的作用是什么...
  12. IIC输出模式选择推挽输出还是开漏输出?
  13. ListView 仿QQ微信侧滑出现删除按钮
  14. [Maven进阶]多环境配置与应用
  15. jmeter断言操作详解
  16. 操作系统——linux
  17. 数据挖掘课笔记(七)
  18. 枚举(2) : No enum constant xxx
  19. Android 状态栏沉浸式效果
  20. 云测平台进行软件兼容性测试

热门文章

  1. 机器视觉工程师必须了解的基础知识
  2. 快速系统从零学习OpenCV 4路线图
  3. 链表问题20——按照左右半区的方式重新组合单链表
  4. C++字符串数组排序技巧
  5. EP936E的IIC
  6. Clever Answers in Codewar(Javascript 持续更新)
  7. 作业3.1:沟通管理计划包括哪些内容
  8. VisualStudio:WEB 性能测试和负载测试 入门
  9. mysql 客户端提示“Cannot proceed because system tabl...
  10. linux 学习笔记 (1) —— 安装 Redhat enterprise 5