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

用scanf("%s%d"),printf可以过的

Posted by loreweiyao at 2012-02-02 01:16:32 on Problem 1251
#include <stdio.h>
#include <string.h>
#include <stdio.h>

int v1[80],v2[80],e[80],r[80],p[27];

int find(x)
{
    return (p[x] == x) ? x : (p[x] = find(p[x]));
}

int compare(const void *e1,const void *e2)
{
    int *p1 = (int *)e1;
    int *p2 = (int *)e2;
    return e[*p1] - e[*p2];
}

int kruskal(int vi,int ei)
{
    int i,x,y,tmp,ans;
    int count = 0;
    for(i = 0;i < vi;i++)
       p[i] = i;
    ans = 0;
    for(i = 0;i < ei;i++)
    {
        tmp = r[i];
        x = find(v1[tmp]);
        y = find(v2[tmp]);
        if(x != y)
        {
            p[x] = y;
            ans += e[tmp];
            count++;
        }
        if(count == vi - 1)
            break;
    }
    return ans;
}

int main()
{
   // FILE *fp;
    //fp = fopen("d:\\c.txt","r");
    int i,n,t,k,w;
    char cs[2],ce[2];
    scanf("%d",&n);
    //fscanf(fp,"%d",&n);
    while(n)
    {
        t = 0;
        for(i = 0;i < n-1;i++)
        {
            scanf("%s%d",cs,&k);
            //fscanf(fp,"%s%d",cs,&k);
            {
                while(k--)
                {
                   //scanf(fp,"%s%d",ce,&w);      
                   scanf("%s%d",ce,&w);
                   e[t] = w;
                   v1[t] = cs[0] - 'A';
                   v2[t] = ce[0] - 'A';
                   t++;
                }
            }
        }
        for(i = 0;i < t;i++)
             r[i] = i;
        qsort(r,t,sizeof(r[0]),compare);
        printf("%d\n",kruskal(n,t));
        scanf("%d",&n);
        //fscanf(fp,"%d",&n);
    }
    return 0;
}

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