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属于同一个集合,即可以连通时停止.过程类似于 ...
随机推荐
- android-sdk-windows下载版
Android SDK 4.0.3 开发和执行环境配置 近期又装了一次最新版本号的ADK环境 眼下最新版是Android SDK 4.0.3 本文的插图和文本尽管是Android2.2的 步骤都是一样 ...
- li排序
html <ul id="ul1"> <li>9</li> <li>2</li> <li>7</li& ...
- 在WPF中处理Windows消息
在Winform中 处理Windows消息通过重写WndProc方法 在WPF中 使用的是System.Windows. Sytem.Windows.Controls等名字空间,没有WndProc函数 ...
- C++类实现最大数的输出
Description 判断整数的大小,输入n个数,找出最大的数并输出. Input 有多组测试实例,输入n,并输入n个数. Output 输出的最大的数,每个输出结果占一行. Sample Inpu ...
- poj3928 Ping pong 树状数组
http://poj.org/problem?id=3928 Ping pong Time Limit: 1000MS Memory Limit: 65536K Total Submissions ...
- 于win7使用虚拟磁盘隐藏文件
于win7使用虚拟磁盘隐藏文件,我只是win7在验证.其他型号未知. 一.创建虚拟磁盘 1.右键点击"计算机"-----"管理" ------"磁盘管 ...
- IIS使用 URL Rewrite Module 2.0组件 设置伪静态的方法
简体中文版WIn10无法安装,需要改注册表, HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\InetStp MajorVersion 项,这个也是 dword 值 10 ...
- Android-Service组件
转载请标明出处:http://blog.csdn.net/goldenfish1919/article/details/40381109 原文:http://developer.android.com ...
- IIS7和IIS7.5备份和还原的方法
windows2008的iis配置备份和iis6不同,在iis7中没有了6原来的保存配置xml的选项,而采用的是dos命令来进行iis7的备份.以下是win2008上iis7备份的方法. 首先打开CM ...
- rhel6使用的版本数部分intel xeon处理器时间bug
可惜在总前几天"oracle操作和维护的高级别小组"于.BBQ 上帝说,大量RHEL的bug.这bug在这个例子中,下面的URL: https://access.redhat.co ...