思路:

首先如果想到了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. 神经网络和BP算法推导

    注意:绘画太难了,因为他们画,本文中的所有插图来自基本算法饺子机类.请勿转载 1.习模型: 事实上,基本上全部的基本机器学习模型都能够概括为下面的特征:依据某个函数,将输入计算并输出. 图形化表示为下 ...

  2. 使用exp&imp工具进行数据库备份及恢复

    使用exp&imp工具进行数据库备份及恢复1.exp/imp使用方法介绍exp/imp为一种数据库备份恢复工具,也可以作为不同数据库之间传递数据的工具,两个数据库所在的操作系统可以不同.exp ...

  3. C++ STL set集合容器

    汇总了一些set的常用语句,部分参考了这篇:http://blog.163.com/jackie_howe/blog/static/199491347201231691525484/ #include ...

  4. C# ashx生成的验证码

    public void ProcessRequest(HttpContext context) { context.Response.ContentType = "image/jpeg&qu ...

  5. excel导入mssql数据库,支持excel2003--2010文件格式

    rt,简单的excel导入mssql.未做性能及海量数据优化,一般单表5000左右条数据导入适用. 源码非原创,来源于之前搜集整理,原作者无从考究,如有版权问题请留言注明. 看代码.前台页面是一个Fi ...

  6. hdu 1232

    以前写的.....拿出来看看.... 并查集模板: #include <iostream> #include <string> using namespace std; int ...

  7. 不容错过的20段CSS代码

    Web开发技术每年都在革新,浏览器已逐渐支持CSS3特性,并且网站设计师和前端开发者普遍采用这种新技术进行设计与开发.但仍然有一些开发者迷恋着一些CSS2代码. 分享20段非常专业的CSS2/CSS3 ...

  8. Linq使用GroupBy筛选数据

    StringBuilder sb = new StringBuilder(); List<IGrouping<string, modle>> listRepeat = mode ...

  9. MeasureSpec学习

    在自定义View和ViewGroup的时候,我们经常会遇到int型的MeasureSpec来表示一个组件的大小,这个变量里面不仅有组件的尺寸大小,还有大小的模式. 这个大小的模式,有点难以理解.在系统 ...

  10. CodeSMART for VS.NET插件工具

    今天无聊,想起以前看过的微软的Visual Studio的插件,所以就找了找. 微软的Visual Studio本身就非常强大了,但是仍然有不足的地方,比如下面要介绍的我喜欢的代码格式化功能的这个插件 ...