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): 3999 Accepted Submission(s): 1720
星有许多城市,城市之间通过一种奇怪的高速公路SARS(Super Air Roam
Structure---超级空中漫游结构)进行交流,每条SARS都对行驶在上面的Flycar限制了固定的Speed,同时XX星人对
Flycar的“舒适度”有特殊要求,即乘坐过程中最高速度与最低速度的差越小乘坐越舒服
,(理解为SARS的限速要求,flycar必须瞬间提速/降速,痛苦呀 ),
但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, 表示寻路的起终点。
1 2 2
2 3 4
1 4 1
3 4 2
2
1 3
1 2
0
//#define LOCAL
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
const int inf=;
int father[];
struct node
{
int a,b,c;
bool operator <(node const &bb) const
{
return bb.c>c; //小到大
}
}tt[];
void init(int n)
{
for(int i=;i<=n;i++)
father[i]=i;
}
int fin(int x)
{
while(x!=father[x])
x=father[x];
return x;
}
void unin(int a,int b)
{
father[a]=b;
}
int main()
{
#ifdef LOCAL
freopen("test.in","r",stdin);
#endif
int n,m;
while(scanf("%d%d",&n,&m)!=EOF)
{
for(int i=;i<=m;i++)
scanf("%d%d%d",&tt[i].a,&tt[i].b,&tt[i].c);
sort(tt+,tt+(m+));
int q;
scanf("%d",&q);
while(q--)
{
int en,st;
scanf("%d%d",&st,&en);
int ans=inf;
for(int i=;i<=m;i++)
{
init(n);
for(int j=i;j<=m;j++)
{
int fr=fin(tt[j].a);
int to=fin(tt[j].b);
if(fr!=to)unin(to,fr);
fr=fin(st);
to=fin(en);
if(fr==to)
{
ans=min(ans,tt[j].c-tt[i].c);
break;
}
}
}
if(ans==inf) ans=-;
printf("%d\n",ans);
}
}
return ;
}
hdu 1598 find the most comfortable road(枚举+卡鲁斯卡尔最小生成树)的更多相关文章
- HDU 1598 find the most comfortable road(枚举+并查集,类似于最小生成树)
一开始想到用BFS,写了之后,发现有点不太行.网上查了一下别人的解法. 首先将边从小到大排序,然后从最小边开始枚举,每次取比它大的边,直到start.end属于同一个集合,即可以连通时停止.过程类似于 ...
- 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(最小生成树之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对MST的构建:枚举最小的边,逐渐将更大的边加入集合,当查询的点在同一个集合,那么当前最小值,就是所加的最后一条边与第一条只差. 注意: ...
随机推荐
- Date、String和Timestamp类型转换
1.String与Date类型转换: 1.获取当前系统时间: Date date1 = new Date(); //获取系统当前时间 Calendar cal = Calendar.getInst ...
- [HDOJ5952]Counting Cliques(DFS,剪枝)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5952 题意:求图中规模为s的团的个数. DFS+剪枝,姿势不好很容易TLE啊. #include &l ...
- Verify an App Store Transaction Receipt 【苹果服务端 验证一个应用程序商店交易收据有效性】
转自:http://blog.csdn.net/saindy5828/article/details/6414014 1. 从Transaction 的TransactionReceipt属性中得到接 ...
- h5移动开发css
最近刚开始做移动端的开发,接触到很多新的东西,很荣幸(*^__^*) , 下面我们开始正式介绍最近新接触到的属性啦,一起进步: 1.点击按钮等会产生阴影,可设置这个属性:-webkit-tap-hig ...
- 是时候改变你的开发方式了-XAF信息系统快速框架介绍
我是一名.Net开发者,从DOS时代Turbo c 算起(1996年),马上满20年了.想想写过的代码真是不少,却做了很多重复反复的编码工作.当然中间也带过团队做过几个大项目,但是代码仍没写够,还是每 ...
- updatePanel导致JS失效的解决办法(转)
吐槽下,维护别人之前做的项目好蛋疼,整个页面都是用微软的ajax框架. 今天给repeater用JS写一个hover事件 <script type="text/javascript&q ...
- 将客户端将IE9强制为IE7
有时候由于浏览器的问题我们在IE7中开发的东西需要在IE9中展示 但是会出现兼容性的问题. 那么我们可以同技巧将用户端的浏览器强行以IE7的文档模式展示我们的网页 下面是针对iis asp.net程序 ...
- 2013 Multi-University Training Contest 4
HDU-4632 Palindrome subsequence 题意:给定一个字符串,长度最长为1000,问该串有多少个回文子串. 分析:设dp[i][j]表示从 i 到 j 有多少个回文子串,则有动 ...
- 关于Java控制台输入输出乱码问题
产生原因:因为这个开源项目的默认字符编码为UTF-8,所以我的控制台的字符编码也自动变成了UTF-8,而键盘的输入流的默认格式是GBK格式,这样就造成了在GBK转UTF-8的过程中产生的奇数乱码错误( ...
- Hibernate各种主键生成策略与配置详解《转》
1.assigned 主键由外部程序负责生成,在 save() 之前必须指定一个.Hibernate不负责维护主键生成.与Hibernate和底层数据库都无关,可以跨数据库.在存储对象前,必须要使用主 ...