hdu-4738.Caocao's Bridges(图中权值最小的桥)
Caocao's Bridges
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 10933 Accepted Submission(s): 3065
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
/*************************************************************************
> File Name: hdu-4738.caocaos_bridges.cpp
> Author: CruelKing
> Mail: 2016586625@qq.com
> Created Time: 2019年09月07日 星期六 21时41分41秒
本题思路:无向图所有桥中权值的那条桥的权值.
注意:有重边,如果桥上没敌人,需要有人抗tnt,因此需要输出1.
如果初始图不连通则输出0.
************************************************************************/ #include <cstdio>
#include <cstring>
#include <map>
using namespace std; const int maxn = + , maxm = maxn * maxn + , inf = 0x3f3f3f3f;
int n, m;
int tot, head[maxn]; int bridge, top, Index, min_bridge;
int dfn[maxn], low[maxn], stack[maxn];
bool instack[maxn]; map<int, int> mp; struct Edge {
int to, cost, next;
bool cut;
} edge[maxm << ]; int min(int x, int y) {
return x > y ? y : x;
} void init() {
mp.clear();
memset(head, -, sizeof head);
tot = ;
} void addedge(int u, int v ,int w) {
edge[tot] = (Edge){v, w, head[u], false}; head[u] = tot ++;
edge[tot] = (Edge){u, w, head[v], false}; head[v] = tot ++;
} bool ishash(int u, int v) {
return mp[u * maxn + v] ++ || mp[v * maxn + u] ++;
} void tarjan(int u, int pre) {
int v;
stack[top ++] = u;
instack[u] = true;
dfn[u] = low[u] = ++ Index;
int pre_cnt = ;
for(int i = head[u]; ~i; i = edge[i].next) {
v = edge[i].to;
if(v == pre && pre_cnt == ) {
pre_cnt ++;
continue;
}
if(!dfn[v]) {
tarjan(v, u);
if(low[u] > low[v]) low[u] = low[v];
if(low[v] > dfn[u]) {
edge[i].cut = true;
edge[i ^ ].cut = true;
min_bridge = min(min_bridge, edge[i].cost);
bridge ++;
}
} else if(low[u] > dfn[v]) low[u] = dfn[v];
}
top --;
instack[u] = false;
} void solve() {
memset(instack, false, sizeof instack);
memset(dfn, , sizeof dfn);
memset(low, , sizeof low);
top = Index = bridge = ;
min_bridge = inf;
for(int i = ; i <= n; i ++) {
if(!dfn[i]) {
tarjan(i, i);//cnt ++;
}
}
if(min_bridge == inf) min_bridge = -;
else if(min_bridge == ) min_bridge = ;//if cnt != 1 : min_bridge = 0;
printf("%d\n", min_bridge);
} int fa[maxn]; int find(int x) {
if(fa[x] != x) return fa[x] = find(fa[x]);
else return x;
} void unionset(int u, int v) {
u = find(u);
v = find(v);
if(u != v) fa[u] = v;
} int main() {
int u, v, w;
while(~scanf("%d %d", &n, &m) && (n || m)) {
init();
for(int i = ; i <= n; i ++) fa[i] = i;
for(int i = ; i < m; i ++) {
scanf("%d %d %d", &u, &v, &w);
// if(ishash(u, v)) continue;
addedge(u, v, w);
unionset(u, v);
}
bool flag = true;
for(int i = ; i <= n; i ++)
if(find(i) != find()) {
flag = false;
break;
}
if(flag)
solve();
else printf("0\n");
}
return ;
}
hdu-4738.Caocao's Bridges(图中权值最小的桥)的更多相关文章
- hdu 4738 Caocao's Bridges 图--桥的判断模板
Caocao's Bridges Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- 2013杭州网赛 1001 hdu 4738 Caocao's Bridges(双连通分量割边/桥)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4738 题意:有n座岛和m条桥,每条桥上有w个兵守着,现在要派不少于守桥的士兵数的人去炸桥,只能炸一条桥 ...
- hdu 4738 Caocao's Bridges (tarjan求桥)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4738 题目大意:给一些点,用一些边把这些点相连,每一条边上有一个权值.现在要你破坏任意一个边(要付出相 ...
- 【HDU 4738 Caocao's Bridges】BCC 找桥
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4738 题意:给定一个n个节点m条边的无向图(可能不连通.有重边),每条边有一个权值.判断其连通性,若双 ...
- Hdu 4738 Caocao's Bridges (连通图+桥)
题目链接: Hdu 4738 Caocao's Bridges 题目描述: 有n个岛屿,m个桥,问是否可以去掉一个花费最小的桥,使得岛屿边的不连通? 解题思路: 去掉一个边使得岛屿不连通,那么去掉的这 ...
- hdu 4738 Caocao's Bridges 求无向图的桥【Tarjan】
<题目链接> 题目大意: 曹操在长江上建立了一些点,点之间有一些边连着.如果这些点构成的无向图变成了连通图,那么曹操就无敌了.周瑜为了防止曹操变得无敌,就打算去摧毁连接曹操的点的桥.但是诸 ...
- hdu Caocao's Bridges(无向图边双连通分量,找出权值最小的桥)
/* 题意:给出一个无向图,去掉一条权值最小边,使这个无向图不再连同! tm太坑了... 1,如果这个无向图开始就是一个非连通图,直接输出0 2,重边(两个节点存在多条边, 权值不一样) 3,如果找到 ...
- 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(Tarjan求桥+重边判断)
Caocao's Bridges Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
随机推荐
- CSS3基础——笔记+实战案例(CSS基本用法、CSS层叠性、CSS继承性)
CSS3基础——笔记 CSS是Cascading Style Sheet的缩写,翻译为"层叠样式表" 或 "级联样式表".CSS定义如何显示HTML的标签央视, ...
- java——SimpleDateFormat与DateTimeFormatter
https://www.jianshu.com/p/b212afa16f1f SimpleDateFormat不是线程安全的 DateTimeFormatter是线程安全的
- [luogu]P1800 software_NOI导刊2010提高(06)[DP][二分答案]
[luogu]P1800 software_NOI导刊2010提高(06) 题目描述 一个软件开发公司同时要开发两个软件,并且要同时交付给用户,现在公司为了尽快完成这一任务,将每个软件划分成m个模块, ...
- 2019hdu多校 Fansblog
Problem Description Farmer John keeps a website called 'FansBlog' .Everyday , there are many people ...
- HTML 和 CSS 画三角形和画多边行基本原理及实践
基本 HTML 标签 <div class = 'test'></div> 基本 CSS 代码 .test { width: 100px; height: 100px; bac ...
- Selenium 元素常用操作方法(键盘和鼠标事件)
一.简单操作 click():点击 send_keys():输入 clear():清空 submit():提交表单 size:返回元素的尺寸 text:获取元素的文本 get_attribute(): ...
- sqli-labs(26a)
0x01偷偷看一波源码 和26关一样 闭合变成了’)而已 0X01构造语句爆库名 这是百度到的 第一个 ' 首先闭合id='$id' 中的',%a0是空格的意思,(ps:此处我的环境是ubuntu14 ...
- [LeetCode]-DataBase-Employees Earning More Than Their Managers
The Employee table holds all employees including their managers. Every employee has an Id, and there ...
- DjangoRestFrameWork 版本控制
DRF的版本控制 为什么需要版本控制 API 版本控制允许我们在不同的客户端之间更改行为(同一个接口的不同版本会返回不同的数据). DRF提供了许多不同的版本控制方案. 可能会有一些客户端因为某些原因 ...
- 【重点突破】—— UniApp 微信小程序开发官网学习One
一.初步认识 uni-app官网:https://uniapp.dcloud.io/component/README HBuilderX官方IDE下载地址: http://www.dcloud.io/ ...