标签:long name == turn def string sizeof return struct
#include <cstdio> #include <cstring> #include <iostream> #include <algorithm> using namespace std; typedef long long ll; const int N=510,M=10010; int n,m; struct Edge { int a,b,w; bool f; } edge[M]; int dist1[N][N], dist2[N][N]; int h[N], e[N*2],w[N*2],ne[N*2],idx; int p[N]; void add(int a,int b,int c) { e[idx]=b; w[idx]=c; ne[idx]=h[a]; h[a]=idx++; } int find(int x) { if(p[x]!=x) p[x]=find(p[x]); return p[x]; } bool cmp(Edge a,Edge b) { return a.w<b.w; } void dfs(int u,int fa,int maxd1,int maxd2,int d1[],int d2[]) { d1[u]=maxd1,d2[u]=maxd2; for(int i=h[u]; ~i; i=ne[i]) { int j=e[i]; if(j!=fa) { int td1=maxd1,td2=maxd2; if(w[i]>td1) td2=td1,td1=w[i]; else if(w[i]<td1&&w[i]>td2) td2=w[i]; dfs(j,u,td1,td2,d1,d2); } } } int main() { int t; cin>>t; while(t--) { cin>>n>>m; memset(h,-1,sizeof h); for(int i=0; i<m; i++) { int a,b,w; cin>>a>>b>>w; edge[i]= {a,b,w}; } sort(edge,edge+m,cmp); for(int i=1; i<=n; i++) p[i]=i; ll sum=0; for(int i=0; i<m; i++) { int a=edge[i].a; int b=edge[i].b; int w=edge[i].w; int pa=find(a); int pb=find(b); if(pa!=pb) { p[pa]=pb; sum+=w; add(a,b,w); add(b,a,w); edge[i].f=true; } } for(int i=1; i<=n; i++) dfs(i,-1,0,0,dist1[i],dist2[i]); ll res=1e18; for(int i=0; i<m; i++) if(!edge[i].f) { int a=edge[i].a; int b=edge[i].b; int w=edge[i].w; ll t; if(w>=dist1[a][b]) t=sum+w-dist1[a][b]; else if(w>dist2[a][b]) t=sum+w-dist2[a][b]; res=min(res,t); } if(res==sum) cout<<"Not Unique!"<<endl; else cout<<sum<<endl; } return 0; }
The Unique MST POJ - 1679 次小生成树
标签:long name == turn def string sizeof return struct
原文地址:https://www.cnblogs.com/QingyuYYYYY/p/12240410.html