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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

HDU1102 Constructing Roads 最小生成树

發布時間:2024/10/6 编程问答 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 HDU1102 Constructing Roads 最小生成树 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

點擊打開鏈接

F.A.Q
Hand In Hand
Online Acmers
Forum | Discuss
Statistical Charts
Problem Archive
Realtime Judge Status
Authors Ranklist
?
?????C/C++/Java Exams ????
ACM Steps
Go to Job
Contest LiveCast
ICPC@China
Best Coder beta
VIP | STD Contests
Virtual Contests
????DIY | Web-DIY beta
Recent Contests
liuhulin
Mail 0(0)
Control Panel
Sign Out

Constructing Roads

Time Limit: 2000/1000 MS (Java/Others)????Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 26460????Accepted Submission(s): 10130


Problem Description There are N villages, which are numbered from 1 to N, and you should build some roads such that every two villages can connect to each other. We say two village A and B are connected, if and only if there is a road between A and B, or there exists a village C such that there is a road between A and C, and C and B are connected.

We know that there are already some roads between some villages and your job is the build some roads such that all the villages are connect and the length of all the roads built is minimum.

Input The first line is an integer N (3 <= N <= 100), 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, 1000]) between village i and village j.

Then there is an integer Q (0 <= Q <= N * (N + 1) / 2). Then come Q lines, each line contains two integers a and b (1 <= a < b <= N), which means the road between village a and village b has been built.

Output You should output a line contains an integer, which is the length of all the roads to be built such that all the villages are connected, and this value is minimum.

Sample Input30 990 692990 0 179692 179 011 2Sample Output179
Source kicc
Recommend Eddy???|???We have carefully selected several similar problems for you:??1233?1301?1162?1232?1875?
Statistic?|?Submit?|?Discuss | Note
Hangzhou Dianzi University Online Judge 3.0
Copyright ? 2005-2018 HDU ACM Team. All Rights Reserved.
Designer & Developer : Wang Rongtao?LinLe?GaoJie?GanLu
Total 0.004000(s) query 5, Server time : 2018-05-06 22:29:14, Gzip enabled

分析:

題目中已經給出一些道路,這些道路將村莊將村莊構成了部分連通集
要求的是使全部村莊連通的最小道路和,實際為最小生成樹問題

1.kruskal算法

已經給出一些邊,可能不是最小生成樹的邊,只需要選擇邊長度和最小即可
在給出的邊的集合中,已經構成部分連通集,接下來將所有邊放入一個最小堆中,
每次取出權重最小的邊,看邊的兩個端點是否屬于同一個集合,不屬于就加入這條邊,
否則就舍棄,這種貪心的選擇就是kruskal算法
//kruskal算法 #include<iostream> #include<cstring> #include<queue> using namespace std; const int maxn=120; struct Edge {int v,u,weight;bool friend operator < (Edge a,Edge b){return a.weight>b.weight;} }; //并查集模板 int pre[maxn]; int get_parent(int x) {if(-1==pre[x]) return x;return pre[x]=get_parent(pre[x]); } bool mix (int x,int y) {int fx=get_parent(x),fy=get_parent(y);if(fx==fy)return 1;pre[fx]=fy;return 0; }priority_queue<Edge> E; int kruskal() {int cost=0;while(!E.empty()){Edge now=E.top();E.pop();if(!mix(now.v,now.u)){//不在同一個集合中cost+=now.weight;}}return cost; } int main() {int n;while(cin>>n){while(!E.empty()) E.pop();memset(pre,-1,sizeof(pre));Edge e;for(int i=1;i<=n;i++){e.v=i;for(int j=1;j<=n;j++){e.u=j;cin>>e.weight;if(i<j) E.push(e);}}int q,a,b;cin>>q;while(q--){cin>>a>>b;mix(a,b);}int ans=kruskal();cout<<ans<<endl;}return 0; }

2.Prime算法

???????由于題目中給定了一些邊,處理的辦法是將這些邊權值設置為0.定義一個visit[i]數組記錄頂點i是否在樹中。
#include<iostream> #include<cstdio> #include<cstring> using namespace std; #define MAX_SIZE 105 int parent[MAX_SIZE]; //記錄父節點 int E[MAX_SIZE][MAX_SIZE]; //邊集合 int Prim(int N); bool visit[MAX_SIZE]; //判斷該頂點是否在集合里面 int main(){int Q, N, i, j, v, w, ans;while (scanf("%d",&N)!=EOF){memset(visit, 0, sizeof(visit)); //初始化for (i = 1; i <= N; i++)for (j = 1; j <= N; j++)scanf("%d",&E[i][j]); //輸入邊scanf("%d", &Q);for (i = 0; i < Q; i++){scanf("%d%d",&v,&w);E[v][w] = E[w][v] = 0; //已建好將權值設為0}//-----------------------ans = Prim(N);printf("%d\n",ans);}return 0; } int Prim(int N){int i, j, k,pos,lmin,ans;parent[1] =-1; //設置根節點visit[1] = 1; //表示頂點 1已加入樹中int LowCost[MAX_SIZE];for (i = 1; i <= N; i++)LowCost[i] = INT_MAX; //初始化ans = 0;j = 1;for (i = 2; i <= N; i++){/*j表示上一次加入集合的頂點*/for (k = 1; k <= N; k++){if (!visit[k] && LowCost[k] > E[k][j]){ //更新j的鄰接點parent[k] = j; //將k父節點暫定為 jLowCost[k] = E[k][j];}}lmin = INT_MAX, pos = 0;for (k = 1; k <= N; k++){ //找出不在樹中且離樹距離最小的點if (!visit[k]&&lmin > LowCost[k]){pos = k;lmin = LowCost[k];}}visit[pos] = 1; //加入樹中ans += E[pos][parent[pos]]; //加入邊j = pos; //更新j}return ans; }

總結

以上是生活随笔為你收集整理的HDU1102 Constructing Roads 最小生成树的全部內容,希望文章能夠幫你解決所遇到的問題。

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