hdoj--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): 5350 Accepted Submission(s): 2326
但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#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
#define INF 0xfffffff
int pre[10010],m,n;
struct node
{
int u,v;
int val;
}edge[10010];
int cmp(node s1,node s2)
{
return s1.val<s2.val;
}
void init()
{
for(int i=0;i<10010;i++)
pre[i]=i;
}
int find(int x)
{
if(pre[x]==x)
return x;
return pre[x]=find(pre[x]);
}
int main()
{
while(scanf("%d%d",&n,&m)!=EOF)
{
int minn;
for(int i=0;i<m;i++)
scanf("%d%d%d",&edge[i].u,&edge[i].v,&edge[i].val);
int p,s,e;
sort(edge,edge+m,cmp);
scanf("%d",&p);
while(p--)
{
scanf("%d%d",&s,&e);
minn=INF;
for(int i=0;i<m;i++)
{
init();
for(int j=i;j<m;j++)
{
int fx=find(edge[j].u);
int fy=find(edge[j].v);
if(fx!=fy)
pre[fx]=fy;
if(find(s)==find(e))
{
minn=min(minn,edge[j].val-edge[i].val);
break;
}
}
}
if(minn==INF)
printf("-1\n");
else
printf("%d\n",minn);
}
}
return 0;
}
hdoj--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(最小生成树之Kruskal)
题目链接: 传送门 find the most comfortable road Time Limit: 1000MS Memory Limit: 32768 K Description XX ...
- 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 (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(并查集+枚举)
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属于同一个集合,即可以连通时停止.过程类似于 ...
随机推荐
- iOS - CocoaPods操作详解
在我们进行iOS应用开发的时候,肯定会用到很多的第三方类库,最常用AFNetworking,SDWebImage等等,当我们用到这个类库的时候,就要一个一个的去下载这个类库,如果这个类库中又用到了其他 ...
- Laravel5.1学习笔记4 控制器
HTTP 控制器 简介 基础控制器 控制器中间件 RESTful 资源控制器 隐式控制器 依赖注入和控制器 路由缓存 简介 除了在单一的 routes.php 文件中定义所有的请求处理逻辑之外,你可能 ...
- JS排序之选择排序
遍历这个数组,先确定索引为0的数字为暂时最小数, 在剩下的数据中,以第一个为标杆,和剩下的数依次进行比较,如果标杆大于某数,则进行索引交换,继续比较,则a[i]=min; 最后让a[i]与索引为0的数 ...
- Android贝塞尔曲线应用-跳动的水滴
主要通过6个控制点实现. val startPoint = PointF() val endPoint = PointF() val control1 = PointF() val control2 ...
- py2exe打包遇到的问题
py2exe打包python成.exe文件 打包过程和结果 1.创建setup脚本打包文件,其中设置打包的属性和方法.注意:尽量将被打包文件和此打包脚本放在同目录下(因为在尝试非同目录下时,出现了非可 ...
- SQL Server中,with as使用介绍
一.WITH AS的含义 WITH AS短语,也叫做子查询部分(subquery factoring),可以让你做很多事情,定义一个SQL片断,该SQL片断会被整个SQL语句所用到.有的时候 ...
- creat-react-app 支持 less
yarn eject yarn add less less-loader config/ webpack.config.dev.js config/ webpack.config.prod.js 文 ...
- easyui combobox的增加全选解决方案
1.解决方案背景: 项目中偶然需要用到easyui的combobox的组件,但是本组件自己没有包含全选的api事件.搜索了一些解决方案,但是不是很符合,后来发现是因为所使用的版本不一致所导致的.项 ...
- nginx负载均衡及反向代理
1.启动,重启,关闭 启动:/usr/local/nginx/sbin/nginx (/usr/local/nginx/sbin/nginx -t 查看配置信息是否正确) 重启:/usr/local/ ...
- drf05 路由Routers
对于视图集ViewSet,我们除了可以自己手动指明请求方式与动作action之间的对应关系外,还可以使用Routers来帮助我们快速实现路由信息. REST framework提供了两个router ...