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 |
水过就是赤裸裸的模版题啊 #include<stdio.h> #include<string.h> #include<math.h> #include<string> #include<algorithm> using namespace std; #define ll long long #define inf 0xfffff #define exp 1e-9 int p[510],n,m,s,flag; int lenth,ans; struct e { int a,b; int c; }path[25000]; int cmp(e a,e b) { return a.c<b.c; } int find(int k) { while(p[k]!=k) { k=p[k]; } return k; } void kruskal(int a,int b,int c) { int f1=find(a); int f2=find(b); if(f1!=f2) { ans+=c; p[f1]=f2; } } void inti() { for(int i=1;i<=n;i++) p[i]=i; } int main() { while(scanf("%d",&n)!=EOF) { memset(path,0,sizeof(path)); int num=1,te; inti(); ans=0; for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) { scanf("%d",&te); if(i<j){ path[num].a=i; path[num].b=j; path[num].c=te; // printf("%d %d %d\n",i,j,te); num++;} } sort(path+1,path+num+1,cmp); for(int i=1;i<=num;i++) kruskal(path[i].a,path[i].b,path[i].c); printf("%d\n",ans); } } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator