From 7985fdd9c9a92357bae7fa1b6ecf2f050ae1e561 Mon Sep 17 00:00:00 2001 From: Lrixin <2565381053@qq.com> Date: Thu, 6 Aug 2020 16:25:53 +0800 Subject: [PATCH] 2020/8/6 --- Hello.cpp | 93 +------------------------------------------------------ 1 file changed, 1 insertion(+), 92 deletions(-) diff --git a/Hello.cpp b/Hello.cpp index c67181e..b0e23de 100644 --- a/Hello.cpp +++ b/Hello.cpp @@ -1,99 +1,8 @@ #include #include -#include #include -#include 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; + } \ No newline at end of file