思路:

首先如果想到了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. Robotium--takeScreenshot(截图)

    在Robotium中,截图的方法时调用takeScreenshot(). 但有使用你会发现明明代码里调用了solo.takeScreenshot(),但却没有截图成功,那是因为被测试的应用没有SD卡的 ...

  2. jQuery源代码 解析一 工具方法

    1. 外层沙箱以及命名空间$ 几乎稍微有点经验前端人员都这么做,为了避免声明了一些全局变量而污染,把代码放在一个"沙箱执行",然后在暴露出命名空间(可以为API,函数,对象): 2 ...

  3. linux开关机命令

    1.reboot重启 2.shutdown -r now 立即重启 root用户使用,与reboot命令相同 3.shutdown -r 10 过10分钟后重启root用户使用 4.shutdown ...

  4. 安装android studio 出现的路径问题 tools.jar' seems to be not in Android Studio classpath

    尝试一下android studio  ,谁知出现路径问题 'tools.jar' seems to be not in Android Studio classpath. Please ensure ...

  5. Linux shell入门基础(一)

    Linux shell入门基础(一): 01.增加删除用户: #useradd byf   userdel byf(主目录未删除)  userdel -r byf   该用户的属性:usermod 用 ...

  6. Android Studio 安装

    准备: JDK 7以及以上版本. Android Studio安装文件  中文站下载 http://www.android-studio.org/index.php/download exe ,包含S ...

  7. [HAOI2006]聪明的猴子

    /* 找出能连通所有点的一棵树 是的最大的边最小 很显然就是最小生成树. 堆优化prim. */ #include<iostream> #include<cstring> #i ...

  8. php进程继续执行

    虽然浏览器提示localhost 的服务器响应时间过长.但是进程在后台继续执行,数据库的条数在增加.

  9. (转)DevExpress GridView属性设置

    GirdControl是数据的容器,它包含多种显示方式,GridView则是一种二维表格视图. 绑定数据源: List<Student> list = new List<Studen ...

  10. ICSharpCode.SharpZipLib实现压缩解压缩

    最近,在项目中经常需要处理压缩和解压缩文件的操作.经过查找,发现了ICSharpCode.SharpZipLib.dll ,这是一个完全由c#编写的Zip, GZip.Tar . BZip2 类库,可 ...