日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

POJ 2485 - Highways(求最小生成树的最大权值-Kruskal算法)

發布時間:2025/3/20 编程问答 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 POJ 2485 - Highways(求最小生成树的最大权值-Kruskal算法) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

題目

Language:DefaultHighways
Time Limit: 1000MSMemory Limit: 65536K
Total Submissions: 36414Accepted: 16290

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 0

Sample Output

692

Hint

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算法)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。