问题 H: Vegetable and Road again

时间限制: 1 Sec 内存限制: 128 MB

提交: 19 解决: 8

题目描述

修路的方案终于确定了。市政府要求任意两个公园之间都必须实现公路交通(并不一定有直接公路连接,间接公路相连也可以)。但是考虑到经济成本,市政府希望钱花的越少越好。

你能帮助Vegetable找到给出的修路方案所需的最少花费吗?

输入

有T组测试数据。

每组包含一组N(0<n<=100)和M,N表示有N个公园,M表示这N个公园间的M条路。

接下来给出M行,每行包括A,B, C。表示A和B之间修公路需要花费C元。

输出

若给出的方案可行,输出该方案最小需要的花费,若给出的方案不可行,输出Wrong。

样例输入

1
4 3
1 2 1
2 3 2
3 4 3

样例输出

6
#include<cstdio>
#include<algorithm>
using namespace std; const int maxn = ; int f[maxn]; struct edge
{
int from;
int to;
int cost;
friend bool operator < (edge a,edge b)
{
return a.cost < b.cost;
}
}e[maxn]; void ufs(int n)
{
for(int i = ; i <= n; i++)
f[i] = i;
} int findd(int x)
{
if(f[x] == x)
return x;
else return findd(f[x]);
} void merger(int x,int y)
{
int fx = findd(x);
int fy = findd(y);
if(fx != fy)
f[fy] = fx;
} int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int n,m;
scanf("%d%d",&n,&m);
ufs(n);
int cnt = ;
for(int i = ; i < m; i++)
scanf("%d%d%d",&e[i].from,&e[i].to,&e[i].cost);
sort(e,e + m);
for(int i = ; i < m; i++)
{
if(findd(e[i].from) != findd(e[i].to))
{
merger(e[i].from,e[i].to);
cnt += e[i].cost;
}
}
int flag = ;
for(int i = ; i < n; i++)
{
if(findd(f[i]) != findd(f[i + ]))
{
flag = ;
printf("Wrong\n");
break;
}
}
if(flag)
printf("%d\n",cnt);
}
return ;
}

最小生成树模板题 hpu 积分赛 Vegetable and Road again的更多相关文章

  1. O - 听说下面都是裸题 (最小生成树模板题)

    Economic times these days are tough, even in Byteland. To reduce the operating costs, the government ...

  2. POJ 1258:Agri-Net Prim最小生成树模板题

    Agri-Net Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 45050   Accepted: 18479 Descri ...

  3. POJ 1789 Truck History (Kruskal最小生成树) 模板题

    Description Advanced Cargo Movement, Ltd. uses trucks of different types. Some trucks are used for v ...

  4. 最小生成树模板题-----P3366 【模板】最小生成树

    题目描述 如题,给出一个无向图,求出最小生成树,如果该图不连通,则输出orz 输入格式 第一行包含两个整数N.M,表示该图共有N个结点和M条无向边.(N<=5000,M<=200000) ...

  5. 最小生成树模板题POJ - 1287-prim+kruskal

    POJ - 1287超级模板题 大概意思就是点的编号从1到N,会给你m条边,可能两个点之间有多条边这种情况,求最小生成树总长度? 这题就不解释了,总结就算,prim是类似dijkstra,从第一个点出 ...

  6. poj 1251 poj 1258 hdu 1863 poj 1287 poj 2421 hdu 1233 最小生成树模板题

    poj 1251  && hdu 1301 Sample Input 9 //n 结点数A 2 B 12 I 25B 3 C 10 H 40 I 8C 2 D 18 G 55D 1 E ...

  7. POJ1258:Agri-Net(最小生成树模板题)

    http://poj.org/problem?id=1258 Description Farmer John has been elected mayor of his town! One of hi ...

  8. POJ 1287 Networking (最小生成树模板题)

    Description You are assigned to design network connections between certain points in a wide area. Yo ...

  9. 继续畅通工程--hdu1879(最小生成树 模板题)

    http://acm.hdu.edu.cn/showproblem.php?pid=1879 刚开始么看清题  以为就是n行  后来一看是n*(n-1)/2行   是输入错误  真是够够的 #incl ...

随机推荐

  1. 如何将本地项目上传至GitHub(so easy!!!)

    如何将本地项目上传至GitHub 首先你需要一个github账号,所有还没有的话先去注册吧! https://github.com/ 我们使用git需要先安装git工具,这里给出下载地址,下载后一路直 ...

  2. 非root用户在80端口运行nginx

    一般情况下没有这种需求,但对于强迫症患者来说,还是完整的走了一把. 普通用户是不允许使用1024以下端口的,所以此次操作仍然需要root权限来进行配置.而且由于使用了root安装,因此nginx用户仍 ...

  3. JAVA获取apk包的package和launchable-activity名称(完善成EXE版)

    出来混迟早是要还的. 在这一篇中https://www.cnblogs.com/sincoolvip/p/5882817.html,只是简单讲了一下获取apk包的package和launchable- ...

  4. [原创]Fashion汽车定位器拆解

    随着共享单车的爆发增长,定位方案被彻底激活.当下主流的共享单车都采用了MTK2503的方案(后续再详细分解),本文针对某商城热卖的汽车定位器进行拆解分析. 第一部分,定位器外观. 第二部分,拆解开壳, ...

  5. GraphX学习笔记——可视化

    首先自己造了一份简单的社交关系的图 第一份是人物数据,id和姓名,person.txt 1 孙俪 2 邓超 3 佟大为 4 冯绍峰 5 黄晓明 6 angelababy 7 李冰冰 8 范冰冰 第二份 ...

  6. Uncaught InvalidStateError: Failed to set the 'value' property on 'HTMLInputElement': This input element accepts a filename, which may only be programmatically set to the empty string.

    使用 HTML5 的图片上传api的时候报如下错误: Uncaught InvalidStateError: Failed to set the 'value' property on 'HTMLIn ...

  7. jquery面试(2)

    DOM操作——怎样添加.移除.移动.复制.创建和查找节点? (1)创建新节点 createDocumentFragment() //创建一个DOM片段 createElement() //创建一个具体 ...

  8. 14 CSS题目附答案

    转载自公众号:web前端开发  原文题目:45道CSS基础面试题(附答案) 1. 介绍一下标准的CSS的盒子模型?与低版本IE的盒子模型有什么不同的? 标准盒子模型:宽度=内容的宽度(content) ...

  9. 【python】——三级菜单

    作业需求: 打印三级菜单 可返回上一级 可随时退出程序 #!/usr/bin/env python # -*- coding:utf-8 -*- #Author: __Json.Zzgx__ menu ...

  10. js中级小知识1

    首先我们复习之前的小知识,本期博客与之前有关 js数据类型 基本数据类型:string    undefined         null         boolean          numbe ...