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

  • 时间:2016-04-09 00:02:24 星期六

  • 题目编号:[2016-04-09][POJ][1251][Jungle Roads]

  • 题目大意:给定n个城镇的若干条路及其每月维修的代价,问,在所有城市联通的情况下,最少需要多少维修费

  • 分析:

    • 保证边权最小,并且图联通—–>最小生成树
  1. #include <algorithm>
  2. #include <cstring>
  3. #include <cstdio>
  4. using namespace std;
  5. int fa[30];
  6. void ini(int n){
  7. for(int i = 0 ;i <= n ; ++i)fa[i] = i;
  8. }
  9. int fnd(int x){
  10. return x == fa[x]?x:fa[x] = fnd(fa[x]);
  11. }
  12. struct Edge{
  13. int u,v,c;
  14. Edge(int _u = 0,int _v = 0,int _c = 0):u(_u),v(_v),c(_c){}
  15. bool operator < (const Edge & a)const{
  16. return c < a.c;
  17. }
  18. }e[100];
  19. int main(){
  20. int n,k,u,cnt,tmp;char ch[10];
  21. while(~scanf("%d",&n) && n){
  22. cnt = 0;
  23. for(int i = 0 ; i < n - 1 ; ++i){
  24. scanf("%s%d",ch,&tmp);u = ch[0] - ‘A‘;
  25. for(int j = 0 ;j < tmp ; ++j){
  26. scanf("%s%d",ch,&k);
  27. e[cnt++] = Edge(u,ch[0] - ‘A‘,k);
  28. }
  29. }
  30. ini(n);
  31. sort(e,e+cnt);
  32. int cur = 0,f1,f2,ans = 0;
  33. for(int i = 0 ; i < cnt;++i){
  34. f1 = fnd(e[i].u);f2 = fnd(e[i].v);
  35. if(f1 != f2){
  36. fa[f1] = f2;
  37. ans += e[i].c;
  38. ++cur;
  39. }
  40. if(cur == n - 1) break;
  41. }
  42. printf("%d\n",ans);
  43. }
  44. return 0;
  45. }

来自为知笔记(Wiz)

时间: 2024-10-12 19:27:35

[2016-04-09][POJ][1251][Jungle Roads]的相关文章

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

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 (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(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(最小生成树)

题意  有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对数据,每对数据的第一个字母表示与该岛屿连通的岛屿的编号,第二个数字表示要重修

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

【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