ZOJ 2048 Highways
2012-11-12 15:21
369 查看
N-Highways TimeLimit:5000MSMemoryLimit:32768KB64bitIOFormat:%lld&%llu SubmitStatus Appointdescription: Description TheislandnationofFlatopiaisperfectlyflat.Unfortunately,Flatopiahasaverypoorsystemofpublichighways.TheFlatopiangovernmentisawareofthisproblemandhasalreadyconstructedanumberofhighwaysconnectingsomeofthemostimportanttowns.However,therearestillsometownsthatyoucan'treachviaahighway.Itisnecessarytobuildmorehighwayssothatitwillbepossibletodrivebetweenanypairoftownswithoutleavingthehighwaysystem. Flatopiantownsarenumberedfrom1toNandtownihasapositiongivenbytheCartesiancoordinates(xi,yi).Eachhighwayconnectsexacltytwotowns.Allhighways(boththeoriginalonesandtheonesthataretobebuilt)followstraightlines,andthustheirlengthisequaltoCartesiandistancebetweentowns.Allhighwayscanbeusedinbothdirections.Highwayscanfreelycrosseachother,butadrivercanonlyswitchbetweenhighwaysatatownthatislocatedattheendofbothhighways. TheFlatopiangovernmentwantstominimizethecostofbuildingnewhighways.However,theywanttoguaranteethateverytownishighway-reachablefromeveryothertown.SinceFlatopiaissoflat,thecostofahighwayisalwaysproportionaltoitslength.Thus,theleastexpensivehighwaysystemwillbetheonethatminimizesthetotalhighwayslength. Input Theinputconsistsoftwoparts.Thefirstpartdescribesalltownsinthecountry,andthesecondpartdescribesallofthehighwaysthathavealreadybeenbuilt. ThefirstlineoftheinputcontainsasingleintegerN(1<=N<=750),representingthenumberoftowns.ThenextNlineseachcontaintwointegers,xiandyiseparatedbyaspace.Thesevaluesgivethecoordinatesofithtown(forifrom1toN).Coordinateswillhaveanabsolutevaluenogreaterthan10000.Everytownhasauniquelocation. ThenextlinecontainsasingleintegerM(0<=M<=1000),representingthenumberofexistinghighways.ThenextMlineseachcontainapairofintegersseparatedbyaspace.Thesetwointegersgiveapairoftownnumberswhicharealreadyconnectedbyahighway.Eachpairoftownsisconnectedbyatmostonehighway. Output Writetotheoutputasinglelineforeachnewhighwaythatshouldbebuiltinordertoconnectalltownswithminimalpossibletotallengthofnewhighways.Eachhighwayshouldbepresentedbyprintingtownnumbersthatthishighwayconnects,separatedbyaspace. Ifnonewhighwaysneedtobebuilt(alltownsarealreadyconnected),thentheoutputshouldbecreatedbutitshouldbeempty. Thisproblemcontainsmultipletestcases! ThefirstlineofamultipleinputisanintegerN,thenablanklinefollowedbyNinputblocks.Eachinputblockisintheformatindicatedintheproblemdescription.Thereisablanklinebetweeninputblocks. TheoutputformatconsistsofNoutputblocks.Thereisablanklinebetweenoutputblocks. SampleInput 1 9 15 00 32 45 51 04 52 12 53 3 13 97 12 SampleOutput 16 37 49 57 83 //求最小生成树将新加入的边的2个端点输出来
#include<iostream> #include<stdio.h> #include<queue> #include<stack> #include<set> #include<vector> #include<math.h> #include<string.h> #include<algorithm> usingnamespacestd; #defineN755 doubledis(doublex,doubley,doublea,doubleb) { returnsqrt((x-a)*(x-a)+(y-b)*(y-b)); } doubled[N][2]; doublemap[N][N]; boolb[N]; doubleds[N]; intf[N],n; voidPrim() { inti,k; memset(b,0,sizeof(b)); doubleMin; intt=n; while(--t) { Min=1000000; for(i=2;i<=n;i++) if(!b[i]&&Min>ds[i]) { Min=ds[i]; k=i; } b[k]=1; for(i=2;i<=n;i++) if(!b[i]&&map[k][i]<ds[i]) { f[i]=k; ds[i]=map[k][i]; } } } intmain() { intT; boolfl=0; inti,j,k,m; scanf("%d",&T); while(T--) { if(fl)printf("\n");elsefl=true; scanf("%d",&n); for(i=1;i<=n;i++) scanf("%lf%lf",&d[i][0],&d[i][1]),f[i]=1; for(i=1;i<=n;i++) for(j=i+1;j<=n;j++) map[i][j]=map[j][i]=dis(d[i][0],d[i][1],d[j][0],d[j][1]); scanf("%d",&m); for(i=1;i<=m;i++) { scanf("%d%d",&j,&k); map[j][k]=map[k][j]=0; } for(i=1;i<=n;i++) ds[i]=map[1][i]; Prim(); for(i=2;i<=n;i++) if(map[i][f[i]]!=0) printf("%d%d\n",i,f[i]); } return0; }
相关文章推荐
- POJ 1751 Highways (ZOJ 2048 ) MST
- ZOJ 2048 / POJ 2485 Highways ( kruskal )
- zoj 2048 Highways(Kruskal~)
- POJ 1751 Highways (ZOJ 2048 ) MST
- ZOJ 2048 Highways
- ZOJ 2048 Highways【kruskal】
- zoj 2048 Highways
- ZOJ 2048 Highways 最小生成树 Kruskal && Prim
- ZOJ 2048 :Highways 【Prim】
- zoj 2048 && poj 1751 Highways
- ZOJ 2048 highways
- ZOJ 2048 highways
- zoj - 2048 - Highways
- zoj - 2048 - Highways
- zoj 2048 - Highways
- zoj 2048 Highways(最小生成树)
- zoj 2048 Highways
- ZOJ 2048 POJ 1751 Highways
- zoj 3802 Easy 2048 Again(状压DP)
- ZOJ 3802 Easy 2048 Again(简单DP)