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

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
 
Recommend
liuyiding
 
 
下面的代码是使用并查集判环,实际上只需要在每次进行tarjan算法时给计数器加一,就知道有几个连通块了,在哪加我标出来了
 /*************************************************************************
> 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(图中权值最小的桥)的更多相关文章

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

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

  2. 2013杭州网赛 1001 hdu 4738 Caocao's Bridges(双连通分量割边/桥)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4738 题意:有n座岛和m条桥,每条桥上有w个兵守着,现在要派不少于守桥的士兵数的人去炸桥,只能炸一条桥 ...

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

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

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

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

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

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

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

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

  7. hdu Caocao's Bridges(无向图边双连通分量,找出权值最小的桥)

    /* 题意:给出一个无向图,去掉一条权值最小边,使这个无向图不再连同! tm太坑了... 1,如果这个无向图开始就是一个非连通图,直接输出0 2,重边(两个节点存在多条边, 权值不一样) 3,如果找到 ...

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

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

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

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

随机推荐

  1. mysql——非主键自增

    今天遇到一个问题: 要创建一张表,其中我想将ip和date这两列作为一个复合主键,可以通过如下语句修改表结构: alter table tb_name add primary key (字段1,字段2 ...

  2. 在Windows 10 操作系统打开Windows Mobile 设备中心,要么双击无反应,要么正在启动后过会就关闭了

    在Windows 10 操作系统打开Windows Mobile 设备中心,要么双击无反应,要么正在启动后过会就关闭了 解决方法: 1.运行:输入services.msc进入服务 2.找到(前提你的P ...

  3. 【leetcode】1179. Reformat Department Table

    题目如下: SQL Schema Table: Department +---------------+---------+ | Column Name | Type | +------------- ...

  4. TypeScript作为前端开发你必须学习的技能二)

    TypeScript 变量声明 变量是一种使用方便的占位符,用于引用计算机内存地址.我们可以把变量看做存储数据的容器. TypeScript 变量的命名规则:和javascript一样.除了下划线 _ ...

  5. LeetCode - 乘积最大子串

    给定一个整数数组 nums ,找出一个序列中乘积最大的连续子串(该序列至少包含一个数). 示例 1: 输入: [2,3,-2,4] 输出: 6 解释: 子数组 [2,3] 有最大乘积 6. 示例 2: ...

  6. 随机数:rand()

    首先我们要对rand&srand有个总体的看法:srand初始化随机种子,rand产生随机数,下面将详细说明.   rand(产生随机数)   表头文件: #include<stdlib ...

  7. Windows10 + VS2015 (Win SDK10)环境下的64位 VTK编译小结

    之前在学习vtk过程中,感觉vtk的编译还是很简单的,基本上不会碰到什么棘手的错误.但是,当我在Win10+VS2015这个环境下配置时,却遇到了麻烦.经过一番折腾之后,终于将vtkbian编译成功了 ...

  8. sqli-labs(20)

    0X01 试探一下 这是登录成功的页面 这里题目高速我们是基于cookie的注入 0X01抓包试探 这里登陆的时候有两个包 我们要含有cookie的那个包 0X02试探判断是否cookie存在注入 C ...

  9. 聊聊spring-boot-starter-data-redis的配置变更

    本文主要研究一下spring-boot-starter-data-redis的配置变更 配置变更 以前是spring-boot的1.4.x版本的(spring-data-redis为1.7.x版本), ...

  10. [LeetCode]-DataBase-Combine Two Tables

    Table: Person +-------------+---------+ | Column Name | Type | +-------------+---------+ | PersonId ...