HDU 1598 find the most comfortable road (最小生成树) >>
但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>
#include <math.h>
#include <iostream>
#include <string>
#include <map>
#include <stack>
#define lson o<<1, l, m
#define rson o<<1|1, m+1, r
using namespace std;
typedef long long LL;
const int maxn = 105;
const int MAX = 0x3f3f3f3f;
const int mod = 1000000007;
int n, m, q, fa[205];
struct C {
int v, w, num;
} ed[1005];
int Find(int x) {
return x == fa[x] ? x : fa[x] = Find(fa[x]);
}
bool cmp(C x, C y) {
return x.num < y.num;
}
int main()
{
while(~scanf("%d%d", &n, &m)) {
for(int i = 0; i < m; i++) scanf("%d%d%d", &ed[i].v, &ed[i].w, &ed[i].num);
sort(ed, ed + m, cmp);
scanf("%d", &q);
while(q--) {
int st, en, ans = MAX;
scanf("%d%d", &st, &en);
for(int k = 0; k < m; k++) {
for(int i = 1; i <= n; i++) fa[i] = i;
for(int i = k; i < m; i++) {
int px = Find(ed[i].v), py = Find(ed[i].w);
if(px != py) fa[px] = py;
if(Find(st) == Find(en)) {
ans = min(ans, ed[i].num - ed[k].num);
break;
}
}
}
if(ans == MAX) printf("-1\n");
else printf("%d\n", ans);
}
}
return 0;
}
版权声明:本文博主原创文章。博客,未经同意不得转载。
HDU 1598 find the most comfortable road (最小生成树) >>的更多相关文章
- 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 并查集+贪心
题目链接: 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 1589 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 (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属于同一个集合,即可以连通时停止.过程类似于 ...
随机推荐
- enumerateObjectsUsingBlock、enumerateObjectsWithOptions、enumerateObjectsAtIndexes、makeObjectsPerfor使用
OC至 NSArray它提供了一个方便的遍历block,以下具体说明 第一.enumerateObjectsUsingBlock NSArray *array=@[@"aa",@& ...
- effective c++ 条款3 use const whereever you can
1 const 传达的意思应该是这个变量是常量不能更改 2 const 在 * 左边表示数据是const,在右边表示指针是const // char greeting[] = "hello& ...
- A WPF/MVVM Countdown Timer
Introduction This article describes the construction of a countdown timer application written in C# ...
- 非正确使用浮点数据由项目产生BUG讨论的问题
乘分配 当小学学会了乘法分配.详细乘法分配:并与多个两个数相乘的,他们能够把这个数字乘以,然后加入.由于一个恒定.乘法分配律也能够使用表达式的定义"(a+b)×c = a×c+b×c&quo ...
- poj3928 Ping pong 树状数组
http://poj.org/problem?id=3928 Ping pong Time Limit: 1000MS Memory Limit: 65536K Total Submissions ...
- A ResourcePool could not acquire a resource from its primary factory or source
出处:http://aaron81939097.iteye.com/blog/1144642 原配置: <bean id="dataSource" class="c ...
- 菜鸟版JAVA设计模式-从抽象与实现说桥接模式
桥接模式,初学的时候事实上非常不理解为什么要把这个模式命名为桥接模式,脑海里突然联想到.事实上我学习是一件比較痛苦的事情,由于我必需要知道来龙去脉才干学的进去,所以,非常快我就对这个命名产生了兴趣,桥 ...
- SpringMVC+Mybatis(SMM)+mybatis-generate
java搭建 SpringMVC+Mybatis(SMM)+mybatis-generate 搭建SSM系统,首先要了解整个过程: 1.创建spring-mvc项目 2.在maven中添加要引用的ja ...
- DNA和纳米(Nano)Fusion技术的发展趋势
细观国内外有关DNA与Nano技术的报道.不得不承认存在不小的差距,并且差距有继续拉大的趋势. 在我们国内,DNA技术是作为遗产基因来对待的.引用的有关中文參考资料一般比較陈旧.缺少參考价值.在发达国 ...
- SQL优化策略高级优化经常使用-1(The Return Of The King)
1 经常使用的优化策略 1.1 语句 1.1.1使用实际的列名 当我们查询SQL语句时.你是否觉得使用实际的列名比使用*更快呢?答案是肯定的. 为了证实这一点,感兴趣的朋友能够自己验证一下.我这 ...