作者:手机用户美佛_885 | 来源:互联网 | 2023-10-11 11:59
f.highways time limit 33601000 msmemorylimit 3360漂亮的砖头64-bitintegerioformat : % lldjavaclassname 3360 maintheislandnationonofsatate y, sldmnhasaverypoorsystemofpublichighways.thesldmnngovernmentisawareofthisproblemandhasalreadyconstructedanumberofhighwaysconconononays rtanttowns.However,therearestillsometownsthatyoucan ' treachviaahighway.itisnecessarytobuildmorehighwayssothatitwilbepoucan
sldmntownsarenumberedfrom1tonandtownihasapositiongivenbythecartesiancoordinates (Xi, yi ).eachighwayconnectsexacltytwotowns.all highways (boththeoriginalonesandtheonesthataretobebuilt ) folllow straight lines ays esiandistancebetweentowns.allhighwayscanbeusedinbothdirections.highwayscanfreelycrosseachotth butadrivercanonlyswitchbetwetwethbetwetwethbetwetwethetwetwetetwethbetwetwetwetet
thesldmnngovernmentwantstominimizethecostofbuildingnewhighways.however, theywanttoguaranteethateverytownishighway-reachablefromeveryothertown.sincesldmnissoflat,thecostofahighwayisalwaysproporat theleastexpensivehighwaysystemwillbetheonethatminimizesthetotalhighwal
inputtheinputconsistsoftwoparts.thefirstpartdescribesalltownsinthecountry,andthesecondpartdescribesallofthehighwaysthathavave
thefirstlineoftheinputfilecontainsasingleintegern (1=n=750 ),representingthenumberoftowns.thenextnlineseachcontaintwow xiandyiseparatedbyaspace.thesevaluesgivethecoordinatesofithtown (for ifrom1ton ).coordinateswillhaveanabsolutevaluenogreatesogreated
thenextlinecontainsasingleintegerm (0=m=1000 ), representingthenumberofexistinghighways.thenextmlineseachcontainapairofintegersseparatedbyaspace.thesetwointegersgiveapapair onnectedbyahighway.eachpairoftownsisconnectedbyatmostonehighway。
outputwritetotheoutputasinglelineforeachnewhighwaythatshouldbebuiltinordertoconnectalltownswithminimalposssibletotalllllengthof new ldbepresentedbyprintingtownnumbersthatthishighwayconnects,separated by a space。
ifnonewhighwaysneedtobebuilt (alltownsarealreadyconnected ),theoutputfileshouldbecreatedbutitshouldbeeempty。
sample input 91500324551045212533139712 sample output 163749578
解决问题:找到最小生成树的父节点。 。
1 # include iostream2# include cstdio3# include cstring4# include cmath5# include algorithm6# include climits7# include vector 8 include string 11 # includeset 12 # definelllonglong 13 # define INF0x3F3 f14 usingnamespacestd 15 const int maxn=800; 16 int mp[maxn腼腆的故事maxn] 17 struct pos {18 int x,y; 19 ) p[maxn]; 20 int n,m,d[maxn],pre[maxn]; 1intdis(constposa,const pos b ) 22返回(a.x-b.x ) ) a.x-b.x ) ) a.y-b.y ) ) a.y-b.y ); 23 ) 24voidprim(intsrc ) {25 int i,j,index,theMin; 26for(I=1; i=n; I({27pre[I]=src; 28 d[i]=mp[src腼腆故事i] 29 ) 30pre[src]=-1; 31for(I=1; i n; I ) {32 theMin=INF; 33索引=-1; 34for(j=1; j=n; j ) 35if(pre[j]!=-1d [ j ] them in (them in=d [ index=j ]; 36 ) 37if(themin!=0) printf('%d%d(n ),index,pre[index] ); 38 pre[index]=-1; 39for(j=1; j=n; j () 40if(pre[j]!=-1d [ j ] MP [索引腼腆故事j ] ({ 41d [ j ]=MP [索引腼腆故事j ] 42 pre [ j ]=索引; 43(45 ) 46 ) 47intmain ) ) {48 int i,j,u,v; 49Scanf('%d ',n ); 50for(I=1; i=n; I )扫描(' % d % d )、p[i].x、p[i].y ); 51for(I=1; i=n; I ) 52for(j=I1; j=n; j ) 53 mp[i腼腆故事j]=mp[j腼腆故事I]=dis(p[I],p[j]; 54 ) 55扫描(' % d ',m ); 56while(m-- ) 57scanf ) ' %d%d ',u,v ); 58 mp[u腼腆故事v]=mp[v腼腆故事u]=0; 59 ) 60prim(1; 61返回0; 62 )视图代码