问题 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. dom4j 简单使用

    1,需要用到dom4j的jar包.为了打开xml方便,设计一个简单的封装类. package cn.com.gtmc.glaf2.util; import java.io.File; import j ...

  2. android手机测试中如何查看内存泄露

    (一) 生成.hprof文件生成.hprof 文件的方法有很多,而且Android 的不同版本中生成.hprof 的方式也稍有差别,我使用的版本的是2.1,各个版本中生成.prof 文件的方法请参考: ...

  3. hdoj:2061

    #include <iostream> #include <string> using namespace std; int main() { int n,k; double ...

  4. Oracle分析函数-first_value()和last_value()

    first_value()和last_value()字面意思已经很直观了,取首尾记录值.例:查询部门最早发生销售记录日期和最近发生的销售记录日期 select dept_id ,sale_date , ...

  5. 每天学点Linux-切割命令split

    一种常见的需求是,有一个比较大的文件,需要把它切割成比较小的几个文件,在Linux系统中你就可以使用Split命令了.Split命令可以将一个大的文件按照文件大小或者行数切割成小文件.Split命令的 ...

  6. 手写LRU算法

    import java.util.LinkedHashMap; import java.util.Map; public class LRUCache<K, V> extends Link ...

  7. IDEA-各模块间引用出现问题的解决方法

    1 点击项目右上角的Project Structure 2 选择Modules->父项目->点击右上角的加号->添加需要依赖的模块

  8. apache 2.4.23 只能本地访问,其他用户不能访问,提示You don't have permission to access

    这个版本的httpd.conf的配置方法跟原版本的设置不一样了. 需要在目录安全配置中 修改为 Require all granted 比如  把Require local 修改为Require al ...

  9. G - Throw nails

    来源hde4393 The annual school bicycle contest started. ZL is a student in this school. He is so boring ...

  10. 6.2 Pandora 实操 - 数据展示

    具体每个图可以到蛋蛋团仪表盘里,查看配置即可.过程中没有截图记下来. 配置方法,建议参考已存在的图表参数,照样配,会比较快. 配置网站访问量 配图结果 参考 报表说明