POJ-1251 Jungle Roads---MST裸题(需要编号)

题目链接:

https://vjudge.net/problem/POJ-1251

题目大意:

首先给你一个图,需要你求出最小生成树,输入N个节点,用大写字母表示了节点,然后节点与节点之间有权值。

思路:
这里需要编号,其他的就是模板

  1 #include<iostream>
  2 #include<cstdio>
  3 #include<cstring>
  4 #include<algorithm>
  5 #include<cmath>
  6 #include<queue>
  7 #include<stack>
  8 #include<map>
  9 #include<set>
 10 #include<sstream>
 11 using namespace std;
 12 typedef long long ll;
 13 const int maxn = 1e3 + 10;
 14 const int INF = 1 << 30;
 15 int dir[4][2] = {1,0,0,1,-1,0,0,-1};
 16 int T, n, m;
 17 struct edge
 18 {
 19     int u, v, w;
 20     edge(){}
 21     edge(int u, int v, int w):u(u), v(v), w(w){}
 22     bool operator <(const edge& a)const
 23     {
 24         return w < a.w;
 25     }
 26 };
 27 edge a[maxn];
 28 int par[60], high[60];
 29 //初始化n个元素
 30 void init(int n)
 31 {
 32     for(int i = 0; i < n; i++)
 33     {
 34         par[i] = i;
 35         high[i] = 0;
 36     }
 37 }
 38 //查询树的根
 39 int Find(int x)
 40 {
 41     return par[x] == x ? x : par[x] = Find(par[x]);//路径压缩
 42 }
 43 void unite(int x, int y)
 44 {
 45     x = Find(x);
 46     y = Find(y);
 47     if(x == y)return;
 48     if(high[x] < high[y])par[x] = y;//y的高度高,将x的父节点设置成y
 49     else
 50     {
 51         par[y] = x;
 52         if(high[x] == high[y])high[x]++;
 53     }
 54 }
 55 bool same(int x, int y)
 56 {
 57     return Find(x) == Find(y);
 58 }
 59 void kruskal(int n, int m)//点数n,边数m
 60 {
 61     int sum_mst = 0;//mst权值
 62     int num= 0;//已经选择的边的边数
 63     sort(a, a + m);//边进行排序
 64     init(n);//初始化并查集
 65     for(int i = 0; i < m; i++)
 66     {
 67         int u = a[i].u;
 68         int v = a[i].v;
 69         if(Find(u - 1) != Find(v - 1))//图最开始的下标是1,并查集是0
 70         {
 71             //printf("%d %d %d\n", u, v, a[i].w);
 72             sum_mst += a[i].w;
 73             num++;
 74             unite(u - 1, v - 1);
 75         }
 76         if(num >= n - 1)break;
 77     }
 78     //printf("weight of mst is %d\n", sum_mst);
 79     cout<<sum_mst<<endl;
 80 }
 81 map<char, int>id;
 82 set<char>s;
 83 int getid(char c)
 84 {
 85     if(s.count(c))return id[c];
 86     else
 87     {
 88         s.insert(c);
 89         return id[c] = s.size();
 90     }
 91 }
 92 int main()
 93 {
 94     while(cin >> n && n)
 95     {
 96         char c, d;
 97         id.clear();
 98         s.clear();
 99         int tot = 0;
100         for(int i = 1; i < n; i++)
101         {
102             cin >> c >> m;
103             int u = getid(c), v, w;
104             while(m--)
105             {
106                 cin >> d >> w;
107                 v = getid(d);
108                 a[tot++] = edge(u, v, w);
109             }
110         }
111         kruskal(n, tot);
112     }
113     return 0;
114 }

原文地址:https://www.cnblogs.com/fzl194/p/8727510.html

时间: 2025-01-17 18:44:41

POJ-1251 Jungle Roads---MST裸题(需要编号)的相关文章

ZOJ 1406 POJ 1251 Jungle Roads 丛林中的道路,最小生成树,Kruskal算法

题目链接:ZOJ 1406 POJ 1251 Jungle Roads 丛林中的道路 Jungle Roads Time Limit: 2 Seconds      Memory Limit: 65536 KB The Head Elder of the tropical island of Lagrishan has a problem. A burst of foreign aid money was spent on extra roads between villages some ye

[2016-04-09][POJ][1251][Jungle Roads]

时间:2016-04-09 00:02:24 星期六 题目编号:[2016-04-09][POJ][1251][Jungle Roads] 题目大意:给定n个城镇的若干条路及其每月维修的代价,问,在所有城市联通的情况下,最少需要多少维修费 分析: 保证边权最小,并且图联通-–>最小生成树 #include <algorithm> #include <cstring> #include <cstdio> using namespace std; int fa[30]

POJ 1251 Jungle Roads (最小生成树)

POJ 1251 Jungle Roads Description The Head Elder of the tropical island of Lagrishan has a problem. A burst of foreign aid money was spent on extra roads between villages some years ago. But the jungle overtakes roads relentlessly, so the large road

POJ 1251 Jungle Roads(kruskal)

Submit Status Practice POJ 1251 Description The Head Elder of the tropical island of Lagrishan has a problem. A burst of foreign aid money was spent on extra roads between villages some years ago. But the jungle overtakes roads relentlessly, so the l

POJ 1251 Jungle Roads (prim)

D - Jungle Roads Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u Submit Status Practice POJ 1251 Description The Head Elder of the tropical island of Lagrishan has a problem. A burst of foreign aid money was spent on extr

POJ 1251 Jungle Roads(最小生成树)

题意  有n个村子  输入n  然后n-1行先输入村子的序号和与该村子相连的村子数t  后面依次输入t组s和tt s为村子序号 tt为与当前村子的距离  求链接所有村子的最短路径 还是裸的最小生成树咯 #include<cstdio> #include<cstring> #include<algorithm> using namespace std; const int N=30,M=1000; int par[N],n,m,ans; struct edge{int u

POJ 1251 Jungle Roads

题意:嗯……没看题……看了眼图……求个最小生成树. 解法:kruskal. 代码: #include<stdio.h> #include<iostream> #include<algorithm> #include<string> #include<string.h> #include<math.h> #include<limits.h> #include<time.h> #include<stdlib.

NYOJ 434 &amp;&amp; POJ 1251 Jungle Roads(最小生成树)

链接:click here 题意: 题目大意在相通n个岛屿的所有桥都坏了,要重修,重修每一个桥所用的时间不同,求重修使每个岛屿都间接或直接与其他岛屿相同时所用的的最短时间(只有修完一个桥后才可修下一个桥).简言之就是求最小生成树. 对于数据,数据输入的第一行n代表岛屿的个数,当为0是结束程序,接着n-1行开始时为这岛屿的编号,用大写字母表示,接着是一个整数m,表示与该岛屿连接的字典序大于该岛屿编号的个数,然后该行输入m对数据,每对数据的第一个字母表示与该岛屿连通的岛屿的编号,第二个数字表示要重修

【POJ】1251 Jungle Roads

题目链接:http://poj.org/problem?id=1251 题意:n个村庄字母标号,每个字母后跟m个字母,表示该字母到mi的距离.求构建所有村庄道路的最短距离. 题解:最小生成树裸题.注意输入. 代码: 1 #include<iostream> 2 #include<cstring> 3 #include<cstdio> 4 using namespace std; 5 const int maxn = 30; 6 const int inf = 0x3f3

HDU 1301 &amp;POJ 1215 Jungle Roads【最小生成树,Prime算法+Kruskal算法】

Jungle Roads Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 6737    Accepted Submission(s): 4893 Problem Description The Head Elder of the tropical island of Lagrishan has a problem. A burst o