mymain
Lrixin 4 years ago
parent 27e6f7ca19
commit 7985fdd9c9

@ -1,99 +1,8 @@
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<cstdio>
using namespace std;
const int INF = 1e9;
const int maxn = 2e2 + 5;
struct edge{
int u, v, w;
edge() :u(),v(),w(){}
edge(int a,int b,int c) :u(a),v(b),w(c){}
bool operator<(const edge& op) const{
return w < op.w;
}
} e[maxn*maxn];
int f[maxn], sum = 0;
int used[maxn*maxn];
void add_edge(int u,int v,int w){
e[sum]=edge(u,v,w);
sum++;
}
int findroot(int x){
return (f[x] == x )? x : (f[x]= findroot(f[x]));
}
void init(int n){
for (int i = 0; i <= n;i++){
f[i] = i;
}
sum = 0;
memset(used, 0, sizeof(used));
}
int kruskal(int n,int m,int op){
for (int i = 0; i <= n;i++){
f[i] = i;
}
int ans=0,cnt=0;
for (int i = 0; i < m;i++){
if(i==op){
continue;
}
int fx=findroot(e[i].u);
int fy = findroot(e[i].v);
if(fx!=fy){
f[fy] = fx;
ans += e[i].w;
cnt++;
if(op==-1){
used[cnt - 1] = i;
}
if(cnt==n-1){
return ans;
}
}
}
return (cnt < n - 1) ? INF : (ans);
}
int main(){
int t;
cin >> t;
int cas = 1;
while (t--)
{
int n, m;
cin>>n>>m;
init(n);
int u, v, w;
int tt = m;
while (tt--)
{
cin >> u >> v >> w;
add_edge(u, v, w);
}
sort(e, e + m);
int mst = kruskal(n,m,-1);
int smst = INF;
for (int i = 0; i < m;i++){
smst = min(smst, kruskal(n, m, used[i]));
}
printf("Case #%d : ", cas++);
if(mst==INF){
printf("No way\n");
}
else if(smst==INF){
printf("No second way\n");
}
else{
printf("%d\n", smst);
}
}
return 0;
}
Loading…
Cancel
Save