Ice_cream’s world III

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 1450    Accepted Submission(s): 496

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
 
Recommend
威士忌   |   We have carefully selected several similar problems for you:  2120 2121 2119 2129 2118 

#include<stdio.h>
#include<string.h>
#include<iostream>
#include<queue>
#include<algorithm>
using namespace std;
int pre[10010];
struct node
{
int u,v,val;
}p[10010];
int m,n;
void init()
{
for(int i=0;i<10010;i++)
pre[i]=i;
}
int cmp(node s1,node s2)
{
return s1.val<s2.val;
}
int find(int x)
{
int r=x;
while(r!=pre[r])
r=pre[r];
while(x!=r)
{
int t=pre[x];
pre[x]=r;
x=t;
}
return r;
}
int join(int x,int y)
{
int fx=find(x);
int fy=find(y);
if(fx!=fy)
{
pre[fx]=fy;
return 1;
}
return 0;
}
int main()
{
while(cin>>n>>m)
{
init();
for(int i=0;i<m;i++)
cin>>p[i].u>>p[i].v>>p[i].val;
sort(p,p+m,cmp);
int sum=0;
for(int i=0;i<m;i++)
{
if(join(p[i].u,p[i].v))
sum+=p[i].val;
}
int flog=0;
int gen=0;
for(int i=0;i<n;i++)
{
if(pre[i]==i)
gen++;
if(gen>1)
flog=1;
}
if(flog)
printf("impossible\n\n");
else
printf("%d\n\n",sum); }
return 0;
}

hdoj--2122--Ice_cream’s world III(克鲁斯卡尔)的更多相关文章

  1. hdoj 2122 Ice_cream’s world III【最小生成树】

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

  2. hdoj 2122 Ice_cream’s world III

    并查集+最小生成树 Ice_cream’s world III Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 ...

  3. HDU 2122 Ice_cream’s world III【最小生成树】

    解题思路:基础的最小生成树反思:不明白为什么i从1开始取,就一直WA,难道是因为村庄的编号是从0开始的吗 Ice_cream’s world III Time Limit: 3000/1000 MS ...

  4. 图的生成树(森林)(克鲁斯卡尔Kruskal算法和普里姆Prim算法)、以及并查集的使用

    图的连通性问题:无向图的连通分量和生成树,所有顶点均由边连接在一起,但不存在回路的图. 设图 G=(V, E) 是个连通图,当从图任一顶点出发遍历图G 时,将边集 E(G) 分成两个集合 T(G) 和 ...

  5. 贪心算法(Greedy Algorithm)之最小生成树 克鲁斯卡尔算法(Kruskal&#39;s algorithm)

    克鲁斯卡尔算法(Kruskal's algorithm)是两个经典的最小生成树算法的较为简单理解的一个.这里面充分体现了贪心算法的精髓.大致的流程能够用一个图来表示.这里的图的选择借用了Wikiped ...

  6. Eddy's picture(prime+克鲁斯卡尔)

    Eddy's picture Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Tota ...

  7. Ice_cream’s world III(prime)

    Ice_cream’s world III Time Limit : 3000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Othe ...

  8. 【类克鲁斯卡尔做法+枚举最小边】【HDU1598】【find the most comfortable road】

    题意:  给你一个图,有边权,K个询问:u到v 的路径中   边权最大值-边权最小值的最小值是多少 http://acm.hdu.edu.cn/showproblem.php?pid=1598 题解( ...

  9. 克鲁斯卡尔(Kruskal)算法

    # include <stdio.h> # define MAX_VERTEXES //最大顶点数 # define MAXEDGE //边集数组最大值 # define INFINITY ...

随机推荐

  1. Html Ajax上传文件,form表单下载文件

    Html中的代码: <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type&quo ...

  2. AJAX异步删除操作

    @Ajax.ActionLink("删除", "Delete", new {id = user.Id}, ajaxOption) @{ var ajaxOpti ...

  3. Unity3d 销毁

    using UnityEngine; using System.Collections; public class destroy : MonoBehaviour { void Start () { ...

  4. idiom的学习笔记(一)、三栏布局

    三栏布局左右固定,中间自适应是网页中常用到的,实现这种布局的方式有很多种,这里我主要写五种.他们分别是浮动.定位.表格.flexBox.网格. 在这里也感谢一些老师在网上发的免费教程,使我们学习起来更 ...

  5. QS之vsim

    vsim - The vsim command invokes the VSIM simulator -L <library_name> … (optional) Specifies th ...

  6. matlab中的@函数

    原文链接:http://blog.sina.com.cn/s/blog_5e73a8fc0100t9yg.html 这是个函数句柄 @(x,y) 表示未知数是x和y punct     - Funct ...

  7. ubuntu 搭建Mercurial 服务(nginx)

    ubuntu 搭建Mercurial 服务(nginx) 环境:ubuntu 12.05  Mercurial 步骤: (1)安装nginx 和 Mercurial: sudo apt-get ins ...

  8. (转)RabbitMQ学习之消息可靠性及特性

    http://blog.csdn.net/zhu_tianwei/article/details/53971296 下面主要从队列.消息发送.消息接收方面了解消息传递过的一些可靠性处理. 1.队列 消 ...

  9. SQL数据查询2

    USE h CREATE TABLE zy1( empno INT, ename ), job ), mgr INT, hiredate DATE, sal DOUBLE, COOM DOUBLE, ...

  10. 用Navicat Prenium12连接Oracle数据库(oracle11g版本)时报错ORA-28547:connection to server failed,probable Oracle Net admin error.解决办法

    上网一查原来是oci.dll版本不对.因为Navicat是通过Oracle客户端连接Oracle服务器的,Oracle的客户端分为两种,一种是标准版,一种是简洁版,即Oracle Install Cl ...