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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

【POJ - 2485 】Highways (最小生成树,Prim算法,瓶颈生成树)

發布時間:2023/12/10 编程问答 21 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【POJ - 2485 】Highways (最小生成树,Prim算法,瓶颈生成树) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

題干:

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.

題目大意:

A國沒有高速公路,因此A國的交通很困難。政府意識到了這個問題并且計劃建造一些高速公路,以至于可以在不離開高速公路的情況下在任意兩座城鎮之間行駛。

A國的城鎮編號為1到N, 每條高速公路連接這兩個城鎮,所有高速公路都可以在兩個方向上使用。高速公路可以自由的相互交叉。

A國政府希望盡量減少最長高速公路的建設時間(使建設的最長的高速公路最短),但是他們要保證每個城鎮都可以通過高速公路到達任意一座城鎮。

解題報告:

? ?介于有的題目確實Prim算法效率極高(對于n=2000這樣的完全圖,用Kruskal就作死了),我決定順道學習一下Prim、、、(

AC代碼:

#include<cstdio> #include<iostream> #include<algorithm> #include<queue> #include<map> #include<vector> #include<set> #include<string> #include<cmath> #include<cstring> #define ll long long #define pb push_back #define pm make_pair using namespace std; const int MAX = 2e5 + 5; const int INF = 0x3f3f3f3f; int n; int cost[666][666]; int dis[666]; bool vis[666]; int prim() {//這種寫法適用于已知點是1~N的。int res = 0;for(int i = 0; i<=n; i++) dis[i] = INF, vis[i] = 0;dis[1] = 0;//假設從1開始while(1) {int v = 0;//這種寫法的話上面初始化必須從0開始了。。for(int i = 1; i<=n; i++) {if(!vis[i] && dis[i] < dis[v]) v = i;}if(!v) break;//這種寫法的話上面初始化必須從0開始了。。 vis[v] = 1;res = max(res,dis[v]) ;for(int i = 1; i<=n; i++) {if(!vis[i] && cost[v][i] < dis[i]) dis[i] = cost[v][i];//!vis[i]這一句加不加都一樣的。}} return res; } int main() {int t;cin>>t;while(t--) {scanf("%d",&n);for(int i = 1; i<=n; i++) {for(int j = 1; j<=n; j++) {scanf("%d",&cost[i][j]);} }int ans = prim();printf("%d\n",ans);}return 0 ;}

Prim的另一種寫法:(相對常用一些)

#include<cstdio> #include<iostream> #include<algorithm> #include<queue> #include<map> #include<vector> #include<set> #include<string> #include<cmath> #include<cstring> #define ll long long #define pb push_back #define pm make_pair using namespace std; const int MAX = 2e5 + 5; const int INF = 0x3f3f3f3f; int n; int cost[666][666]; int dis[666]; bool vis[666]; int prim() {//這種寫法不一定必須點是1~N的,也可以是0~N-1的,只需要把下面第三行那個改成dis[0]=0就行了。int res = 0;for(int i = 1; i<=n; i++) dis[i] = INF, vis[i] = 0;//這種寫法的話初始化就可以從1開始 dis[1] = 0;//假設從1開始while(1) {int v,minw = INF;for(int i = 1; i<=n; i++) {if(!vis[i] && dis[i] < minw) v = i,minw = dis[i];}if(minw == INF) break;vis[v] = 1;res = max(res,dis[v]) ;for(int i = 1; i<=n; i++) {if(!vis[i] && cost[v][i] < dis[i]) dis[i] = cost[v][i];}} return res; } int main() {int t;cin>>t;while(t--) {scanf("%d",&n);for(int i = 1; i<=n; i++) {for(int j = 1; j<=n; j++) {scanf("%d",&cost[i][j]);} }int ans = prim();printf("%d\n",ans);}return 0 ;}

?

總結

以上是生活随笔為你收集整理的【POJ - 2485 】Highways (最小生成树,Prim算法,瓶颈生成树)的全部內容,希望文章能夠幫你解決所遇到的問題。

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