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

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

主站蜘蛛池模板: 成人在线免费视频观看 | 丝袜中文字幕 | 99亚洲国产精品 | 天天射日日射 | 午夜在线小视频 | 欧美另类一区二区 | av在线毛片 | 影音先锋在线看片资源 | 国产又粗又猛又色 | 成人国产一区二区 | 91午夜视频在线观看 | 日韩免费高清视频网站 | 野外一级片 | 欧美一级大片免费看 | 26uuu亚洲国产精品 | 天天爱天天操 | 日韩精品一线二线三线 | 天天操夜夜添 | 精品aaa| 婷婷色亚洲 | 欧美日韩中文字幕在线播放 | 精彩视频一区二区三区 | 国产精品99久久久精品无码 | 阿的白色内裤hd中文 | 欧亚在线视频 | 欧美大片大全 | 欧美人与性动交xxⅹxx | 夜夜福利 | 亚洲日皮 | 老妇裸体性猛交视频 | 国产成人精品影院 | 肌肉猛男裸体gay网站免费 | 日批av | 欧美亚州国产 | 又色又爽又高潮免费视频国产 | 亚洲一区,二区 | 亚洲痴女 | 日韩乱码人妻无码中文字幕久久 | 亚洲精品www| 久草资源网站 | 自拍偷拍国产精品 | 精品久久久亚洲 | 成人av网站在线播放 | 精品资源成人 | 午夜国产一区二区 | 精品视频在线免费观看 | 污版视频在线观看 | 天堂а√在线最新版中文在线 | 国产三级大片 | 亚洲色图视频在线 | 国产不卡在线播放 | 人妻天天爽夜夜爽一区二区三区 | 性色在线观看 | 亚洲影院av | 成人天堂网 | 特级淫片裸体免费看 | 伊人影院在线观看 | 亚洲福利在线观看 | 天天在线观看 | eeuss一区二区三区 | 色噜av | 婷婷综合激情网 | 国产一区二区成人 | 成年人毛片 | 国产不卡一 | 久久精品性爱视频 | 久久精品国产清自在天天线 | 精品国产乱码一区二区三区99 | 污污视频免费网站 | 99精品久久| 精品在线一区 | 国产少女免费观看高清 | 自拍偷拍麻豆 | 99视频免费在线观看 | 四虎色网 | 一本一道av| 簧片av| 草草国产 | 成人免费版 | 裸体美女免费视频网站 | 日韩免费黄色 | 精品久草 | 扒开女人屁股进去 | 欧美日韩一区二区综合 | 黄色69 | 狠狠爱婷婷 | 成人先锋av | 国产原创在线播放 | 免费av看片 | 四虎成人在线视频 | 懂色av一区二区三区在线播放 | 久久福利片 | 亚洲免费色视频 | 欧美一级淫片免费 | 天堂视频中文在线 | 免费中文字幕日韩欧美 | 国产成人区 | 亚洲av少妇一区二区在线观看 | 亚洲特黄视频 |