Caocao‘s Bridges,最小割边,tarjan
Caocao's Bridges
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 15762 Accepted Submission(s): 4325
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.
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
解析 :
求权值最小的桥
这道题有很多特判:
当ans==0时我们也要至少派出一名士兵
当连通分量大于1时,我们不需要派出士兵
#include<iostream>
#include<string>
#include<cstring>
#include<cmath>
#include<ctime>
#include<algorithm>
#include<utility>
#include<stack>
#include<queue>
#include<vector>
#include<set>
#include<math.h>
#include<map>
using namespace std;
typedef long long LL;
const int N = 1e3 + 5, M = 1e6 + 5;
int n, m;
int h[N], e[M], w[M], ne[M],idx;
int dfn[N], low[N],timestamp;
vector<int>bridge;
void add(int a, int b,int c) {
e[idx] = b, w[idx] = c, ne[idx] = h[a], h[a] = idx++;
}
void tarjan(int u,int fa) {
dfn[u] = low[u] = ++timestamp;
for (int i = h[u]; i != -1; i = ne[i]) {
int j = e[i];
if (!dfn[j]) {
tarjan(j,i);
low[u] = min(low[u], low[j]);
if (dfn[u] < low[j]) {
bridge.push_back(i);
}
}
else if(i!=(fa^1)) {
low[u] = min(low[u], dfn[j]);
}
}
}
int main() {
while (scanf("%d%d", &n, &m) != EOF) {
if (n == 0 && m == 0)break;
memset(h, -1, sizeof h);
memset(dfn, 0, sizeof dfn);
bridge.clear();
idx = timestamp = 0;
for (int i = 1,a,b,c; i <= m; i++) {
scanf("%d%d%d", &a, &b,&c);
add(a, b,c), add(b, a,c);
}
int cnt = 0;
for (int i = 1; i <= n; i++) {
if (!dfn[i]) {
tarjan(i, -1);
cnt++;
}
}
if (cnt > 1) {
cout << 0 << endl;
continue;
}
if (bridge.size() == 0) {
cout << -1 << endl;
continue;
}
int ans = 1e6;
for (int i = 0; i < bridge.size(); i++) {
ans = min(w[bridge[i]], ans);
}
if (ans == 0)cout << 1 << endl;
else cout << ans << endl;
}
return 0;
}
分类:
图论 / # tarjan算法
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
· .NET周刊【3月第1期 2025-03-02】