Dark roads

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other)
Total Submission(s) : 7   Accepted Submission(s) : 2
Problem Description
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[sup]31[/sup].
 
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
题解:就把总路径加起来,然后减去最小路径;刚开始用prime包内存;最后用kruscal竟然没超市
代码:
 #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)的更多相关文章

  1. HDU 2988 Dark roads(kruskal模板题)

    Dark roads Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  2. c/c++ 用克鲁斯卡尔(kruskal)算法构造最小生成树

    c/c++ 用克鲁斯卡尔(kruskal)算法构造最小生成树 最小生成树(Minimum Cost Spanning Tree)的概念: 假设要在n个城市之间建立公路,则连通n个城市只需要n-1条线路 ...

  3. 最小生成树之克鲁斯卡尔(Kruskal)算法

    学习最小生成树算法之前我们先来了解下 下面这些概念: 树(Tree):如果一个无向连通图中不存在回路,则这种图称为树. 生成树 (Spanning Tree):无向连通图G的一个子图如果是一颗包含G的 ...

  4. 克鲁斯卡尔(Kruskal)算法

    概览 相比于普里姆算法(Prim算法),克鲁斯卡尔算法直接以边为目标去构建最小生成树.从按权值由小到大排好序的边集合{E}中逐个寻找权值最小的边来构建最小生成树,只要构建时,不会形成环路即可保证当边集 ...

  5. POJ 1251 Jungle Roads (prim)

    D - Jungle Roads Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u Su ...

  6. 最小生成树练习2(Kruskal)

    两个BUG鸣翠柳,一行代码上西天... hdu4786 Fibonacci Tree(生成树)问能否用白边和黑边构成一棵生成树,并且白边数量是斐波那契数. 题解:分别优先加入白边和黑边,求出生成树能包 ...

  7. 最小生成树(Kruskal)

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

  8. WUSTOJ 1346: DARK SOULS(Java)并查集

    题目链接:1346: DARK SOULS 并查集系列:WUSTOJ 1319: 球(Java)并查集 Description CQ最近在玩一款游戏:DARK SOULS,这是一款以高难度闻名的硬派动 ...

  9. 这是一篇每个人都能读懂的最小生成树文章(Kruskal)

    本文始发于个人公众号:TechFlow,原创不易,求个关注 今天是算法和数据结构专题的第19篇文章,我们一起来看看最小生成树. 我们先不讲算法的原理,也不讲一些七七八八的概念,因为对于初学者来说,看到 ...

随机推荐

  1. 观察者模式模拟YUI事件机制

    首先介绍下YUI的事件机制,很好理解,因为和浏览器事件机制差不多.看懂下面几个方法就可以了: publish: 创建自定义事件.第一个参数是事件类型,第二个参数是一个对象,里面可以设置默认动作 on: ...

  2. android-support关联源码

    http://blog.csdn.net/xiaanming/article/details/9031141 http://www.cnblogs.com/androidez/archive/2013 ...

  3. JS如何设置计算几天前的时间?

    计算多少天前的具体时间.比如今天是9月5日,那7天前正常就是8月29了. 之前曾经直接用时间进行加减,吃了大亏,后来脑残到直接写了一个很复杂的计算闰年,闰月,30.31.28的月份 现在分享一下. f ...

  4. CKEditor扩展插件:自动排版功能

    CKEditor是新一代的FCKeditor,是一个重新开发的版本.CKEditor是全球最优秀的网页在线文字编辑器之一,因其惊人的性能与可扩展性而广泛的被运用于各大网站. 如果还没接触过的可以看看, ...

  5. 安全运维之:Linux系统账户和登录安全(转)

    三.删减系统登录欢迎信息 系统的一些欢迎信息或版本信息,虽然能给系统管理者带来一定的方便,但是这些信息有时候可能被黑客利用,成为攻击服务器的帮凶,为了保证系统的安全,可以修改或删除某些系统文件,需要修 ...

  6. javac: cannot execute binary file

    # java/jdk1.6.0_12/bin/javac-bash: java/jdk1.6.0_12/bin/javac: cannot execute binary file   后来检验,检查了 ...

  7. Checkbutton 和 Radiobutton

    The Checkbutton widget is used to display a number of options to a user as toggle buttons. The user ...

  8. 【LeetCode练习题】Next Permutation

    Next Permutation Implement next permutation, which rearranges numbers into the lexicographically nex ...

  9. Objective-C 基本语法:实例变量与成员变量的区别.l........实例方法和类方法区别

    http://leopard168.blog.163.com/blog/static/16847184420138153296930/ http://blog.csdn.net/thdxs/artic ...

  10. Zipper

      Zipper Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Su ...