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 <iostream> #include <cstdio> #include <cstring> using namespace std; #define INF 0x3f3f3f3f #define N 110 int n,m; int closest[N]; int lowcost[N]; int mpt[N][N]; int mxd[N][N]; bool vis[N]; bool connect[N][N]; int ans1,ans2; void prim(int s) { int fk,k,i,j,minc; for(i=1;i<=n;i++) { closest[i]=s; lowcost[i]=mpt[s][i]; } vis[s]=1; for(i=1;i<n;i++) { minc=INF; for(j=1;j<=n;j++) { if(!vis[j] && lowcost[j]<minc) { k=j; minc=lowcost[j]; } } if(minc==INF) break; vis[k]=1; ans1+=minc; fk=closest[k]; connect[fk][k]=1; connect[k][fk]=1; for(j=1;j<=n;j++) { if(vis[j] && j!=k) mxd[k][j]=mxd[j][k]=max(mxd[j][fk],lowcost[k]); if(!vis[j] && mpt[k][j]<lowcost[j]) { lowcost[j]=mpt[k][j]; closest[j]=k; } } } } int main() { int T; scanf("%d",&T); while(T--) { scanf("%d%d",&n,&m); ans1=0; ans2=INF; memset(mxd,0,sizeof(mxd)); memset(mpt,INF,sizeof(mpt)); memset(vis,0,sizeof(vis)); memset(connect,0,sizeof(connect)); while(m--) { int a,b,c; scanf("%d%d%d",&a,&b,&c); mpt[a][b]=c; mpt[b][a]=c; } prim(1); for(int i=1;i<=n;i++) { for(int j=1;j<=n;j++) { if(!connect[i][j]) { ans2=min(ans2,ans1-mxd[i][j]+mpt[i][j]); } } } if(ans1!=ans2) printf("%d\n",ans1); else printf("Not Unique!\n"); } return 0; }