highways time limit 33602000/1000 ms (Java/other )内存limit 3360131072/65536 k ) Java/other )总交付(s ) s ) 3336 k 33609 problemdescriptiontheislandnationofflatopiaisperfectlyflat.unfortunately, flatopiahasnopublichighways.sothetrafficisdifficultinflatopia.theflatopiangovernmentisawareofthisproblem.they ' re planianianian sibletodrivebetweenanypairoftownswithoutleavingthehighwaysystem。
flatopiantownsarenumberedfrom1ton.eachhighwayconnectsexactlytwotowns.allhighwaysfollowstraightlines.allhighwayscanbeused osseachother,butadrivercanonlyswitchbetweenhighwaysatatownthatislocatedattheendofbothhhhighways
theflatopiangovernmentwantstominimizethelengthofthelongesthighwaytobebuilt.however,theywantttoguaranteeethateverytownishighwhwhwhighwhwhetoghighethoghowhwhowhwhthwhwhoghwhtht
inputthefirstlineofinputisanintegert, whichtellshowmanytestcasesfollowed.brthefirstlineofeachcaseisanintegern (3=n=500 which is the number of快速钢铁侠. then come n lln thei-thofwhichcontainsnintegers,andthej-thofthesenintegersisthedistanceshouldbeanintegerwithin [ 1,65536
Output For each test case,youshouldoutputalinecontainsaninteger,whichisthelengthofthelongestroadtobebuiltsuchthatalllthe快速钢铁侠
sample input 1309906929900179692179
Sample Output 692
Source PKU题意:如果有几个城镇,两个城镇之间要相通,那就是修最少的路中最大的路! 想法:
最小生成树问题,prim算法
一、什么是图的最小生成树(MST )?
不知道你是否记得树定理。 n个点在N-1条边相连组成一个相连的块,形成的图形可能只有树。 可能没有其他有n个点的图。 边必须在N-1条以上。 图中的最小生成树从这些边中选择N-1个引出,连接所有n个点。 这个N-1边的边权之和是所有方案中最小的。
Prim算法Prim算法采用与Dijkstra、Bellman-Ford算法相同的“蓝白点”思想。 白点表示已经在最小生成树中的点,蓝点表示不在最小生成树中的点。
算法说明:以1为起点生成最小生成树,min[v]表示蓝点v与白点相连的最小边权。
MST表示最小生成树的权重之和。
a )初始化: min[v]=(v1 ); min[1]=0; MST=0;
b ) for ) I=1; i=n; I )
寻找最小[ u ]的蓝点u。
2 .将u标记为白点
3.MST=min[u]
4 .与for白点u相连的所有蓝点v
if(w(u ) v (min ) v ) )
min[v]=w[u][v];
c )算法结束: MST是最小生成树的权重之和
算法分析思想说明:
Prim算法在每次循环中将蓝点u变为白点,蓝点u与白点相连的最小边权重min[u]是当前所有蓝点中最小的。 这相当于将最小边添加到生成树中n-1次,最终得到的结果一定是最小生成树。
代码:
# include iostream # include stdio.h # include cstring # include cmath # include string # includealgorithmusingnamespacestd; int g[1005][1005]; //相邻矩阵int minn[1005]; //minn[i]表示存储连接蓝点I和白点的最小边权int n,I,j; bool u[1005]; int main () { int T; cinT; wile(t---- ) { cinn; {for(I=1; i=n; I ) for ) j=1; j=n; j ) cing[i][j]; }memset(minn,0x7f,sizeof ) minn ); //注意,在此将最大minn[1]=0初始化; 短信(u,1,sizeof(u ) u ); //1初始化,表示所有顶点都不在生成树for中(I=1; i=n; I ) { int k=0; 注意理解//for (j=1; j=n; j ) if(u(j ) minn ) j ) minn ) k ) k=j; //寻找与白点相关的最小权重,未进入生成树的k。 因为与白点无关的蓝点不会支付为正常值,而是无限大u[k]=0; //在生成树中添加标记,然后单击for(j=1; j=n; j ) if(u[j] ) g[k][j]minn[j] ) minn[j]=g[k][j]; 修复与//k相关联的所有生成树点} int maxx=-1; for(I=1; i=n; I ) if(maxxminn[I] ) maxx=minn[i]; //求最大中间路径coutmaxxendl; }返回0; )须知:
开始接触图论!