Ice_cream’s world III

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 802    Accepted Submission(s): 274
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 104 0
Sample Output
10impossible
#include <stdio.h>
#include <string.h>
#include <algorithm>
#define maxn 1002
#define maxm 10002
using std::sort;int pre[maxn], count, len;
struct Node {int u, v, cost;
} E[maxm];int ufind(int k)
{int a = k, b;while(pre[k] != -1) k = pre[k];while(a != k) {b = pre[a];pre[a] = k;a = b;}return k;
}bool cmp(Node a, Node b) {return a.cost < b.cost;
}void Kruskal(int m)
{memset(pre, -1, sizeof(pre));int u, v, i; len = 0;for(i = 0; i < m && count > 1; ++i) {u = ufind(E[i].u);v = ufind(E[i].v);if(u != v) {len += E[i].cost;pre[v] = u;--count;}}if(1 == count) printf("%d\n\n", len);else printf("impossible\n\n");
}int main()
{int n, m, i;while(scanf("%d%d", &n, &m) == 2) {count = n;for(i = 0; i < m; ++i)scanf("%d%d%d", &E[i].u, &E[i].v, &E[i].cost);sort(E, E + m, cmp);Kruskal(m);}return 0;
}

HDU2122 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. 杭电2122Ice_cream’s world III

    Ice_cream's world III Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Ot ...

  3. 【转】并查集MST题集

    转自:http://blog.csdn.net/shahdza/article/details/7779230 [HDU] 1213 How Many Tables 基础并查集★ 1272 小希的迷宫 ...

  4. ACM比赛经验、刷题记录及模板库总结(更新中)

    前言 本文所提及的部分题目代码,可以在我的Github上找到 第一部分 经验分享及感受 第二部分 刷题记录 一.基础算法&程序语言 //strlen()函数的复杂度是O(n)要小心 //截取字 ...

  5. android资源文件下的图片绝对路径,如何获取drawable目录下的图片绝对路径

    Public DNS (公共域名解析服务) 114DNS 服务IP:114.114.114.114, 114.114.115.115 拦截 钓鱼病毒木马网站:114.114.114.119, 114. ...

  6. HDU2121:Ice_cream’s world II (虚根+有向图最小生成树)

    Ice_cream's world II Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Oth ...

  7. BZOJ 2139 road(构造,最小生成树)【BZOJ 修复工程】

    整理的算法模板合集: ACM模板 点我看算法全家桶系列!!! 实际上是一个全新的精炼模板整合计划 题目链接 https://hydro.ac/d/bzoj/p/2139 是 hydro 的 BZOJ ...

  8. 生成树、最小生成树的一些性质以及邻域的概念

    设一个无向连通图G=(V,E),顶点个数记作m,边的数量为n.其对应的最小生成树记作f. 则生成树T的性质有: 边数量为m-1 没有圈 加入任何一条边都会形成一个圈 对于一颗生成树T,定义 操作:在其 ...

  9. 遗传算法应用(实例详细演示最小生成树的prufer编码和Cayley定理)

    prufer编码:用n-2位自然数唯一的表达出一棵n个节点的生成树.而且两者相互可逆,即给定一颗生成树的连接方式,可以唯一确定这棵树的编码. Cayley定理:n个顶点的完全图中有nn−2n^{n-2 ...

  10. 2021牛客多校3 - Black and white(思维+最小生成树)

    题目链接:点击查看 题目大意:给出一个 n∗mn*mn∗m 的矩阵,初始时都是白色,可以花费掉 cost[i][j]cost[i][j]cost[i][j] 将格子 (i,j)(i,j)(i,j) 染 ...

最新文章

  1. HttpUnit学习笔记
  2. node版本升级for ubuntu
  3. 查看python版本号-怎么查看python版本号
  4. Spark广播变量实现原理及基础编程
  5. html5 网页桌面图标,打开控制面板是网页,而且桌面上很多图标变成网页
  6. java io类库,Java利用io类库对各种文件的操作详解
  7. 设置Eclipse中的字符集为UTF-8
  8. 60、date的使用
  9. python批量_Python学习第九十三天:Python批量处理图片
  10. 小红书疑似被下架 官方回应正在积极沟通
  11. java 错误代码2503_错误代码:11300001 数据集配置错误
  12. 计算机自带的画图软件工具介绍,电脑自带画图软件
  13. Mysql集群方案简介
  14. iperf 的下载和使用
  15. iit delhi_向印度最聪明的人学习—这里有来自IIT的300项免费课程即将开始
  16. 制造业干货,只需3步,快速提高产品良率!
  17. 深度学习笔记——情感分析
  18. PowerShell 实现企业微信机器人推送消息
  19. (电脑软件启动无法验证)win10专业版如何解决无法验证发布者
  20. ps中如何把图片变白底

热门文章

  1. BFS(广度优先算法)
  2. Android实战简易教程五(ListView用法研究)
  3. 给element添加自定义图标
  4. 阿里云ASR 语音识别接口调用
  5. WP 手机Lumia 820 锁屏密码的POJI研究
  6. eclipse安装翻译插件(通用)
  7. PayPal美元提现到国内银行卡教程!
  8. 35岁高以翔过劳猝死:人生无常,求各位项目经理少加点班吧
  9. 2.14 视图的分页预览以及分页符的使用 [原创Excel教程]
  10. 爱快软路由上装linux,爱快软路由系统利用自带虚拟机安装系统