Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 22346 | Accepted: 7924 |
Description
Input
Output
Sample Input
2 3 3 1 2 1 2 3 2 3 1 3 4 4 1 2 2 2 3 2 3 4 2 4 1 2
Sample Output
3 Not Unique!
Source
#include<map> #include<string> #include<cstring> #include<cstdio> #include<cstdlib> #include<cmath> #include<queue> #include<vector> #include<iostream> #include<algorithm> #include<bitset> #include<climits> #include<list> #include<iomanip> #include<stack> #include<set> using namespace std; bool used[110],fb[110][110]; int pre[110],dis[110],mx[110][110],cost[110][110]; int work(int n) { int ans=0; for(int i=1;i<=n;i++) { dis[i]=cost[1][i]; pre[i]=1; } memset(fb,0,sizeof(fb)); memset(used,0,sizeof(used)); used[1]=1; int N=n; while(--N) { int from,mn=INT_MAX; for(int i=1;i<=n;i++) if(!used[i]&&mn>dis[i]) { from=i; mn=dis[i]; } ans+=mn; used[from]=1; fb[from][pre[from]]=fb[pre[from]][from]=1; for(int i=1;i<=n;i++) if(used[i]) mx[i][from]=mx[from][i]=max(mx[i][pre[from]],dis[from]); else if(!used[i]&&dis[i]>cost[from][i]) { dis[i]=cost[from][i]; pre[i]=from; } } return ans; } int ok(int n) { int ans=work(n); int mn=INT_MAX; for(int i=1;i<=n;i++) for(int j=i+1;j<=n;j++) if(!fb[i][j]&&cost[i][j]!=INT_MAX) mn=min(mn,ans-mx[i][j]+cost[i][j]); return mn==ans?-1:ans; } int main() { int T; cin>>T; while(T--) { int n,m; cin>>n>>m; for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) cost[i][j]=INT_MAX; while(m--) { int s,e,w; cin>>s>>e>>w; cost[s][e]=cost[e][s]=w; } int ans=ok(n); if(ans==-1) cout<<"Not Unique!"<<endl; else cout<<ans<<endl; } }
poj1679The Unique MST判断最小生成树是否唯一以及求次小生成树边权和的讲解
原文:http://blog.csdn.net/stl112514/article/details/45133225