Problem Description
XX明星有许多城市,通过与一个陌生的城市高速公路SARS(Super Air Roam Structure---超级空中漫游结构)进行交流。每条SARS都对行驶在上面的Flycar限制了固定的Speed,同一时候XX星人对 Flycar的“舒适度”有特殊要求,即乘坐过程中最快速度与最低速度的差越小乘坐越舒服 ,(理解为SARS的限速要求,flycar必须瞬间提速/降速,痛苦呀 ),

但XX星人对时间却没那么多要求。要你找出一条城市间的最舒适的路径。(SARS是双向的)。
 
Input
输入包含多个測试实例,每一个实例包含:

第一行有2个正整数n (1<n<=200)和m (m<=1000),表示有N个城市和M条SARS。

接下来的行是三个正整数StartCity,EndCity,speed,表示从表面上看StartCity到EndCity,限速为speedSARS。speed<=1000000

然后是一个正整数Q(Q<11),表示寻路的个数。

接下来Q行每行有2个正整数Start,End, 表示寻路的起终点。

 
Output
每一个寻路要求打印一行,仅输出一个非负整数表示最佳路线的舒适度最快速与最低速的差。假设起点和终点不能到达。那么输出-1。
 
Sample Input
4 4
1 2 2
2 3 4
1 4 1
3 4 2
2
1 3
1 2
 
Sample Output
1
0
 

暴力寻找每已排好序的边 的最小生成树。起点和终点在最小生成树里就更新答案。

#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <math.h>
#include <iostream>
#include <string>
#include <map>
#include <stack>
#define lson o<<1, l, m
#define rson o<<1|1, m+1, r
using namespace std;
typedef long long LL;
const int maxn = 105;
const int MAX = 0x3f3f3f3f;
const int mod = 1000000007;
int n, m, q, fa[205];
struct C {
int v, w, num;
} ed[1005];
int Find(int x) {
return x == fa[x] ? x : fa[x] = Find(fa[x]);
}
bool cmp(C x, C y) {
return x.num < y.num;
}
int main()
{
while(~scanf("%d%d", &n, &m)) {
for(int i = 0; i < m; i++) scanf("%d%d%d", &ed[i].v, &ed[i].w, &ed[i].num);
sort(ed, ed + m, cmp);
scanf("%d", &q);
while(q--) {
int st, en, ans = MAX;
scanf("%d%d", &st, &en);
for(int k = 0; k < m; k++) {
for(int i = 1; i <= n; i++) fa[i] = i;
for(int i = k; i < m; i++) {
int px = Find(ed[i].v), py = Find(ed[i].w);
if(px != py) fa[px] = py;
if(Find(st) == Find(en)) {
ans = min(ans, ed[i].num - ed[k].num);
break;
}
}
}
if(ans == MAX) printf("-1\n");
else printf("%d\n", ans);
}
}
return 0;
}



版权声明:本文博主原创文章。博客,未经同意不得转载。

HDU 1598 find the most comfortable road (最小生成树) &gt;&gt;的更多相关文章

  1. HDU 1598 find the most comfortable road(最小生成树之Kruskal)

    题目链接: 传送门 find the most comfortable road Time Limit: 1000MS     Memory Limit: 32768 K Description XX ...

  2. HDU 1598 find the most comfortable road 并查集+贪心

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1598 find the most comfortable road Time Limit: 1000 ...

  3. hdu 1598 find the most comfortable road (并查集+枚举)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1598 find the most comfortable road Time Limit: 1000/ ...

  4. hdu 1598 find the most comfortable road(枚举+卡鲁斯卡尔最小生成树)

    find the most comfortable road Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K ...

  5. HDU 1589 Find The Most Comfortable Road 最小生成树+枚举

    find the most comfortable road Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K ...

  6. hdu 1598 find the most comfortable road (并查集)

    find the most comfortable road Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K ...

  7. HDU 1598 find the most comfortable road (MST)

    find the most comfortable road Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d ...

  8. hdu 1598 find the most comfortable road(并查集+枚举)

    find the most comfortable road Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K ...

  9. HDU - 1598 find the most comfortable road 【最小生成树】

    题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=1598 思路 用kruskal 算法 将边排序后 跑 kruskal 然后依次将最小边删除 再去跑 kr ...

  10. HDU 1598 find the most comfortable road(枚举+并查集,类似于最小生成树)

    一开始想到用BFS,写了之后,发现有点不太行.网上查了一下别人的解法. 首先将边从小到大排序,然后从最小边开始枚举,每次取比它大的边,直到start.end属于同一个集合,即可以连通时停止.过程类似于 ...

随机推荐

  1. Linux(Centos)中tcpdump参数用法详解(转)

    在linux下进行编程开发的人尤其是网络编程的人会经常需要分析数据包,那么一定会用到tcpdump,下面就是关于tcpdump的使用方法说明(1). tcpdump的选项 -a       将网络地址 ...

  2. grep在一个特定的文件搜索文件夹keyword

    grep -R --include="*.*"(文件名匹配)  key(keyword) dir(夹) eg.在当前文件夹搜索xml关键文件172.19.32.22 grep -R ...

  3. Oracle 初始化参数文件pfile和spfile

    pfile和spfile差额 pfile :Oracle 9i之前.ORACLE使用我们一直PFILE存储的初始化参数,,能够在操作系统级别改动. 当spfile文件改动出现错误导致oracle无法启 ...

  4. Storm基础知识

    上一篇文章我们介绍一个简单的Storm起源,今天我去学习Storm一些主要的知识,他的基本使用基本的了解.幸运的是,,不是太困难,假设我们理解Hadoop的MapReduce模型的话.看这个也是很类似 ...

  5. COCOS2D-X FRAME动画创作随笔

    CCAnimate继承CCActionInterval,和CCAnimate是一家action,有着action所有的属性和方法. CCAnimate一些重要的方法: static CCAnimate ...

  6. Spring 注解学习手札(七) 补遗——@ResponseBody,@RequestBody,@PathVariable (转)

    最近需要做些接口服务,服务协议定为JSON,为了整合在Spring中,一开始确实费了很大的劲,经朋友提醒才发现,SpringMVC已经强悍到如此地步,佩服! 相关参考: Spring 注解学习手札(一 ...

  7. 《javascript高级编程》读书笔记(两)javascript基本概念

    第三章:基本概念 ECMAScript那里5种简单数据类型(也称基本数据类型):Undefined\Null\Boolean\Number\String,另一种复杂数据类型--Object,Objec ...

  8. Linux核心regulator建筑和准备

    电源引入的物种 (百度百科)LDO这是low dropout regulator,这意味着低压差线性稳压器.它相比于传统的线性调节器.传统的线性稳压器.例如78xx系列芯片需要输入电压比输出电压高2v ...

  9. SDI接口

    SDI接口,是"数字分量串行接口". SDI接口是数字分量串行接口(serial digital interface)的首字母缩写. 1简单介绍编辑 SDI接口是一种"数 ...

  10. DBA查询命令积累——不断更新

    原文:DBA查询命令积累--不断更新 一.服务器配置: 1.兼容级别:兼容级别只影响指定数据库中的行为,而不会影响整个服务器上的行为. 1.1.查看数据库兼容级别及更改兼容级别: SELECT com ...