HDU 1598 find the most comfortable road 并查集+贪心
题目链接:
http://acm.hdu.edu.cn/showproblem.php?pid=1598
find the most comfortable road
Time Limit: 1000/1000 MS (Java/Others)Memory Limit: 32768/32768 K (Java/Others)
#### 问题描述
> XX星有许多城市,城市之间通过一种奇怪的高速公路SARS(Super Air Roam Structure---超级空中漫游结构)进行交流,每条SARS都对行驶在上面的Flycar限制了固定的Speed,同时XX星人对 Flycar的“舒适度”有特殊要求,即乘坐过程中最高速度与最低速度的差越小乘坐越舒服 ,(理解为SARS的限速要求,flycar必须瞬间提速/降速,痛苦呀 ),
> 但XX星人对时间却没那么多要求。要你找出一条城市间的最舒适的路径。(SARS是双向的)。
#### 输入
> 输入包括多个测试实例,每个实例包括:
> 第一行有2个正整数n (1 接下来的行是三个正整数StartCity,EndCity,speed,表示从表面上看StartCity到EndCity,限速为speedSARS。speed 然后是一个正整数Q(Q 接下来Q行每行有2个正整数Start,End, 表示寻路的起终点。
#### 输出
> 每个寻路要求打印一行,仅输出一个非负整数表示最佳路线的舒适度最高速与最低速的差。如果起点和终点不能到达,那么输出-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
题解
并查集+贪心。
我们先对所有的边排序,然后对于每个查询,我们枚举开始的边,然后用并查集维护连通性,一旦目前的边足以连通查询的顶点对,那么说明这一轮的答案就出来了,像这样做m轮就能求出答案。最坏情况也就O(m^2)。
代码
#include<iostream>
#include<cstdio>
#include<vector>
#include<algorithm>
#define X first
#define Y second
#define mp make_pair
using namespace std;
const int maxn = 1e3+10;
const int INF = 0x3f3f3f3f;
int n, m;
int fa[maxn];
int find(int x) { return fa[x] = fa[x] == x ? x : find(fa[x]); }
struct Edge {
int u, v, w;
bool operator < (const Edge& tmp) const {
return w < tmp.w;
}
}egs[maxn];
int main() {
while (scanf("%d%d", &n, &m) == 2 && n) {
for (int i = 0; i < m; i++) {
scanf("%d%d%d", &egs[i].u, &egs[i].v, &egs[i].w);
}
sort(egs, egs + m);
int q;
scanf("%d", &q);
while (q--) {
int s, d;
scanf("%d%d", &s, &d);
int ans = INF;
for (int i = 0; i < m; i++) {
for (int i = 1; i <= n; i++) fa[i] = i;
for (int j = i; j < m; j++) {
Edge& e = egs[j];
int pu = find(e.u);
int pv = find(e.v);
if (pu != pv) {
fa[pv] = pu;
}
if (find(s) == find(d)) {
ans = min(ans, egs[j].w - egs[i].w);
break;
}
}
}
if (ans < INF) printf("%d\n", ans);
else puts("-1");
}
}
return 0;
}
Notes
很多枚举区间的问题,排序之类的预处理一下,一般只要枚举上界就可以了。
HDU 1598 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 (并查集)
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 ...
- HDU 1598 find the most comfortable road(最小生成树之Kruskal)
题目链接: 传送门 find the most comfortable road Time Limit: 1000MS Memory Limit: 32768 K Description XX ...
- HDU 1598 find the most comfortable road (MST)
find the most comfortable road Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d ...
- 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 【最小生成树】
题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=1598 思路 用kruskal 算法 将边排序后 跑 kruskal 然后依次将最小边删除 再去跑 kr ...
- HDU 1598 find the most comfortable road (罗列+Kruskal) 并检查集合
Problem Description XX星有很多城市,城市之间通过一种奇怪的快速公路SARS(Super Air Roam Structure---超级空中漫游结构)进行交流.每条SARS都对行驶 ...
- HDU 1598 find the most comfortable road(枚举+并查集,类似于最小生成树)
一开始想到用BFS,写了之后,发现有点不太行.网上查了一下别人的解法. 首先将边从小到大排序,然后从最小边开始枚举,每次取比它大的边,直到start.end属于同一个集合,即可以连通时停止.过程类似于 ...
随机推荐
- 如何调节datagridview中字体
设置ColumnHeaderDefaultCellStyle的Font属性 或者 编程 datagridview.Columns[index].DefaultCellStyle.Font.Size=“ ...
- [leetcode]_Add Two Numbers
题目:两个链表存储数字,然后求和,和值存储在一个链表中. 代码: public ListNode addTwoNumbers(ListNode l1, ListNode l2) { ListNode ...
- cassandra 之 jdbc 使用【java、scala】
1.数据库创建 参考接上文cassandra入门 http://www.cnblogs.com/piaolingzxh/p/4197833.html 2.下载jdbc驱动源码,构建jar包 源码下载地 ...
- Cassandra 之 入门
1.到官网下载压缩包. http://cassandra.apache.org/download/ 我下载的是最新的 apache-cassandra-2.1.2-bin.tar.gz 另外:语言支持 ...
- thinkphp foreach循环生成二维数组的方法
先做个问题记录,另外下面是做的过程中遇到的一个没想明白的现象 foreach($result as $key => $val ){ $wzList[$key]['lik']=$val[0]; $ ...
- 自定义获取html元素对象的7种方法。
- ok6410的DMA裸机总结
1.为何使用DMA:为了提高CPU的工作效率,避免多余的等待时间 2.关于DMA控制器:(1)通道数:2440有4个通道,6410有4个DMA控制器(初始化的时候要选择),32个通道.210有两种DM ...
- 【微网站开发】之微信内置浏览器API使用
最近在写微网站,发现了微信内置浏览器的很多不称心的地方: 1.安卓版的微信内浏览器底部总是出现一个刷新.前进.后退的底部栏,宽度很大,导致屏幕显示尺寸被压缩 2.分享当前网站至朋友圈时,分享的图片一般 ...
- <?php>慢慢写一些php的cookie问题<?>
写网站是个爬坑的过程,在你设计完功能之后,就会发现:卧槽,这个怎么实现?你妹,这个能实现么? 进了公司分工明确还好说(= =学长们都这么说),在学校自己没事写一些项目的话只能自己爬坑了. 蹬蹬瞪蹬,登 ...
- jquery的异步获取返回值为中文时乱码解决方法
用jqgrid异步获取列表值,遇到个问题,服务器端从数据库取到的数据没有出现中文乱码问题(日志打出来是没有乱码的),但是异步传到客户的时候却出现了乱码. 服务器端已经编码过了(UTF-8编码).开始一 ...