HDU 1598 find the most comfortable road (罗列+Kruskal) 并检查集合
Problem Description
但XX星人对时间却没那么多要求。要你找出一条城市间的最舒适的路径。
(SARS是双向的)。
Input
第一行有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, 表示寻路的起终点。
Output
Sample Input
4 4
1 2 2
2 3 4
1 4 1
3 4 2
2
1 3
1 2
Sample Output
1
0
# include<iostream>
# include<cstdio>
# include<algorithm>
using namespace std;
const int maxn=1000+5;
struct node{
int st;
int ed;
int fu;
}road[maxn];
int p[maxn];
int st,ed;
int miin;
int inf=1000000+5;
bool cmp(node x,node y)
{
return x.fu<y.fu;
}
int find(int x)
{
return p[x]== x ? x:p[x]=find(p[x]);
} int main()
{
int n,m;
while(cin>>n>>m)
{
for(int i=1;i<=m;i++)
scanf("%d%d%d",&road[i].st,&road[i].ed,&road[i].fu);
sort(road+1,road+m+1,cmp);
int tot;
cin>>tot;
while(tot--)
{
miin=inf;
scanf("%d%d",&st,&ed);
for(int i=1;i<=m;i++)
{
for(int i=1;i<=n;i++) p[i]=i;
for(int j=i;j<=m;j++)
{
int x=find(road[j].st);
int y=find(road[j].ed);
if(x!=y) p[x]=y;
if(find(st)==find(ed))
{
int sum=road[j].fu-road[i].fu;
if(sum<miin) miin=sum;
break;
}
}
}
if(miin==inf)
printf("-1\n");
else
printf("%d\n",miin);
} }
return 0;
}
版权声明:本文博客原创文章,博客,未经同意,不得转载。
HDU 1598 find the most comfortable road (罗列+Kruskal) 并检查集合的更多相关文章
- 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(枚举+并查集,类似于最小生成树)
一开始想到用BFS,写了之后,发现有点不太行.网上查了一下别人的解法. 首先将边从小到大排序,然后从最小边开始枚举,每次取比它大的边,直到start.end属于同一个集合,即可以连通时停止.过程类似于 ...
随机推荐
- js cookie介绍和实例(用于自动登录,记住用户名等)
js cookie介绍和实例(用于自动登录,记住用户名等) 一.总结 1.cookie在客户端:因为js是最初是用来在客户端和服务器端进行通信使用的,所以客户端比如js可以操作cookie正常 2.c ...
- POJ 1979 Red and Black (zoj 2165) DFS
传送门: poj:http://poj.org/problem?id=1979 zoj:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problem ...
- javascrit开发的基本代码结构的
今天看到群里一个demo,简单看了一下. 然后自己就写了一个通用的javascrit开发的基本代码结构的js文件. 代码例如以下: (function($,win){ //定义全局变量对象 var o ...
- 【Debug】— C++ 表达式必须包含类类型
错误一般发生在使用.进行访问时,原因可能在于: 你以为你定义了一个类对象,其实你是声明了一个函数,在编译器看来: 对类对象指针采用.的方式访问其成员变量: 也包括基本类型变量,错误地使用. int a ...
- 【35.29%】【codeforces 557C】Arthur and Table
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
- 【TP SRM 703 div2 500】 GCDGraph
Problem Statement You are given four ints: n, k, x, and y. The ints n and k describe a simple undire ...
- [Most.js] Create Streams From Single Values With Most.js
Most provides many means for creating streams, the simplest of which is the offunction. In this less ...
- 联想笔记本装win7
http://jingyan.baidu.com/article/7c6fb42862d63a80642c90b8.html http://bbs.thinkworldshop.com.cn/thre ...
- [Vue] Get up and running with vue-router
How do we set up a simple app with its own routes with their own components in Vue? Install: npm i - ...
- js进阶 9-10 html控件如何实现点击自动选择控件内容
js进阶 9-10 html控件如何实现点击自动选择控件内容 一.总结 一句话总结: 1.在click事件中,如果focus,那就select 2.blur 1.html中控件添加两种方式? 在表单 ...