POJ 2485 - Highways(求最小生成树的最大权值-Kruskal算法)
生活随笔
收集整理的這篇文章主要介紹了
POJ 2485 - Highways(求最小生成树的最大权值-Kruskal算法)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
題目
Language:DefaultHighways
Description The island nation of Flatopia is perfectly flat. Unfortunately, Flatopia has no public highways. So the traffic is difficult in Flatopia. The Flatopian government is aware of this problem. They’re planning to build some highways so that it will be possible to drive between any pair of towns without leaving the highway system.Flatopian towns are numbered from 1 to N. Each highway connects exactly two towns. All highways follow straight lines. All highways can be used in both directions. Highways can freely cross each other, but a driver can only switch between highways at a town that is located at the end of both highways. The Flatopian government wants to minimize the length of the longest highway to be built. However, they want to guarantee that every town is highway-reachable from every other town. Input The first line of input is an integer T, which tells how many test cases followed.The first line of each case is an integer N (3 <= N <= 500), which is the number of villages. Then come N lines, the i-th of which contains N integers, and the j-th of these N integers is the distance (the distance should be an integer within [1, 65536]) between village i and village j. There is an empty line after each test case. Output For each test case, you should output a line contains an integer, which is the length of the longest road to be built such that all the villages are connected, and this value is minimum.Sample Input 13 0 990 692 990 0 179 692 179 0Sample Output 692Hint Huge input,scanf is recommended.Source POJ Contest,Author:Mathematica@ZSU |
分析
題目大概意思就是,輸入城鎮數量n,然后接下來是一個n×n的鄰接矩陣,值就代表兩點間的距離,輸出最小生成樹的最大權值。
知道這點就好辦啦,構建一個結構體,把每組起點、終點和兩點間的距離都存進去,然后用Kruskal算法求出最小生成樹,把每一條邊的權值都存入到一個新的數組中,然后排序輸出最大值即可。
注意
1、正常輸出即可,不要特意去增加換行符
2、由于輸入的權值可能會非常大,所以用cin會超時,我就在這奉獻了一次TLE(Time Limit Exceeded)。
cin和scanf的區別是scanf是格式化輸入,printf是格式化輸出,效率較高; cin是輸入流,cout是輸出流,效率稍低。cin與cout之所以效率低,因為是先把要輸入/出的東西存入緩沖區,再輸入/出,導致效率降低。 詳情可參見大佬的一篇博客:https://www.cnblogs.com/limera/p/5405705.html
最后上代碼
#include <iostream> #include <algorithm> #include <cstdio> #include <cstring> using namespace std; const int MAXN=1e5 + 10;struct node {int u,v;//u:起點,v:終點 int dis;//兩點距離 }N[MAXN];int pre[MAXN]; int n; int find(int x) {if(x==pre[x]) return x;return pre[x]=find(pre[x]); } bool cmp1(node a,node b) {return a.dis < b.dis; } bool cmp2(int a,int b) {return a>b; } void init() {for(int i=0;i<=n;i++){pre[i]=i;} }int main() {int t,i,k,j;int a[MAXN];cin>>t;while(t--){memset(N,0,sizeof(N));memset(a,0,sizeof(a));k=0;cin>>n;init();for(i=0;i<n;i++)//將每條路徑的起點、終點和距離都存入到結構體中 {for(j=0;j<n;j++){N[k].u=i;//起點 N[k].v=j;//終點 //cin>>N[k].dis;//如果輸入數據過大,這樣會超時 scanf("%d",&N[k].dis);//兩點距離 k++;}}sort(N,N+k,cmp1);j=0;for(i=0;i<k;i++){int dx=find(N[i].u);int dy=find(N[i].v);if(dy!=dx){a[j++]=N[i].dis;pre[dy]=dx;}if(j==n-1) break;//最小生成樹的條件:邊數=頂點數-1 }sort(a,a+j,cmp2);cout<<a[0]<<endl;}return 0; }總結
以上是生活随笔為你收集整理的POJ 2485 - Highways(求最小生成树的最大权值-Kruskal算法)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Codeblocks配置EGE图形库
- 下一篇: HDU 1711 -Number Seq