Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

感觉没有错啦,怎么就搞不出来呢?

Posted by zjw023 at 2012-03-15 22:24:40 on Problem 1258
#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:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator