Zoj2326Tangled in Cables

Source: Internet
Author: User

// Zoj2326Tangled in Cables
// Use a rope of a given length to link the house in the village. The house can be regarded as a point.
// There is only one road between each pair of houses. I use Kruscal.
// The zoj platform is powerful ......
// The cmp function defines the return value as an integer. When I return a real number
// (Return (p1-> length-p2-> length) on WA .. Bytes
// If (p1-> length> p2-> length)
// Return 1;
// Else return-1;
// Change it to AC...

#include"stdio.h"#include"string.h"#include"stdlib.h"#define maxint 1000typedef struct point{int s,e;float length;}epoint;epoint side[maxint];int v[maxint];int cmp(const void *a,const void *b){epoint *p1,*p2;p1=(epoint*)a;p2=(epoint*)b;if(p1->length>p2->length)return 1;else return -1;}int find(int i){while(v[i]!=i)i=v[i];return i;}float kruscal(int n,int m){int i,j=0,v1,v2;float sum=0.0;for(i=0;i<m;i++){v1=find(side[i].s);v2=find(side[i].e);if(v1!=v2){sum+=side[i].length;v[v1]=v2;j++;if(j==n-1)break;}}return sum;}int main(){float cable,val,sum;char name[maxint][22],name1[22],name2[22];int n,m,r,c,i,j;while(scanf("%f",&cable)!=EOF){scanf("%d",&n);for(i=0;i<n;i++)scanf("%s",name[i]);scanf("%d",&m);for(i=0;i<m;i++){scanf("%s",name1);scanf("%s",name2);scanf("%f",&val);for(j=0;j<n;j++)if(!strcmp(name1,name[j])){r=j;break;}for(j=0;j<n;j++)if(!strcmp(name2,name[j])){c=j;break;}side[i].e=c;side[i].s=r;side[i].length=val;}for(i=0;i<n;i++)v[i]=i;qsort(side,m,sizeof(epoint),cmp);sum=kruscal(n,m);if(sum>cable)printf("Not enough cable\n");elseprintf("Need %.1f miles of cable\n",sum);}return 0;}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.