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<stdlib.h> //#include<windows.h> typedef struct{ long s; int f1,f2; }Farm; int cmp(const void *a,const void *b) { Farm *c=(Farm *)a; Farm *d=(Farm *)b; return c->s-d->s; } main() { int i,j,n,num,k,p[111]={0},m1,m2; long a[110][110],total; Farm farm[12100]; while(scanf("%d",&n)!=EOF) { num=0; k=0; total=0; memset(farm, 0, sizeof(Farm)); for(i=0;i<n;i++) for(j=0;j<n;j++) scanf("%ld",&a[i][j]); for(i=0;i<n;i++) for(j=i+1;j<n;j++) { farm[k].s=a[i][j]; farm[k].f1=i; farm[k].f2=j; k++; } qsort(farm,(n*(n-1)/2),sizeof(Farm),cmp); for(i=0;i<111;i++) p[i]=0; for(i=0;i<n*(n-1)/2&&num<=n-1;i++) { m1=farm[i].f1; m2=farm[i].f2; if(p[m1]<2&&p[m2]<2&&(p[m1]<1||p[m2]<1)) { total+=farm[i].s; p[m1]++; p[m2]++; num++; // printf("m1=%d m2=%d i=%d j=%d total=%ld\n",p[m1],p[m2],m1,m2,total); } } printf("%ld\n",total); } //Sleep(60000); } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator