commit 27e6f7ca19c33ffd31a8a810a6114374fe6eb6b4 Author: Lrixin <2565381053@qq.com> Date: Thu Aug 6 16:15:39 2020 +0800 wrote a cpp file diff --git a/Hello.cpp b/Hello.cpp new file mode 100644 index 0000000..c67181e --- /dev/null +++ b/Hello.cpp @@ -0,0 +1,99 @@ +#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