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属于同一个集合,即可以连通时停止.过程类似于 ...
随机推荐
- http://www.codeproject.com/KB/validation/MultipleDataAnnotations.aspx
原文发布时间为:2011-08-12 -- 来源于本人的百度文章 [由搬家工具导入] http://www.codeproject.com/KB/validation/MultipleDataAnno ...
- WIN8.1侧边栏文件夹删除
1.注册表定位到 HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\ CurrentVersion\Explorer\MyComputer\NameSpace ...
- 乌班图中的ssh服务
SSH服务(TCP端口号22):安全的命令解释器 为客户机提供安全的Shell 环境,用于远程管理 ...
- scrapy实战
采用scrapy实现对股票网站的爬取 功能描述: 技术路线:scrapy 目标:获取上交所和深交所所有股票名称和交易信息并存储 实例编写: 步骤1:建立工程和spider爬虫模板 步骤2:编写spid ...
- 《Linux命令行与shell脚本编程大全 第3版》Linux命令行---46
以下为阅读<Linux命令行与shell脚本编程大全 第3版>的读书笔记,为了方便记录,特地与书的内容保持同步,特意做成一节一次随笔,特记录如下:
- PyCharm 安装配置使用
PyCharm 安装 下载地址 专业版:https://download.jetbrains.com/python/pycharm-professional-2017.2.3.exe 社区版:http ...
- Java面试常问的问题(转载)
并发.JVM.分布式.TCP/IP协议 1)Java的数据结构相关的类实现原理,比如LinkedList,ArrayList,HashMap,TreeMap这一类的.以下简单模拟一个数据结构的连环炮. ...
- 2016北京集训测试赛(十六)Problem C: ball
Solution 这是一道好题. 考虑球体的体积是怎么计算的: 我们令\(f_k(r)\)表示\(x\)维单位球的体积, 则 \[ f_k(1) = \int_{-1}^1 f_{k - 1}(\sq ...
- SecureCRT鼠标快速复制粘贴
- tiny4412 解决内核编译版本号问题
内核版本: linux-3.5开发板: tiny4412作者:彭东林邮箱:pengdonglin137@163.com 问题: 由于我使用 git 管理内核代码,导致编译完成后内核版本变成了如下形式: ...