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属于同一个集合,即可以连通时停止.过程类似于 ...
随机推荐
- phaser源码解析(三) Phaser.Utils类下isPlainObject方法
/** * #这是一个对jQuery.isPlainObject(obj)稍加修改的方法. 一个 普通对象 obj.toString() => "[object Object]&quo ...
- Java教程——CMD手动编译运行失败原因(高手略过)
(仅对新手,高手略过)在学习Java初期,我们在利用cmd手动编译java程序的时候,会遇到编译成功,但运行却总是提示失败.已经排除了java配置环境的问题,Path和ClassPath以及%JAVA ...
- 查看编译后的calss文件编译jdk版本
使用UtralEdit或者sublime text打开编译后的.class文件, 其中cafe babe为magic number(魔数),标识这个文件是java的class文件. 0033转换成10 ...
- c++ fstream中seekg()和seekp()的用法
转自:http://blog.sina.com.cn/s/blog_679f85d40100mysi.html 先说一下C语言中fseek()的功能: 函数原型:int fseek(FILE *fp, ...
- gitlab的安装以及汉化
gitlab的安装 首先在网上下载好任意版本gitlab的rpm包 推荐下面的地址: https://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/yum/el7/gi ...
- android 开源框架推荐
同事整理的 android 开源框架,个个都堪称经典.32 个赞! 1.volley 项目地址 https://github.com/smanikandan14/Volley-demo (1) JS ...
- 关于C# Winform 程序开机自动启动
1.程序运行时调用下面方法即可. /// <summary> /// 设置开机自动启用 /// </summary> private void SetAutoStart() { ...
- HTML标签总结
HTML 基本文档 <!DOCTYPE html> <html> <head> <title>文档标题</title> </head& ...
- php获取文件mime类型Fileinfo等方法
前几天写到使用wordpress xmlrpc api远程发布文章,如果本地服务器的文章库里某一篇待发表的wordpress文章包含图片文件时,就会使用到WordPress上传文件的API metaW ...
- AppDomain(1)-AppDomainSetup