Ice_cream’s world III

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

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
Author
Wiskey
好久没写代码了,挺简单的一道题,不知道为啥么没人做,就是最基础的最小生成树,这里用的最小生成树的库鲁斯卡尔算法,该算法用的并查集,很基础很重要的算法 ;
附ac代码:
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
struct node
{int start;int end;int cost;
}t[100100];
int per[10001];
int cmp(node a,node b)
{return a.cost<b.cost;
}
int find(int x)
{int r=x;while(r!=per[r])r=per[r];return r;
}
int join(int x,int y)
{int fx=find(x);int fy=find(y);if(fx!=fy){per[fx]=fy;return 1;}return 0;
}
int main()
{int i,j,k,l,m,n;while(scanf("%d%d",&m,&n)!=EOF){for(i=0;i<10001;i++)per[i]=i;for(i=0;i<n;i++)scanf("%d%d%d",&t[i].start,&t[i].end,&t[i].cost);sort(t,t+n,cmp);int ans=0;for(i=0;i<n;i++){if(join(t[i].start,t[i].end))ans+=t[i].cost;}int flag=0;for(i=0;i<m;i++)if(per[i]==i)flag++;if(flag>1)printf("impossible\n\n");elseprintf("%d\n\n",ans);}return 0;
}

杭电2122Ice_cream’s world III相关推荐

  1. 杭电 汉诺塔问题总结

    看了一下杭电的各种汉诺塔问题,遇到些奇奇葩葩的小问题,也有很多很好的思想,比如最后一题,来来回回的颠倒很有意思.总结一下: Pro.ID 1207 :http://acm.hdu.edu.cn/sho ...

  2. HDU1290 献给杭电五十周年校庆的礼物 平面分割空间问题

    点击打开链接 献给杭电五十周年校庆的礼物 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Oth ...

  3. HDU1290 献给杭电五十周年校庆的礼物【水题】

    献给杭电五十周年校庆的礼物 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total ...

  4. 杭电OJ分类题目(3)

    原题出处:HDOJ Problem Index by Type,http://acm.hdu.edu.cn/typeclass.php 杭电OJ分类题目(3) HDU Computational Ge ...

  5. 杭电OJ分类题目(1)

    原题出处:HDOJ Problem Index by Type,http://acm.hdu.edu.cn/typeclass.php 杭电OJ分类题目(1) HDU Introduction HDU ...

  6. HDU 1290 献给杭电五十周年校庆的礼物(空间分割)

    献给杭电五十周年校庆的礼物 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Pr ...

  7. HDU杭电OJ经典100题2000-2099_Java版详细题解(持续更新)

    今年寒假打算用Java把杭电2000-2099全部AC(现在持续更新),如下是题目链接,之后是我的题解,全部做完后我会把所有AC的题解打包上传的 题号 题名 题号 题名 2000 ASCII码排序 2 ...

  8. 杭电OJ第11页2075~2079算法题(C语言)

    目录 2075.A|B? 2076.夹角有多大 2077.汉诺塔IV 2078.复习时间 2079.选课时间 2075.A|B? Problem Description 正整数A是否能被正整数B整除, ...

  9. 【HDU100】杭电入门一百道 C++ 全 题 解

    " Ctrl AC!一起 AC!" 题目集合链接:HDU2000-2099http://acm.hdu.edu.cn/listproblem.php?vol=11 前言 1. 好像 ...

最新文章

  1. JRBeanCollectionDataSource cannot be resolved to a type 的原因
  2. linux脚本case语句,shell中的case语句,数组及函数
  3. Go基础系列:构建go程序
  4. QML基础类型之double
  5. java学习(98):线程join使用中断进行另一个
  6. 带你看清梦饷集团如何成为上海在线新经济四小龙
  7. python中metaclass的理解
  8. Win7登录时出现Group policy client 未启动无法登陆问题的解决方法
  9. AI 外挂!百度 Lens 是如何突破人眼视觉极限的?
  10. Android Preference 的使用
  11. 把.Net开发环境迁移到Linux上去
  12. 2020泰迪杯数据挖掘挑战赛总结(A题)
  13. unity拖拽drag_UGUI事件之Drag拖拽事件
  14. 100 年前科幻小说的概念,创造了今天千亿美元的市场
  15. 文本框仅允许输入大于0的正整数
  16. jqwidgets API
  17. 家电专用降压DC-DC方案PL8310
  18. (FAQ)现购的采购发票保存时提示:超过最大可钩稽数值[金蝶K/3]
  19. vue项目搭建和配置
  20. 【03月13日】建筑材料行业的估值高度排名

热门文章

  1. 【转】位操作基础篇之位操作全面总结
  2. 语音识别——触发字检测
  3. java多线程采集+线程同步-【多线程数据采集之四】
  4. wxWidgets第四课 EVT_LEFT_UP关联鼠标弹起事件不生效
  5. [codevs1378]选课
  6. 学一下HDFS,很不错(大数据技术原理及应用)
  7. JavaScript简介及基础知识(1)
  8. swift(不同设备适配详解)
  9. 民航重组:做大容易做强难
  10. STM32 MDK常见错误与解决