Ice_cream’s world III

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 575    Accepted Submission(s): 184

Problem Description
ice_cream’s world becomes stronger and stronger; every road is built as undirected. The queen enjoys traveling around her world; the queen’s requirement is like II problem, beautifies the roads, by which there are some ways from every city to the capital. The project’s cost should be as less as better.
Input
Every case have two integers N and M (N<=1000, M<=10000) meaning N cities and M roads, the cities numbered 0…N-1, following N lines, each line contain three integers S, T and C, meaning S connected with T have a road will cost C.
Output
If Wiskey can’t satisfy the queen’s requirement, you must be output “impossible”, otherwise, print the minimum cost in this project. After every case print one blank.
Sample Input
2 1
0 1 10
4 0

Sample Output
10
impossible

Author
Wiskey
Source
HDU 2007-10 Programming Contest_WarmUp
Recommend
威士忌
思路:Kruskal
代码:
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <cmath>
#include <algorithm>
using namespace std;
const int MAXN = 10060;
int map[1100];
int n,m;
int the_last_flag;
int a,b,how_long;
struct Node
{int first,end;int value;int select;
}Edge[MAXN];
int find(int x)
{while(x != map[x]){x = map[x];}return x;
}
void Merge(int x,int y)
{int p = find(x);int q = find(y);if(p < q)map[q] = p;elsemap[p] = q;
}
bool cmp(Node a,Node b)
{if(a.value != b.value)return a.value < b.value;if(a.first != b.first)return a.first < b.first;return a.end < b.end;
}
void Kruskal(Node *Edge,int n,int m)
{the_last_flag = 0;sort(Edge + 1,Edge + m + 1,cmp);for(int i = 1;i <= m;i ++){if(the_last_flag == n - 1)break ;int x = find(Edge[i].first);int y = find(Edge[i].end);if(x != y){Merge(x,y);Edge[i].select = true;the_last_flag ++;}}
}
int main()
{while(~scanf("%d%d",&n,&m)){for(int i = 1;i <= n;i ++)map[i] = i;memset(Edge,0,sizeof(Edge));for(int i = 1;i <= m;i ++){scanf("%d%d%d",&a,&b,&how_long);a ++;b ++;Edge[i].first = a;Edge[i].end = b;Edge[i].value = how_long;}Kruskal(Edge,n,m);if(the_last_flag != n - 1)printf("impossible\n");else{int the_last_sum = 0;for(int i = 1;i <= m;i ++){if(Edge[i].select == 1)the_last_sum += Edge[i].value;}printf("%d\n",the_last_sum);}printf("\n");}return 0;
}

转载于:https://www.cnblogs.com/GODLIKEING/p/3376849.html

HDU 2122 Ice_cream’s world III相关推荐

  1. HDU - 2122 Ice_cream’s world III

    ice_cream's world becomes stronger and stronger; every road is built as undirected. The queen enjoys ...

  2. HDU——2064汉诺塔III

    汉诺塔III Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Sub ...

  3. HDU 2064:汉诺塔III

    汉诺塔III Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Sub ...

  4. HDU 2121 Ice_cream’s world II(最小树形图+虚根)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2121        题意是有n个点(0--n-1),m条有向边,问以那个点作为起点使得最小生成树的权值 ...

  5. HDU 5266 pog loves szh III【LCA RMQ】

    B - pog loves szh III 题目:添加链接描述 题意:找出区域l到r的LCA->找l和r的LCA 分析: 链式前向星存树,先用dfs处理结点倍增关系. 然后从循环处理较深结点,直 ...

  6. HDU 1028 HDU Ignatius and the Princess III

    简单的钱币兑换问题,就是钱的种类多了一点,完全背包. #include<cstdio> #include<cstring> int main () {int i,j,dp[12 ...

  7. hdu 2064汉诺塔III 递推

    汉诺塔递推题,比汉诺塔多了一个限制条件,盘子只允许在相邻的柱子之间移动. 分析: 第1步:初始状态: 第2步:把上面的n-1个盘移到第3号杆上: 第3步:把第n个盘从1移到2: 第4步:把前n-1个从 ...

  8. HDU 2121 Ice_cream’s world II (最小树形图+虚根)

    题意:有n个点(0~n-1),m条有向边,问以哪个点作为起点使得最小生成树的权值最小,如果可以构成输出权值和顶点编号,否则输出impossible. 题解:最小树形图+虚根 还好做了这题,板子有点问题 ...

  9. HDU - 2121 Ice_cream’s world II(朱刘算法+虚根)

    题目大意:给你N个点,M条有向边,问以哪个点为根结点时,能使最小生成树总权值达到最小,输出总权值和根. 如果构不成最小生成树,另外输出 解题思路:这题很巧妙,暴力枚举的话,肯定TLE,所以,这题就需要 ...

  10. HDU 2064 汉诺塔III(递归)

    题目链接 Problem Description 约19世纪末,在欧州的商店中出售一种智力玩具,在一块铜板上有三根杆,最左边的杆上自上而下.由小到大顺序串着由64个圆盘构成的塔.目的是将最左边杆上的盘 ...

最新文章

  1. flutter 按钮_flutter好用的轮子推荐二-点赞按钮动画
  2. HttpSession常见问题
  3. 用于在公网环境下测试的Telnet/SSH服务器
  4. MySQL事务隔离级别和实现原理
  5. C语言实现小时候经常做的智力测试题
  6. 老男孩linux培训期中学生作业文档目录展示
  7. gin框架长连接_一个TCP长连接设备管理后台工程(一)
  8. C# 使用Timer控件设置时间间隔
  9. java静态页面我都做不出_Java高并发:静态页面生成方案
  10. 想成为一个高效的Web开发者吗?来看看大牛分享的经验吧
  11. 快速部署简单私有云CloudStack(下)
  12. LoadRunner压力测试:测试报告
  13. CGAL库的安装及示例代码的编译
  14. 東京音頭 (东京音头) 歌词翻译
  15. c语言多态性编码图形,C和C++经典著作 C专家编程Expert C Programming Deep C Secrets pdf...
  16. 华为荣耀X1相机或图库图标被删除后的恢复方法(不需要恢复出厂设置)!
  17. 可可英语奇文老师 中高级词汇记忆方法(免费下载)
  18. 燕十八PHP公益课堂学习笔记
  19. 测试硬盘,ssd,优盘读取速度
  20. No module named 'gensim'

热门文章

  1. KMeans算法流程
  2. 1寸、小2寸、2寸证件照片是多大尺寸?
  3. php 图片底色替换,手把手教你替换相片中的颜色(给相片换底色)
  4. wps怎么删除空白页?你学会了吗?
  5. 深刻分析有效值与均方根
  6. 漫谈多模光纤类型:OM1、OM2、OM3、OM4、OM5,深度好文,值得阅读
  7. 全自动高清录播服务器,常态化高清录播服务器 高清全自动录播系统
  8. java开发自学怎么样_Java工程师的薪资待遇怎么样,自学Java开发可以吗?
  9. 链表初始化typedef struct LNode{}LNode,*linklist的理解
  10. 与计算机专业的社会学的论文,计算机专业研究生论文致谢词