首页 技术 正文
技术 2022年11月19日
0 收藏 562 点赞 5,113 浏览 3722 个字

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 DescriptionCaocao 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. InputThere 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.

 OutputFor 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 Input3 3
1 2 7
2 3 4
3 1 4
3 2
1 2 7
2 3 4
0 0 Sample Output-1
4 Source2013 ACM/ICPC Asia Regional Hangzhou Online Recommendliuyiding  下面的代码是使用并查集判环,实际上只需要在每次进行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 ;
}
上一篇: 1000行基本SQL
下一篇: honpeyhonepy
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,983
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,500
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,344
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,127
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,761
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,838