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): 4899 Accepted Submission(s): 2131
但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 中文题目就不用说题意了
并查集,排序加枚举,下边是我理解大神代码猴,自己加的凝视。,唉,,仅仅怪自己太菜
2015,。7,,22#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
struct node
{
int sc,ec,speed;
}a[1100];
int f[210];
int n,m; bool cmp(node a,node b){ return a.speed<b.speed; }
void init() { for(int i=0;i<210;i++) f[i]=i; }
int find(int k) { if(f[k]!=k) k=find(f[k]); return k; }
void merge(int a,int b){ a=find(a); b=find(b); if(a!=b) f[a]=b; }
int slove(int s,int e)
{
int i,j,ans;
init();
for(i=0;i<m;i++)//推断是否连通
{
merge(a[i].sc,a[i].ec);
}
if(find(s)!=find(e)) return -1;//假设不连通就返回-1
ans=0x3f3f3f;
for(i=0;i<m;i++)//以每条边为起始边。这条边权就是这条线的最小速度
{
init();
for(j=i;j<m;j++)//依次增加边。直到连通。ans存放最小的差值
{
if(a[j].speed-a[i].speed>=ans) continue;
merge(a[j].sc,a[j].ec);
if(find(s)==find(e))//假设起点和终点连通时 ,此时的a[j].speed就是这条路线的最大速度
{
if(ans>a[j].speed-a[i].speed)
ans=a[j].speed-a[i].speed;
}
}
}
return ans;
}
int main(){
int i,t,s,e;
while(~scanf("%d%d",&n,&m))
{
for(i=0;i<m;i++)
{
scanf("%d%d%d",&a[i].sc,&a[i].ec,&a[i].speed);
}
sort(a,a+m,cmp);//按权值排序
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&s,&e);
printf("%d\n",slove(s,e));
}
}
return 0;
}
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(枚举+卡鲁斯卡尔最小生成树)
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 【最小生成树】
题目链接 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属于同一个集合,即可以连通时停止.过程类似于 ...
随机推荐
- rand()与 srand()
一 头文件 <time.h> srand()就是给rand()提供种子seed. 在C语言中,srand()与rand()是随机函数,其中srand函数是伪随机数发生器的初始化函数,原 ...
- 在react当中巧用扩展运算符
...props可以把没有写到的属性补充完整 ...style 可以把style 属性在styles当中展开
- 有关UITableView--cell复用问题
近来用Tableview做了一个九宫格.过程中碰到了两个cell复用问题. 问题一: 在cell中为button添加addTarget点击事件时,出现后面的cell会重叠它前面cell的事件.代码 ...
- js判断手机的左右滑动
js代码 $(function() { function judge() { var startx;//让startx在touch事件函数里是全局性变量. var endx; var el = doc ...
- 定时执行rsync同步数据以及mysql备份
需求:把机器A中的附件.图片等,备份到备份机B中.将数据库进行备份 附件备份 在A中,启动rsync服务,编辑/etc/xinetd.d/rsync文件,将其中的disable=yes改为disabl ...
- Java发送邮件--web.xml配置,Java代码配置
前言:我目前总结的使用java发送邮件的方式有两种,分别是在spring框架xml配置文件使用bean标签,另一种方法是把发送功能封装成一个对象,废话不多说上代码, 边看代码边讲解,希望对需要的人能有 ...
- Java爬虫系列之实战:爬取酷狗音乐网 TOP500 的歌曲(附源码)
在前面分享的两篇随笔中分别介绍了HttpClient和Jsoup以及简单的代码案例: Java爬虫系列二:使用HttpClient抓取页面HTML Java爬虫系列三:使用Jsoup解析HTML 今天 ...
- Single Number III - LeetCode
Given an array of numbers nums, in which exactly two elements appear only once and all the other ele ...
- log4j(一)
一.基础知识 Log4j有三个重要组件:Logger-日志信息的级别,appenders-日志信息的输出目的地,layouts-日志信息的输出格式. Logger-日志信息的级别:level 是日志记 ...
- Centos7下实现多虚拟机互信
假设CentOS 7三台虚拟机A(192.168.111.10).B(192.168.111.11).C(192.168.111.12),需要保证三台虚拟机之间网络的连通性. 操作步骤: 一.在A机上 ...