思路:

首先如果想到了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. C++获得系统路径

    源码: [cpp] view plaincopy #include <Shlobj.h> #include <stdio.h> #include <locale.h> ...

  2. Java内部类实现伪方法级多线程

    最近碰到一个问题,就是用户在填写相关信息提交后,后台需要将一些文件同步到另外一台服务器,而这个时候,由于用的是spring的框架,导致前端页面需要等待文件同步完成,才能弹出提示信息.相信大家在很多时候 ...

  3. android ListView和GridView拖拽移位具体实现及拓展

    关于ListView拖拽移动位置,想必大家并不陌生,比较不错的软件都用到如此功能了.如:搜狐,网易,百度等,但是相比来说还是百度的用户体验较好,不偏心了,下面看几个示例:              首 ...

  4. Java Performance Optimization Tools and Techniques for Turbocharged Apps--reference

    Java Performance Optimization by: Pierre-Hugues Charbonneau reference:http://refcardz.dzone.com/refc ...

  5. [转] What is Ec/Io (and Eb/No)?

    PS:http://www.telecomhall.com/what-is-ecio-and-ebno.aspx If someone asks you "Which Signal Leve ...

  6. IntelliJ IDEA 中module的dependencies是其它module时的注意事项

    Dependencies on other modules If a module (module A) depends on another module (module B), IntelliJ ...

  7. 比较好的总结runtime

    http://www.cocoachina.com/ios/20160128/15154.html

  8. mvc和webapi同一解决方案调试办法

    今天在研究WebApi的时候,用mvc端直接请求webapi接口,发现怎么也请求不了,自己搞了半天,猜测可能是webapi没有完全启动吧,解决办法是将解决方案属性改为多启动项目,具体方法如下: 直接运 ...

  9. MySQL 5.6.19编译安装

    1.创建mysql安装目录.mysql用户 [root@serv19 ~]# mkdir -p /u01/mysql/data [root@serv19 ~]# groupadd mysql [roo ...

  10. 4、第4次课 CSS代码第三节课20150923

    1.列表li样式及取值意义 list-style-type : disc | circle | square | decimal | lower-roman | upper-roman | lower ...