HDU1102 Constructing Roads 最小生成树
生活随笔
收集整理的這篇文章主要介紹了
HDU1102 Constructing Roads 最小生成树
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
點擊打開鏈接
要求的是使全部村莊連通的最小道路和,實際為最小生成樹問題
在給出的邊的集合中,已經構成部分連通集,接下來將所有邊放入一個最小堆中,
每次取出權重最小的邊,看邊的兩個端點是否屬于同一個集合,不屬于就加入這條邊,
否則就舍棄,這種貪心的選擇就是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; }
#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; }
| ||||||||||
Constructing RoadsTime 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 | ||||||||||
|
分析:
題目中已經給出一些道路,這些道路將村莊將村莊構成了部分連通集要求的是使全部村莊連通的最小道路和,實際為最小生成樹問題
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 最小生成树的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 51Nod 1298 圆与三角形相交
- 下一篇: HDU1250 Hat's Fibona