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

Re:错误代码大公布.............

Posted by huangzhengdoc at 2015-08-22 08:51:32 on Problem 2075
In Reply To:错误代码大公布............. Posted by:huangzhengdoc at 2015-08-22 08:43:22
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<cmath>
using namespace std;
struct node
{
	int x,y;double d;
}a[1100000];int len,fa[110000];
int n;
int cmp(const void *x1,const void *x2)
{
	node n1=*(node *)x1;
	node n2=*(node *)x2;
	if(n1.d>n2.d) return 1;
	else return -1;
}
int findmax(int x)
{
	if(fa[x]==x) return x;
	else
	{
		fa[x]=findmax(fa[x]);
		return fa[x];
	}
}
char name[11000][30];
int panduan(const char *s)
{
    for(int i=1;i<=n;i++)
    {
        if(strcmp(name[i],s)==0) return i;
    }
}
int main()
{
    int k,x,y;double ans=0.0;double l;double d;
    scanf("%lf",&l);
    scanf("%d",&n);
    int len=0;
    int t=0;ans=0.0;
    for(int i=1;i<=n;i++) scanf("%s",name[i]);
    scanf("%d",&len);
    char s1[21],s2[21];
    for(int i=1;i<=len;i++)
    {
        scanf("%s%s%lf",s1,s2,&d);
        x=panduan(s1);
        y=panduan(s2);
        a[i].x=x;a[i].y=y;a[i].d=d;
    }
    for(int i=1;i<=n;i++) fa[i]=i;
    qsort(a+1,len,sizeof(node ),cmp);
    for(int i=1;i<=len;i++)
    {
        int fx=findmax(a[i].x),fy=findmax(a[i].y);
        if(fx!=fy)
        {
            ans+=a[i].d;
            fa[fy]=fx;
            t++;
            if(t==n-1) break;
        }
    }
    if(ans<=l) printf("Need %.1f miles of cable\n",ans);
    else printf("Not enough cable\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