标签:ble connect present init out bre return output ini
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 28673 | Accepted: 10239 |
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
// // main.cpp // poj1679 // // Created by Candy on 10/11/2016. // Copyright © 2016 Candy. All rights reserved. // #include <iostream> #include <cstdio> #include <algorithm> #include <cstring> using namespace std; const int N=105,M=10005,INF=1e9; inline int read(){ char c=getchar();int x=0,f=1; while(c<‘0‘||c>‘9‘){if(c==‘-‘)f=-1;c=getchar();} while(c>=‘0‘&&c<=‘9‘){x=x*10+c-‘0‘;c=getchar();} return x*f; } int n,m,u,v,w; struct edge{ int v,w,ne; }e[N<<1]; int h[N],cnt=0; inline void ins(int u,int v,int w){ cnt++; e[cnt].v=v;e[cnt].w=w;e[cnt].ne=h[u];h[u]=cnt; cnt++; e[cnt].v=u;e[cnt].w=w;e[cnt].ne=h[v];h[v]=cnt; } struct data{ int u,v,w; bool operator <(const data &r)const{return w<r.w;} }a[M]; int fa[N]; inline int find(int x){return fa[x]==x?x:fa[x]=find(fa[x]);} int use[N]; int kruskal(){ sort(a+1,a+1+m); int cnt=0,ans=0; for(int i=1;i<=m;i++){ int u=a[i].u,v=a[i].v,w=a[i].w; int f1=find(u),f2=find(v); if(f1==f2) continue; fa[f1]=f2; ins(u,v,w); use[i]=1; cnt++; ans+=w; if(cnt==n-1) break; } return ans; } int f[N][N],vis[N]; void dfs(int u){ vis[u]=1; for(int i=h[u];i;i=e[i].ne){ int v=e[i].v,w=e[i].w; if(vis[v]) continue; for(int x=1;x<=n;x++) if(vis[x]) f[x][v]=f[v][x]=max(f[x][u],w); dfs(v); } } void sol(){ cnt=0;memset(h,0,sizeof(h)); memset(f,0,sizeof(f)); memset(vis,0,sizeof(vis)); memset(use,0,sizeof(use)); for(int i=1;i<=n;i++) fa[i]=i; int ans=kruskal(); dfs(1); int mn=INF; for(int i=1;i<=m;i++) if(!use[i]){ int u=a[i].u,v=a[i].v,w=a[i].w;//printf("hi %d %d %d %d\n",u,v,w,f[u][v]); mn=min(mn,w-f[u][v]); } if(mn==0) puts("Not Unique!"); else printf("%d\n",ans); } int main(int argc, const char * argv[]){ int T=read(); while(T--){ n=read();m=read(); for(int i=1;i<=m;i++){a[i].u=read();a[i].v=read();a[i].w=read();} sol(); } return 0; }
标签:ble connect present init out bre return output ini
原文地址:http://www.cnblogs.com/candy99/p/6053789.html