思路:

首先如果想到了Kruskal算法,那么下一步我们可能马上会想:那我们就从头开始写这个算法吧。然后一写就很容易发现一个问题——如果按照正常的Kruskal算法来做,那么start到end的舒适度中的那个“最小边”就只能是所有边中最小的那个,而这是明显不符合逻辑的事情,所以我们就会接着想,如果不是这个最小边,那它会是哪个边——当然是更大的边了,于是我们便开始按照从小到大的顺序去依次遍历所有的边长,这是外层的for循环;然后对于内层的for循环呢,就是根据这个题目的要求来了,由于我们要求舒适度最高的,因此这一路下来我们再往上加边的时候一定是要尽量在现有的min的基础上尽量减小max的值,从而实现max-min最小。

AC代码:

#include <iostream>
#include <cstring>
#include <algorithm>
#define maxn 207
#define INF 9999999
using namespace std; int n,m;
int father[maxn];
int s[maxn];
struct edge{
int s;
int e;
int w;
};
int set_same(int x,int y)
{
int i,j;
for(i = x;i != father[i];i = father[i])
father[i] = father[father[i]];
for(j = y;j != father[j];j = father[j])
father[j] = father[father[j]];
return i==j?:;
}
void set_union(int x,int y)
{
int i,j;
for(i = x;i != father[i];i = father[i])
father[i] = father[father[i]]; for(j = y;j != father[j];j = father[j])
father[j] = father[father[j]];
if(s[i] < s[j]) {
father[i] = j;
s[i] += s[j];
}
else {
father[j] = i;
s[j] += s[i];
}
}
int set_find(int x)
{
int i;
for(i = x;i != father[i];i = father[i])
father[i] = father[father[i]];
return i;
}
void set_init()
{
for(int i = ;i <= n;i++){
father[i] = i;
s[i] = ;
}
} void Kruskal(int S,int E)
{
int dmax,dmin; } bool cmp(edge a,edge b)
{
return a.w<b.w;
} int main()
{
int i,j;
edge edges[];
while(cin>>n>>m)
{
for(i = ;i <= m;i++)
cin>>edges[i].s>>edges[i].e>>edges[i].w;
sort(edges+,edges++m,cmp);
int cast;
cin>>cast;
int S,E;
while(cast--)
{
cin>>S>>E;
int ans = INF;
for(i = ;i <= m;i++)
{
set_init();
for(j = i;j <= m;j++)
{
int A = edges[j].s;
int B = edges[j].e;
set_union(A,B);
if(set_same(S,E)) {
ans = min(ans,edges[j].w-edges[i].w);
break;
}
}
if(j == m) break;
}
if(ans == INF)
cout<<"-1"<<endl;
else
cout<<ans<<endl;
}
}
return ;
}

hdu-1598的更多相关文章

  1. HDU 1598 find the most comfortable road 并查集+贪心

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1598 find the most comfortable road Time Limit: 1000 ...

  2. hdu 1598 (并查集加贪心) 速度与激情

    题目传送门:http://acm.hdu.edu.cn/showproblem.php?pid=1598 一道带有贪心思想的并查集 所以说像二分,贪心这类基础的要掌握的很扎实才行. 用结构体数组储存公 ...

  3. hdu 1598 find the most comfortable road (并查集+枚举)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1598 find the most comfortable road Time Limit: 1000/ ...

  4. HDU - 1598 find the most comfortable road 【最小生成树】

    题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=1598 思路 用kruskal 算法 将边排序后 跑 kruskal 然后依次将最小边删除 再去跑 kr ...

  5. hdu 1598 find the most comfortable road(枚举+卡鲁斯卡尔最小生成树)

    find the most comfortable road Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K ...

  6. hdu 1598 find the most comfortable road (并查集)

    find the most comfortable road Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K ...

  7. HDU 1598 find the most comfortable road(最小生成树之Kruskal)

    题目链接: 传送门 find the most comfortable road Time Limit: 1000MS     Memory Limit: 32768 K Description XX ...

  8. HDU 1598 find the most comfortable road (MST)

    find the most comfortable road Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d ...

  9. HDU 1598 find the most comfortable road(枚举+并查集,类似于最小生成树)

    一开始想到用BFS,写了之后,发现有点不太行.网上查了一下别人的解法. 首先将边从小到大排序,然后从最小边开始枚举,每次取比它大的边,直到start.end属于同一个集合,即可以连通时停止.过程类似于 ...

  10. HDU 1598 find the most comfortable road (最小生成树) &gt;&gt;

    Problem Description XX明星有许多城市,通过与一个陌生的城市高速公路SARS(Super Air Roam Structure---超级空中漫游结构)进行交流.每条SARS都对行驶 ...

随机推荐

  1. Android 用ping的方法判断当前网络是否可用

    判断网络的情况中,有个比较麻烦的情况就是连上了某个网络,但是那个网络无法上网 ,,, = = 想到了用ping指令来判断,经测试,可行~ ~ ~ private static final boolea ...

  2. lubuntu12.11 source.list

    deb http://ports.ubuntu.com/ubuntu-ports/ precise main universe deb-src http://ports.ubuntu.com/ubun ...

  3. 阿里云linux的nginx下面配置多站点

    假设有服务器ip为 114.214.85.35 域名1为  www.jieshendada.cn 域名2为 www.jieshenxiaoxiao.cn 1.首先打开nginx域名配置文件存放目录:/ ...

  4. Zend Framework

    参考:http://www.php100.com/manual/ZendFramework/index.html 1.1. 概述 Zend Framework (ZF) 是一个开放源代码的 PHP5 ...

  5. oracle单行函数之字符函数

    Lower--转换为小写 upper--转换为大写 Initcap--首字母大写 concat--连接字符 substr--截取字符 length/length--获取字符串长度(字节长度) inst ...

  6. 使用2to3转换python代码

    如果要把python2编译器下的代码转换升级到python3编译器,可以使用python自带的 2to3工具进行转化: windows下转化: doc 命令窗口: >> python  C ...

  7. Oracle数据库之创建表空间与用户

    Oracle数据库之创建表空间与用户 一.创建表空间 基本语法表述: CREATE TABLESPACE tablespace_name [DATAFILE datafile_spec1 [,data ...

  8. 使用 phpMyAdmin无法登录mysql的问题

    今天使用使用phpmyadmin时出现了以下错误: (1)第一次时: 当配置文件config.inc.php里的配置项是: $cfg['Servers'][$i]['host'] = 'localho ...

  9. php.ini的中文解释

    [PHP]  ; PHP还是一个不断发展的工具,其功能还在不断地删减  ; 而php.ini的设置更改可以反映出相当的变化,  ; 在使用新的PHP版本前,研究一下php.ini会有好处的 ;;;;; ...

  10. PHP获取文件扩展名的多种方法

    PHP获取文件扩展名的N种方法. 第1种方法: function get_extension($file) { substr(strrchr($file, '.'), 1): } 第2种方法: fun ...