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属于同一个集合,即可以连通时停止.过程类似于 ...
随机推荐
- java web-----servelet
1,定义servlet: 2,servlet生命周期: 3,HttpServlet常用方法: 4,servlet常用接口: 一,servlet定义: servlet是一个继承HttpServlet类的 ...
- 10.08_逛逛OSC
(1)每天逛逛OSC是我的习惯了. JNative.JACOB.Shrinkwrap API? .Lua.WSO2 Identity Server .JBoss Forge.Bugzilla.Cou ...
- ECMAScript一元操作符
在ECMAScript中提供了一元操作符进行简单的运算,一元操作符是ECMAScript中最简单的操作符,它只能对一个值进行操作. 一元操作符有两种类型,一种是递增和递减操作符,一种是一元加和一元减操 ...
- absolute独立使用之下拉框最佳实践
说明:传统的做法是给外部盒子relative定位,再给弹出框absolute定位,而这里我们将absolute独立使用 html <div class="searc ...
- 用eclipse建立简单WebService客户端,使用WSDL,用于短信接口发送
使用工具:eclipse 标准版,不用任何插件. 操作步骤: 建立java Project 命名为mess: 再在project上右键,选择other,选择web service文件类别,选择web ...
- 在类库或winform项目中打开另一个winform项目的窗体
假设类库或winform项目为A,另一个winform项目为B.那麽在A中添加一个接口,里面有一个Show方法,然后在B中写一个类b继承这个接口,并重写这个方法,具体内容为弹出某个窗体.然后在A中另一 ...
- 对React的理解
转自:http://www.cocoachina.com/webapp/20150721/12692.html 现在最热门的前端框架有AngularJS.React.Bootstrap等.自从接触了R ...
- margin系列之内秀篇
本系列摘自 飘零雾雨的博客 最Cool的利器 一样东西在不同的场景,不同的人手里,所能做的事会有很大不同.我深切的以为 margin 绝对是 CSS 中最有能力的利器之一,不知大家以为然否? 前面几 ...
- DOM Ready 详解
DOM Ready 概述 熟悉jQuery的人, 都知道DomReady事件. window.onload事件是在页面所有的资源都加载完毕后触发的. 如果页面上有大图片等资源响应缓慢, 会导致wind ...
- const和define的使用区别
在PHP中(PHP 4及以后),我们可以使用函数define()来定义常量,例如: <?php define('PI',3.14159); //定义一个名为PI的常量 echo PI; ...