HDU1598 find the most comfortable road 【并查集】+【枚举】
find the most comfortable road
但XX星人对时间却没那么多要求。要你找出一条城市间的最舒适的路径。(SARS是双向的)。
第一行有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, 表示寻路的起终点。
4 4
1 2 2
2 3 4
1 4 1
3 4 2
2
1 3
1 2
1
0
题意:求联通路中的最小速度差。
题解:先按边权从小到大排序,再用并查集枚举。
2014-11-2 22:38:14更新
/*
** 用并查集每次选择一些权值最接近的边组合使得源点跟终点联通
*/
#include <stdio.h>
#include <string.h>
#include <algorithm> #define maxn 210
#define maxm 1010
#define inf 0x3f3f3f3f int pre[maxn], id;
struct Node {
int u, v, w;
} E[maxm]; int min(int a, int b) {
return a < b ? a : b;
} bool cmp(Node a, Node b) {
return a.w > b.w;
} int ufind(int k) {
int a = k, b;
while(pre[k]) k = pre[k];
while(a != k) {
b = pre[a];
pre[a] = k;
a = b;
}
return k;
} bool same(int a, int b) {
return ufind(a) == ufind(b);
} void unite(int a, int b) {
a = ufind(a);
b = ufind(b);
if(a != b) pre[a] = b;
} void addEdge(int u, int v, int w) {
E[id].u = u;
E[id].v = v;
E[id++].w = w;
} int main() {
int n, m, i, a, b, c, j, q, ans;
while(scanf("%d%d", &n, &m) == 2) {
for(i = id = 0; i < m; ++i) {
scanf("%d%d%d", &a, &b, &c);
addEdge(a, b, c);
}
std::sort(E, E + m, cmp);
scanf("%d", &q);
while(q--) {
scanf("%d%d", &a, &b);
ans = inf;
for(i = 0; i < m; ++i) {
memset(pre, 0, sizeof(int) * (n + 1));
for(j = i; j < m; ++j) {
unite(E[j].u, E[j].v);
if(same(a, b)) {
ans = min(ans, E[i].w - E[j].w);
break;
}
}
if(ans == inf) break; // cut
}
if(ans == inf) ans = -1;
printf("%d\n", ans);
}
}
return 0;
}
#include <stdio.h>
#include <string.h>
#include <limits.h>
#include <algorithm>
#define maxn 202
#define maxm 1002
using std::sort;
using std::min; int pre[maxn];
struct Node{
int u, v, cost;
} E[maxm]; int ufind(int k)
{
int a = k, b;
while(pre[k] != -1) k = pre[k];
while(a != k){
b = pre[a];
pre[a] = k;
a = b;
}
return k;
} bool cmp(Node a, Node b){
return a.cost < b.cost;
} int main()
{
int n, m, q, a, b, i, j, x, y, ans;
while(scanf("%d%d", &n, &m) == 2){
for(i = 0; i < m; ++i)
scanf("%d%d%d", &E[i].u, &E[i].v, &E[i].cost);
sort(E, E + m, cmp);
scanf("%d", &q);
while(q--){
scanf("%d%d", &a, &b);
ans = INT_MAX;
for(i = 0; i < m; ++i){
memset(pre, -1, sizeof(pre));
for(j = i; j < m; ++j){
x = ufind(E[j].u);
y = ufind(E[j].v);
if(x != y) pre[x] = y;
if(ufind(a) == ufind(b)){
ans = min(ans, E[j].cost - E[i].cost);
break;
}
}
}
if(ans == INT_MAX) printf("-1\n");
else printf("%d\n", ans); }
}
return 0;
}
HDU1598 find the most comfortable road 【并查集】+【枚举】的更多相关文章
- hdu 1598 find the most comfortable road (并查集+枚举)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1598 find the most comfortable road Time Limit: 1000/ ...
- HDU 1598 find the most comfortable road 并查集+贪心
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1598 find the most comfortable road Time Limit: 1000 ...
- hdu 1598 find the most comfortable road (并查集)
find the most comfortable road Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K ...
- 最舒适的路(并查集+枚举)(hdu1598)
hdu1598 find the most comfortable road Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768 ...
- hdu1598 find the most comfortable road (枚举)+【并查集】
<题目链接> 题目大意: XX星有许多城市,城市之间通过一种奇怪的高速公路SARS(Super Air Roam Structure---超级空中漫游结构)进行交流,每条SARS都对行驶在 ...
- hdu 1598 find the most comfortable road(并查集+枚举)
find the most comfortable road Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K ...
- HDU-1598 find the most comfortable road
find the most comfortable road Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K ...
- [HDU1598]find the most comfortable road
思路: 考虑一个暴力:枚举最大的边权和最小的边权,然后将边权在这之间的边全拿出来构成一张无向图,剩下的就是判断是否存在一条从$S$到$T$的路径.相当于判$S$和$T$是否连通,用并查集连一下即可.时 ...
- hdu-5861 Road(并查集)
题目链接: Road Time Limit: 12000/6000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Pro ...
随机推荐
- 关于史考特证券(scottrade Inc)资金转出的手续费问题
投资美股账户的朋友可能开始的时候并没有关心史考特账户转出的费用,其实,仔细算一下这个费用还是蛮贵的,根据官网的文档可以明确知道,转出史考特账户资金到国内银行的费用由以下几项组成: 1. 国际电汇资金费 ...
- CentOS系统中出现错误--SSH:connect to host centos-py port 22: Connection refused
我在第一次搭建自己的 hadoop2.2.0单节点的伪分布集成环境时遇到了此错误,通过思考问题和查找解决方案最终搞定了这个问题,其错误原因主要有以下几种: 1)SSH服务为安装 此时,采用在线安装的方 ...
- 网站出现service unavailable的解决方法
特别提示:本文的教程仅适合采用windows服务器的IIS组件上操作,service unavailable是许多网站会经常遇到的问题,希望对大家有用. 昨天一小段时间网站出现了service una ...
- js规范
js规范 Array 和 Object 直接量 为了避免这些歧义, 我们应该使用更易读的直接量来声明. var a = [x1, x2, x3]; var a2 = [x1, x2]; var a3 ...
- C#的Main(String[] args)参数输入问题
1.新建一个控制台应用程序,保存在桌面上,Main函数如下所示 using System;using System.Collections.Generic;using System.Linq;usin ...
- nodejs a和b文件相互引用
//取自于node中文网 http://nodejs.cn/api/modules.html 当循环调用 require() 时,一个模块可能在未完成执行时被返回. 例如以下情况: a.js: con ...
- mysql timeout
(待更新整理) 因为最近遇到一些超时的问题,正好就把所有的timeout参数都理一遍,首先数据库里查一下看有哪些超时: root@localhost : test 12:55:50> show ...
- Storm入门之第一章
Storm入门之第一章 1.名词 spout龙卷,读取原始数据为bolt提供数据 bolt雷电,从spout或者其他的bolt接收数据,并处理数据,处理结果可作为其他bolt的数据源或最终结果 nim ...
- 基础拾遗-----mongoDB操作
基础拾遗 基础拾遗------特性详解 基础拾遗------webservice详解 基础拾遗------redis详解 基础拾遗------反射详解 基础拾遗------委托详解 基础拾遗----- ...
- 《java.util.concurrent 包源码阅读》24 Fork/Join框架之Work-Stealing
仔细看了Doug Lea的那篇文章:A Java Fork/Join Framework 中关于Work-Stealing的部分,下面列出该算法的要点(基本是原文的翻译): 1. 每个Worker线程 ...