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. HeadFirst设计模式读书笔记(3)-装饰者模式(Decorator Pattern)

    装饰者模式:动态地将责任附件到对象上.若要扩展功能,装饰者提东了比继承更有弹性的替代方案. 装饰者和被装饰对象有相同的超类型 你可以用一个或者多个装饰者包装一个对象. 既然装饰者和被装饰对象有相同的超 ...

  2. X光机的原理及构造

    一.X射线的发现 1895年德国物理学家伦琴(W.C.RÖntgen)在研究阴极射线管中气体放电现象时,用年伦琴荣获物理学第一个诺贝尔奖金.科学总是在不断发展的,经伦琴及各国科学家的反复实践和研究,逐 ...

  3. IOS中http请求使用cookie

    http://rainbird.blog.51cto.com/211214/805173 IOS中http请求使用cookie 2012-03-13 23:04:30 标签:http 记录 龙的传人  ...

  4. iOS 创建上线证书

    1.制作上线证书需要准备一个付费的账号(99$),登陆https://developer.apple.com在最上方的位置点击Member Center进入登陆界面,在登陆界面输入付费的账号和密码进入 ...

  5. 下载类网站的SEO优化方面技巧

    在互联网国际中有一类十分主要的网站,那即是供应各种软件下载的网站,这类网站可以协助用户解决许多软件运用方面的疑问,可是随着知识产权维护的认识越来越强,许多下载类网站也要开端改动自个的经营策略,这么才可 ...

  6. 任务调度开源框架Quartz动态加入、改动和删除定时任务

    Quartz 是个开源的作业调度框架,为在 Java 应用程序中进行作业调度提供了简单却强大的机制.Quartz框架包括了调度器监听.作业和触发器监听.你能够配置作业和触发器监听为全局监听或者是特定于 ...

  7. CentOS 安装nload(流量统计)

    yum install gcc gcc-c++ ncurses-devel wget http://www.roland-riegel.de/nload/nload-0.7.2.tar.gz tar ...

  8. VS2015 企业版不支持 JavaScript 语法高亮、智能提醒

    2015年7月,微软终于放出了 Visual Studio 2015 正式版,博主安装了 Visual Studio 2015 企业版之后,居然不支持 JavaScript 的语法高亮.智能提醒功能, ...

  9. Spring事务讲解示例

    Spring 事务Transaction1.事务的属性1.1 事务隔离IsolationLevel1.2 事务传播PropagationBehavior1.3 事务超时Timeout1.4 只读状态R ...

  10. strutr2运行流程

    1. 请求发送给 StrutsPrepareAndExecuteFilter 2. StrutsPrepareAndExecuteFilter 询问 ActionMapper: 该请求是否是一个 St ...