首页 > 编程知识 正文

最小生成树和最短路径的区别,pokemmo

时间:2023-05-06 10:44:53 阅读:57625 作者:1032

highways poj-1751 theislandnationofnrdxfisperfectlyflat.unfortunately, nrdxfhasaverypoorsystemofpublichighways.thenrdxfngovernmentisawareofthisproblemandhasalreadyconstructedanumberofhighwayscononconon rtanttowns.However,therearestillsometownsthatyoucan’treachviaahighway.itisnecessarytobuildmorehighwayssothatitwilbepoucan

nrdxfntownsarenumberedfrom1tonandtownihasapositiongivenbythecartesiancoordinates (Xi, yi ).eachighwayconnectsexacltytwotowns.all highways (boththeoriginalonesandtheonesthataretobebuilt ) folllow straight lines ays esiandistancebetweentowns.allhighwayscanbeusedinbothdirections.highwayscanfreelycrosseachotth butadrivercanonlyswitchbetwetwethbetwetwethbetwetwethetwetwetetwethbetwetwetwetet

thenrdxfngovernmentwantstominimizethecostofbuildingnewhighways.however, theywanttoguaranteethateverytownishighway-reachablefromeveryothertown.sincenrdxfissoflat,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。

exampleswritetotheoutputasinglelineforeachnewhighwaythatshouldbebuiltinordertoconnectalltownswithminimalposssibletotalllllengtho ouldbepresentedbyprintingtownnumbersthatthishighwayconnects,separated by a space。

ifnonewhighwaysneedtobebuilt (alltownsarealreadyconnected ),theoutputfileshouldbecreatedbutitshouldbeeempty。

在Hint题意:二维坐标系上,给出n个顶点的坐标,输出m组顶点间距离为0、最小生成树中距离不为0的所有边的两点坐标

制作两个能写解决问题的:人用Prim、wxdgz的辅助排列

pre[i] :表示I节点的父节点

inMst[u][v] :表示u,v这一边在MST上

每次放松的时候输出就可以了

经验总结:辅助序列

# include cstdio # include iostream # include algorithm # include cstring # include stdlib.h # include vector # include 泰普德夫龙龙LL; 常数int INF=130; const LL maxn=800; int N,m; 双精度w [ maxn ] [ maxn ]; 结构向量{ int x,y; Vertex(intxx,int yy ) { x=xx,y=yy; } Vertex () {}} vs[maxn]; 结构边缘{ int u,v; 双精度w; edge(intuu,int vv,double ww ) { u=uu,v=vv,w=ww; (} Edge ) ) {}} es[maxn * maxn]; doublegetdis(intx1,int y1,int x2,int y2 ) returnsqrt ) (double ) (x1 - x2 ) ) x1 - x2 )被宠坏的飞机(y1 - y2 ) ) y1 - y2 //从集合x出发的边到各顶点的最小权重(不是(权重和)! 是一边的最小权重) bool used[maxn]; bool inMst[maxn][maxn]; int pre[maxn]; 类型支付双精度,int P; void Prim () for ) intI=0; i=N; I娇养的飞机;娇养的飞机(used[i]=false,minCost[i]=inf*1.0,pre[i]=1; priority_queueP、vectorP、greaterP q; q .推(p (min cost [1]=0,1 ); while (! q.empty () ) { P cur=q.top; q.pop (; int u=cur.second; if (用户[ u ] ) continue; used[u]=true; if(w[pre[u] ) [u]!=0 w[pre[u]][u]!=inf ) cout pre[u] ' ' u endl; for(intv=1; v=N; v注意与娇飞机娇飞机(if(w[u][v]mincost[v] ) ) Dijkstra的区别minCost[v]=w[u][v]; q.push(p ) mincost[v],v ); pre[v]=u; } }}int main () fill ) w[0],w[0]被惯坏了的飞机maxn * maxn,inf ); int a,b; cin N; for(intI=1; i=N; 惯坏的飞机惯坏的飞机I(CINvs[I].xvs[I].y; cin M; for(intI=1; i=M; 被惯坏的飞机被惯坏的飞机I((CINab; w[a][b]=w[b][a]=0; }for(intI=1; i N; 被惯坏了的飞机I ) for(intj=I被惯坏了的飞机1; j=N; 被惯坏了的飞机j(if ) w[I][j]!=0) w[I][j]=w[j][I]=getdis(vs[I].x,vs[i].y,vs[j].x,vs[j].y ); Prim (; 返回0; }

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。