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属于同一个集合,即可以连通时停止.过程类似于 ...
随机推荐
- 实现高可用-Keepalived
简介 Keepalived是HA Cluster(High Availability Cluster,高可用集群)的一个服务软件,用来防止单点故障. Keepalived采用VRRP(virtual ...
- javaScript 字符串与unicode码之间的相互转换,函数的封装
在我们的开发过程中,有时在对数据进行储存的时候,我们需要将字符串转成unicode. 比如,在jsp开发时,前端使用页面间传值时,将传值参数先存入cookie中,然后在使用的时候,再从ookie中取出 ...
- h5移动端页面meta标签
<!DOCTYPE html> <!-- 使用 HTML5 doctype,不区分大小写 --> <html lang="zh-cmn-Hans"&g ...
- [转]Centos7 内核从3.10升级到4.12过程
[原文地址] http://blog.csdn.net/youshijifen/article/details/73472434 [摘要] 近期,国家互联网应急中心漏洞(CNCERT)公告中提到Lin ...
- Andrew Ng Machine Learning Coursera学习笔记
课程记录笔记如下: 1.目前ML的应用 包括:数据挖掘database mining.邮件过滤email anti-spam.机器人autonomous robotics.计算生物学computati ...
- 转译符,re模块,random模块
一, 转译符 1.python 中的转译符 正则表达式中的内容在Python中就是字符串 ' \n ' : \ 转移符赋予了这个n一个特殊意义,表示一个换行符 ' \ \ n' : \ \ 表示取 ...
- u-boot.bin生成过程分析
ELF格式“u-boot”文件的生成规则如下,下面对应Makefile的执行过程分别分析各个依赖. $(obj)u-boot: depend version $(SUBDIRS) $(OBJS) $( ...
- 各种数据库分页语句整理以及Oracle数据库中的ROWNUM和ORDER BY的区别
.oracle数据库分页 select * from (select a.*,rownum rc from 表名 where rownum<=endrow) a where a.rc>=s ...
- poj1050 dp动态规划
Description Given a two-dimensional array of positive and negative integers, a sub-rectangle is any ...
- 暗影精灵3安装无线网卡驱动(ubuntu16.04)
干货,无线网卡安装步骤: 1. 由于暗影精灵3的无线网卡较新,版本为Realtek Device b822,(查看命令为lspci | grep -i net,Ethernet controller代 ...