Economic times these days are tough, even in Byteland. To reduce the operating costs, the government of Byteland has decided to optimize the road lighting. Till now every road was illuminated all night long, which costs 1 Bytelandian Dollar per meter and day. To save money, they decided to no longer illuminate every road, but to switch off the road lighting of some streets. To make sure that the inhabitants of Byteland still feel safe, they want to optimize the lighting in such a way, that after darkening some streets at night, there will still be at least one illuminated path from every junction in Byteland to every other junction.

What is the maximum daily amount of money the government of Byteland can save, without making their inhabitants feel unsafe?

Input

The input file contains several test cases. Each test case starts with two numbers m and n, the number of junctions in Byteland and the number of roads in Byteland, respectively. Input is terminated by m=n=0. Otherwise, 1 ≤ m ≤ 200000 and m-1 ≤ n ≤ 200000. Then follow n integer triples x, y, z specifying that there will be a bidirectional road between x and y with length z meters (0 ≤ x, y < m and x ≠ y). The graph specified by each test case is connected. The total length of all roads in each test case is less than 2 31.

Output

For each test case print one line containing the maximum daily amount the government can save.

Sample Input

7 11
0 1 7
0 3 5
1 2 8
1 3 9
1 4 7
2 4 5
3 4 15
3 5 6
4 5 8
4 6 9
5 6 11
0 0

Sample Output

51
题解(一个求出修路总和减去最小的花费即可,最小生成树裸题)
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
int pre[200005];
struct node{
int x,y;
int val;
}road[200005];
int find(int x)
{
if(x==pre[x])
return x;
else
{
return pre[x]=find(pre[x]);
}
}
bool merge (int x,int y)
{
int fx=find(x);
int fy=find(y);
if(fx!=fy)
{
pre[fx]=fy;
return true;
}
else
{
return false;
}
}
bool cmp(node x,node y)
{
return x.val<y.val;
}
int main()
{
int m,n;
while(scanf("%d%d",&m,&n))
{
if(m==0&&n==0)
{
break;
}
for(int t=0;t<m;t++)
{
pre[t]=t;
}
long long int sum1=0;
for(int t=0;t<n;t++)
{
scanf("%d%d%d",&road[t].x,&road[t].y,&road[t].val);
sum1+=road[t].val; }
sort(road,road+n,cmp);
long long int sum=0;
int cnt=0;
for(int t=0;t<n;t++)
{
if(cnt==m-1)
break;
if(merge(road[t].x,road[t].y))
{
sum+=road[t].val;
cnt++;
}
}
if(cnt==m-1)
{
printf("%d\n",sum1-sum);
} }
return 0;
}

O - 听说下面都是裸题 (最小生成树模板题)的更多相关文章

  1. 洛谷 P4148 简单题 KD-Tree 模板题

    Code: //洛谷 P4148 简单题 KD-Tree 模板题 #include <cstdio> #include <algorithm> #include <cst ...

  2. 纪中10日T1 2300. 【noip普及组第一题】模板题

    2300. [noip普及组第一题]模板题 (File IO): input:template.in output:template.out 时间限制: 1000 ms  空间限制: 262144 K ...

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

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

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

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

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

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

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

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

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

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

  8. 最小生成树模板题 hpu 积分赛 Vegetable and Road again

    问题 H: Vegetable and Road again 时间限制: 1 Sec 内存限制: 128 MB 提交: 19 解决: 8 题目描述 修路的方案终于确定了.市政府要求任意两个公园之间都必 ...

  9. 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 ...

随机推荐

  1. 在Linux下adb连接不上android手机的终极解决方案

    转自: http://blog.csdn.net/liuqz2009/article/details/7942569 1.做android开发的过程,碰到了Linux下adb识别不了android设备 ...

  2. 通过测试确定GCC中 INT DOUBLE的最大/最小值和精度(DOUBLE)

    INT 确定最大/最小值 由于达到极限之后会变符号,直接循环判断条件即可 DOUBLE确定精度 设置一个DOUBLE变量初始值为1/3.0,每次*10,然后取整数部分,当两次的结果相同时说明已经到最大 ...

  3. Action层, Service层 和 Dao层的功能区分

    Action/Service/DAO简介:  Action是管理业务(Service)调度和管理跳转的. Service是管理具体的功能的. Action只负责管理,而Service负责实施. DAO ...

  4. 35-迷宫寻宝(一)-NYOJ82

    http://acm.nyist.edu.cn/JudgeOnline/problem.php?pid=82 迷宫寻宝(一) 时间限制:1000 ms  |  内存限制:65535 KB 难度:4   ...

  5. 主键primary key和唯一索引unique index

    1)主键一定是唯一性索引,唯一性索引并不一定就是主键. 2)主键就是能够唯一标识表中某一行的属性或属性组,一个表只能有一个主键,但可以有多个候选索引. 3)主键常常与外键构成参照完整性约束,防止出现数 ...

  6. 校园客户端(DR)启动后提示我们缺失packet.dll,无法正常启动(7)

    有的时候校园客户端(DR)启动后提示我们缺失packet.dll,无法正常启动,然而我们重装了客户端后任然不感冒,那么问题来了,问题解决不了往往源于我们对问题的本质不够了解,如果了解问题的本质,那么问 ...

  7. [转载]/etc/security/limits.conf解释及应用

    limits.conf的格式如下: username|@groupname type resource limit username|@groupname:设置需要被限制的用户名,组名前面加@和用户名 ...

  8. Blocks UVA - 10559

    传送门 题目大意 有n个带有颜色的方块,没消除一段长度为x的连续的相同颜色的方块可以得到x^2的分数,让你用一种最优的顺序消除所有方块使得得分最多. 分析 首先不难看出这是一个区间dp,于是我们考虑如 ...

  9. 前端基础 之 CSS

    浏览目录 CSS介绍 CSS语法 CSS的几种引入方式 CSS选择器 CSS属性相关 一.CSS介绍 CSS(Cascading Style Sheet,层叠样式表)定义如何显示HTML元素. 当浏览 ...

  10. 创建Mat对象

    Mat 是一个非常优秀的图像类,它同时也是一个通用的矩阵类,可以用来创建和操作多维矩阵.有多种方法创建一个 Mat 对象. 1.构造函数方法 下面是一个使用构造函数创建对象的例子. 常用的构造函数 2 ...