hdu 1598 find the most comfortable road (并查集)
find the most comfortable road
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3240 Accepted Submission(s): 1373
但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, 表示寻路的起终点。
//421MS 244K 1294 B G++
/* 并查集+贪心:
个人觉得这种方法会TLE..但是出于在做并查集专题..
(时间复杂度应该是O(m*m*q*lgn)了,应该用最短路做比较好。)
小变异并查集:
先将数据按速度排好序,然后一个个的搜,搜到结束为止,再从中找
最优解。 */
#include<stdio.h>
#include<stdlib.h>
#define inf 0x7ffffff
struct node{
int a,b,v;
}g[];
int set[];
int cmp(const void*a,const void*b)
{
return (*(node *)a).v-(*(node *)b).v;
}
int find(int x)
{
if(set[x]==x) return x;
return find(set[x]);
}
int main(void)
{
int n,m,q;
int a,b,s;
while(scanf("%d%d",&n,&m)!=EOF)
{
for(int i=;i<m;i++)
scanf("%d%d%d",&g[i].a,&g[i].b,&g[i].v);
qsort(g,m,sizeof(g[]),cmp);
//for(int i=0;i<m;i++) printf("*%d\n",g[i].v);
scanf("%d",&q);
while(q--){
int min=inf;
int i,j;
scanf("%d%d",&a,&b);
for(i=;i<m;i++){
for(j=;j<=n;j++) set[j]=j;
for(j=i;j<m;j++){ //爆搜
int x=find(g[j].a);
int y=find(g[j].b);
if(x!=y) set[y]=x;
if(find(a)==find(b)){ //找到路线,速度直接相减即得舒适度
int temp=g[j].v-g[i].v;
if(temp<min) min=temp;
break; //优化
}
}
if(j==m) break; //优化
}
if(min==inf) puts("-1");
else printf("%d\n",min);
}
}
return ;
}
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 (并查集+枚举)
题目链接: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(最小生成树之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属于同一个集合,即可以连通时停止.过程类似于 ...
随机推荐
- linux服务基础之ftp服务
ftp是一种文件传输协议,我们以redhat6.9为服务器系统,来介绍一下ftp服务器,这里我们先介绍一下ftp协议工作的原理 ftp协议可以在不同类型的计算机之间传输文件,工作流程大致为 1:客户机 ...
- 如何改变memcached默认的缓存时间?
我们在使用php的memcached的扩展来对memcached进行数据添加时,数据的有效时间有两种方式.如下图. 至于设置一个UNIX时间戳或 以秒为单位的整数(从当前算起的时间差)来说明 ...
- [转]App离线本地存储方案
App离线本地存储方案 原文地址:http://ask.dcloud.net.cn/article/166 HTML5+的离线本地存储有如下多种方案:HTML5标准方案:cookie.localsto ...
- 13.4.3 鼠标与滚轮事件【JavaScript高级程序设计第三版】
鼠标事件是Web 开发中最常用的一类事件,毕竟鼠标还是最主要的定位设备.DOM3 级事件中定义了9 个鼠标事件,简介如下. click:在用户单击主鼠标按钮(一般是左边的按钮)或者按下回车键时触发.这 ...
- queue消息队列
class queue.Queue(maxsize=0) #先入先出 class queue.LifoQueue(maxsize=0) #last in fisrt out class queue. ...
- 第1章 MATLAB概述
MATLAB系统由~开发环境.~语言.~数学函数库.~图形处理系统.~应用程序接口(API)5大部分组成. 界面 命令行中的语句格式 命令行的语句格式:>>变量=表达式(没有>> ...
- Electronic Devices【电子设备】
Electronic Devices We may think we're a culture that gets rid of our worn technology at the first si ...
- node解析post表单信息
一共有4种解析方式 urlencoded.json.text .raw 发起请求的form表单中可以设置三种数据编码方式 application/x-www-form-urlencoded.multi ...
- git初始化仓库相关
当我们需要新建一个git项目会遇到的问题 全局设置 git config --global user.name "名字" git config --global user.emai ...
- MyEclipse - 问题集 - Java compiler level does not match the version of the installed Java project facet
右键项目“Properties”,在弹出的“Properties”窗口左侧,单击“Project Facets”,打开“Project Facets”页面. 在页面中的“Java”下拉列表中,选择相应 ...