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): 2796 Accepted Submission(s): 1192
#include <stdio.h>
#include <string.h>
#include <algorithm>//sort函数一定要用c++
#define M 1001
#define inf 99999999
using namespace std;
int n,p[M];
struct node
{
int from,to;
int speed;
}d[M];
void init()
{
int i;
for(i=;i<=n;i++)
p[i]=i;
}
int cmp(node a,node b)
{
return a.speed<b.speed;
}
int Find(int x)
{
if(p[x]!=x)
p[x]=Find(p[x]);
return p[x];
}//找到同根。
void Union(int x,int y)
{
x=Find(x);
y=Find(y);
if(x==y)
return;
p[x]=y;
}//合并同根。
int main()
{
int m,t,i,j,st,ed;
while(scanf("%d%d",&n,&m)!=EOF)
{
for(i=;i<m;i++)
scanf("%d%d%d",&d[i].from,&d[i].to,&d[i].speed);
sort(d,d+m,cmp);//sort函数用法。
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&st,&ed);
int ans=inf;
for(i=;i<m;i++)
{
init();//每次都要数组初始化。
for(j=i;j<m;j++)
{
Union(d[j].from,d[j].to);
int x=Find(st);
int y=Find(ed);
if(x==y)
break;
}
if(j>=m)//j为什么要>;
break;
if(d[j].speed-d[i].speed<ans)
ans=d[j].speed-d[i].speed;
}
printf("%d\n",ans==inf?-:ans);
}
}
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 (并查集)
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属于同一个集合,即可以连通时停止.过程类似于 ...
随机推荐
- Codevs 1191 数轴染色
1191 数轴染色 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题目描述 Description 在一条数轴上有N个点,分别是1-N.一开始所有的点都被染成黑色. ...
- OpenCV(5)-图像掩码操作(卷积)-锐化
锐化概念 图像平滑过程是去除噪声的过程.图像的主要能量在低频部分,而噪声主要集中在高频部分.图像的边缘信息主要也在高频部分,在平滑处理后,将会丢不部分边缘信息.因此需要使用锐化技术来增强边缘. 平滑处 ...
- void void*
void类型及void指针 1.概述 许多初学者对C/C 语言中的void及void指针类型不甚理解,因此在使用上出现了一些错误.本文将对void关键字的深刻含义进行解说,并 详述void及void指 ...
- html定义对象
<object>定义一个对象<param>为对象定义一个参数 参数的名称:name = "" 参数的值:value=""classid: ...
- grunt-mac上安装运行构建工具的总结(一)
安装node.js brew install node.js 安装grunt npm install -g grunt-cli 1.新建package.json,配置 { "name&quo ...
- Python学习_IDLE快捷键以及列表相关杂记
IDLE快捷键 Tab完成:键入部分代码,按下TAB键,IDLE将给出列表帮助完成语句 回退代码语句:按下Alt+P(Previous),可以回退到IDLE中之前输入的代码语句, 下一个代码语句:按下 ...
- 摩尔斯电码(Morse Code)Csharp实现
摩尔斯电码,在早期的"生产斗争"生活中,扮演了很重要的角色,作为一种信息编码标准,摩尔斯电码拥有其他编码方案无法超越的长久生命.摩尔斯电码在海事通讯中被作为国际标准一直使用到199 ...
- Android直接通过ip进行Http请求
在测试环境,如果直接通过ip访问的话,比如:url:123.123.123/user/login.do?username=a&psw=b,这样是不行的,会报protocal协议错误,要写全称, ...
- cocos2dx 3.3创建新项目 和 VS2012解决方案加载失败问题
首先创建新项目,步骤如下: 1.进入cocos2d-x-3.3\tools\cocos2d-console\bin目录,按住shift+鼠标右键 2.输入 cocos new 项目名 –p 包名 – ...
- python 处理cookie简单很多啊 httpclient版本是4.3.3
模拟登录流程: 1 请求host_url 2 从host_url中解析出 隐藏表单 的值 添加到POST_DATA中 3 添加账户,密码到POST_DATA中 4 编码后,发送POST请求 要点 ...