Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
又改了改 发现我的bug太多了。。如果一个环有分支 我的就不对了,改用优先队列做的In Reply To:给后来人一组数据 Posted by:qq490456661 at 2014-07-28 09:34:48 #include <stdio.h> #include <string.h> #include <algorithm> #include <queue> using namespace std; struct node1 { int a,b,cost; friend bool operator<(node1 x,node1 y ) { return x.cost<y.cost; } }; priority_queue<node1>s; struct node { int a,b,cost; }c[10005]; int fa[105],tree[105][105],vis[10005],vis_tree[105]; int n,m,max1,flag1; bool cmp1(node x,node y) { if(x.cost<y.cost) return true; else return false; } int find(int x) { if(fa[x]!=x) fa[x]=find(fa[x]); return fa[x]; } void sec_tree(int a,int b) { node1 temp; vis_tree[a]=1; if(a==b) { flag1=1; } for(int i=1;i<=n;i++) if(tree[a][i]&&!vis_tree[i]) { temp.a=a,temp.b=i,temp.cost=tree[a][i]; s.push(temp); if(!flag1) s.pop(),vis_tree[i]=0,sec_tree(i,b); } } int main() { int ncase; scanf("%d",&ncase); while(ncase--) { memset(vis,0,sizeof(vis)); memset(tree,0,sizeof(tree)); memset(&c,0,sizeof(&c)); scanf("%d %d",&n,&m); for(int i=1;i<=n;i++) fa[i]=i; for(int i=0;i<m;i++) scanf("%d %d %d",&c[i].a,&c[i].b,&c[i].cost); sort(c,c+m,cmp1); int sum=0; for(int i=0;i<m;i++) { int x=find(c[i].a); int y=find(c[i].b); if(x!=y) { fa[x]=y,sum+=c[i].cost; tree[x][y]=tree[y][x]=c[i].cost; vis[i]=1; } } int flag=0; for(int i=0;i<m;i++) { if(!vis[i]) { int flag1=0; while(!s.empty()) s.pop(); memset(vis_tree,0,sizeof(vis_tree)); sec_tree(c[i].a,c[i].b); node1 temp; temp=s.top(); if(temp.cost==c[i].cost) { flag=1; break; } } } if(!flag) printf("%d\n",sum); else printf("Not Unique!\n"); } } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator