O - 听说下面都是裸题 (最小生成树模板题)
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 - 听说下面都是裸题 (最小生成树模板题)的更多相关文章
- 洛谷 P4148 简单题 KD-Tree 模板题
Code: //洛谷 P4148 简单题 KD-Tree 模板题 #include <cstdio> #include <algorithm> #include <cst ...
- 纪中10日T1 2300. 【noip普及组第一题】模板题
2300. [noip普及组第一题]模板题 (File IO): input:template.in output:template.out 时间限制: 1000 ms 空间限制: 262144 K ...
- POJ 1258:Agri-Net Prim最小生成树模板题
Agri-Net Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 45050 Accepted: 18479 Descri ...
- POJ 1789 Truck History (Kruskal最小生成树) 模板题
Description Advanced Cargo Movement, Ltd. uses trucks of different types. Some trucks are used for v ...
- 最小生成树模板题POJ - 1287-prim+kruskal
POJ - 1287超级模板题 大概意思就是点的编号从1到N,会给你m条边,可能两个点之间有多条边这种情况,求最小生成树总长度? 这题就不解释了,总结就算,prim是类似dijkstra,从第一个点出 ...
- POJ1258:Agri-Net(最小生成树模板题)
http://poj.org/problem?id=1258 Description Farmer John has been elected mayor of his town! One of hi ...
- 最小生成树模板题-----P3366 【模板】最小生成树
题目描述 如题,给出一个无向图,求出最小生成树,如果该图不连通,则输出orz 输入格式 第一行包含两个整数N.M,表示该图共有N个结点和M条无向边.(N<=5000,M<=200000) ...
- 最小生成树模板题 hpu 积分赛 Vegetable and Road again
问题 H: Vegetable and Road again 时间限制: 1 Sec 内存限制: 128 MB 提交: 19 解决: 8 题目描述 修路的方案终于确定了.市政府要求任意两个公园之间都必 ...
- 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 ...
随机推荐
- ThinkPad E431按F1后直接进入系统无法进入BIOS
联想的ThinkPad系列笔记本一般是按F1进如BIOS的,但是由于现在联想的笔记本多数都是预装Win 8或者更高版本的系统,所以有时候就没办法直接按F1进去BIOS.其原因是因为Win 8或者更高版 ...
- [Python Study Notes]一个简单的区块链结构(python 2.7)
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ...
- day70-oracle 12-触发器
查询是没有触发器的.trigger是一个数据库的对象.PL/SQL程序是在我插入之前执行还是在插入之后执行?触发器类似于java中的监听器. 监听插入操作,执行一段PLSQL程序. 禁止在非工作时间插 ...
- 思考题-关于CSS(转)
dl, dt, dd三个标签浏览器默认margin值多少?是否有标签默认文字粗体? line-height:150%和line-height:1.5的区别是? float为何会让外部容器高度塌陷?这是 ...
- ruby 访问权限
##################### # 访问权限 ##################### class HeidSoft ##默认方法 def method1 ##### end prote ...
- ZBar开发详解
博客转载自:https://blog.csdn.net/skillcollege/article/details/38855023 什么是ZBar? ZBar是一个开源库,用于扫描.读取二维码和条形码 ...
- R语言输出pdf时,中文乱码处理
本文转载自:https://blog.csdn.net/hongweigg/article/details/47907555 1.使用基础包,使用函数pdf()输出 在使用pdf()函数时,要输出中文 ...
- bzoj1735 [Usaco2005 jan]Muddy Fields 泥泞的牧场
传送门 分析 我们知道对于没有障碍的情况就是将横轴点于纵轴点连边 于是对于这种有障碍的情况我们还是分横轴纵轴考虑 只不过对于有障碍的一整条分为若干个无障碍小段来处理 然后将标号小段连边,跑最大匹配即可 ...
- Entity Framework Tutorial Basics(33):Spatial Data type support in Entity Framework 5.0
Spatial Data type support in Entity Framework 5.0 MS SQL Server 2008 introduced two spatial data typ ...
- Person的delete请求--------详细过程
首先,数据库的增删改查都是在PersonRepository中实现,因此,直接进入PersonRepository,找到其父类,搜索delete. @Override @TransactionalMe ...