Dark roads(kruskal)
Dark roads
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other)
Total Submission(s) : 7 Accepted Submission(s) : 2
What is the maximum daily amount of money the government of Byteland can save, without making their inhabitants feel unsafe?
#include<string.h>
#include<stdio.h>
#include<algorithm>
using namespace std;
const int MAXN=;
struct Node{
int s,e,dis;
};
Node dt[MAXN];
int cmp(Node a,Node b){
return a.dis<b.dis;
}
int pre[MAXN],mi,tot;
int find(int x){
int r=x;
while(r!=pre[r])r=pre[r];
int i=x,j;
while(i!=r)j=pre[i],pre[i]=r,i=j;
return r;
}
int merge(Node a){
int f1,f2;
if(pre[a.s]==-)pre[a.s]=a.s;
if(pre[a.e]==-)pre[a.e]=a.e;
f1=find(a.s);f2=find(a.e);
if(f1!=f2)pre[f1]=f2,mi+=a.dis;
}
int main(){
int N,M;
while(~scanf("%d%d",&N,&M),N||M){mi=tot=;
memset(pre,-,sizeof(pre));
for(int i=;i<M;i++)scanf("%d%d%d",&dt[i].s,&dt[i].e,&dt[i].dis),tot+=dt[i].dis;
sort(dt,dt+M,cmp);
for(int i=;i<M;i++){
merge(dt[i]);
}
//printf("%d %d \n",tot,mi);
printf("%d\n",tot-mi);
}
return ;
}
Dark roads(kruskal)的更多相关文章
- HDU 2988 Dark roads(kruskal模板题)
Dark roads Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- c/c++ 用克鲁斯卡尔(kruskal)算法构造最小生成树
c/c++ 用克鲁斯卡尔(kruskal)算法构造最小生成树 最小生成树(Minimum Cost Spanning Tree)的概念: 假设要在n个城市之间建立公路,则连通n个城市只需要n-1条线路 ...
- 最小生成树之克鲁斯卡尔(Kruskal)算法
学习最小生成树算法之前我们先来了解下 下面这些概念: 树(Tree):如果一个无向连通图中不存在回路,则这种图称为树. 生成树 (Spanning Tree):无向连通图G的一个子图如果是一颗包含G的 ...
- 克鲁斯卡尔(Kruskal)算法
概览 相比于普里姆算法(Prim算法),克鲁斯卡尔算法直接以边为目标去构建最小生成树.从按权值由小到大排好序的边集合{E}中逐个寻找权值最小的边来构建最小生成树,只要构建时,不会形成环路即可保证当边集 ...
- POJ 1251 Jungle Roads (prim)
D - Jungle Roads Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%I64d & %I64u Su ...
- 最小生成树练习2(Kruskal)
两个BUG鸣翠柳,一行代码上西天... hdu4786 Fibonacci Tree(生成树)问能否用白边和黑边构成一棵生成树,并且白边数量是斐波那契数. 题解:分别优先加入白边和黑边,求出生成树能包 ...
- 最小生成树(Kruskal)
题目描述 如题,给出一个无向图,求出最小生成树,如果该图不连通,则输出orz 输入输出格式 输入格式: 第一行包含两个整数N.M,表示该图共有N个结点和M条无向边.(N<=5000,M<= ...
- WUSTOJ 1346: DARK SOULS(Java)并查集
题目链接:1346: DARK SOULS 并查集系列:WUSTOJ 1319: 球(Java)并查集 Description CQ最近在玩一款游戏:DARK SOULS,这是一款以高难度闻名的硬派动 ...
- 这是一篇每个人都能读懂的最小生成树文章(Kruskal)
本文始发于个人公众号:TechFlow,原创不易,求个关注 今天是算法和数据结构专题的第19篇文章,我们一起来看看最小生成树. 我们先不讲算法的原理,也不讲一些七七八八的概念,因为对于初学者来说,看到 ...
随机推荐
- Linux下静态编译Qt程序
一般情况下,我们用Qt编译出来的程序是要依赖于系统Qt库的,也就是这个程序移到别的没有安装Qt库的系统上是不能使用的.会提示缺少……库文件之类的错误.这就是动态编译的结果. 但是如果我们想编译一个程序 ...
- flume【源码分析】分析Flume的启动过程
h2 { color: #fff; background-color: #7CCD7C; padding: 3px; margin: 10px 0px } h3 { color: #fff; back ...
- mysql 1449 : The user specified as a definer ('montor'@'%') does not exist
grant all privileges on *.* to root@"%" identified by "."; flush privileges;
- mode(思维,注意内存)
mode Time Limit:1000MS Memory Limit:1024KB 64bit IO Format:%lld & %llu Submit Status Pra ...
- Dictionary到List转换中的性能问题 转
本文来自:http://www.cnblogs.com/353373440qq/p/3488367.html 在应用泛型中,我们经常使用Dictionary,经常会用到Dictionary到List的 ...
- Mysql Binlog Dump原理
Register 首先,我们需要伪造一个slave,向master注册,这样master才会发送binlog event.注册很简单,就是向master发送COM_REGISTER_SLAVE命令,带 ...
- 使用Devexpress中的CharControl控件,需要控制AxisY轴的显示范围,需要使用该控件的BoundDataChanged事件
一.控制ChartControl的Y轴范围 使用Devexpress中的CharControl控件,需要控制AxisY轴的显示范围,需要使用该控件的BoundDataChanged事件,具体代码如下: ...
- asp.net中的绝对路径和相对路径
一.关于相对路径和绝对路径相对路径转绝对路径一般,我们在ASP.NET网站中往往需要把一个相对路径转化为绝对路径.通常是用Server.MapPath()方法.比如网站根目录下有 个"Upl ...
- 【27前端】base标签带有href属性会让chrome里的svg元素url失效
一个chrome的问题,但具体原因不明. 触发条件:chrome浏览器base标签里href属性有值的时候 触发问题:svg里面的元素如果有用url的滤镜和模糊,则会失效,在firefox里和IE10 ...
- UVA 1615 Highway
题意: 有一条沿x轴正方向,长为L的高速公路,n个村庄,要求修建最少的公路出口数目,使得每个村庄到出口的距离不大于D. 分析: 每个村子可建出口的距离是(l-d,r+d).将所有区间按右端点排序,若需 ...