hdu-1598
思路:
首先如果想到了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的更多相关文章
- 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 (并查集加贪心) 速度与激情
题目传送门:http://acm.hdu.edu.cn/showproblem.php?pid=1598 一道带有贪心思想的并查集 所以说像二分,贪心这类基础的要掌握的很扎实才行. 用结构体数组储存公 ...
- 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 思路 用kruskal 算法 将边排序后 跑 kruskal 然后依次将最小边删除 再去跑 kr ...
- 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 (并查集)
find the most comfortable road Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K ...
- 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 (MST)
find the most comfortable road Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d ...
- HDU 1598 find the most comfortable road(枚举+并查集,类似于最小生成树)
一开始想到用BFS,写了之后,发现有点不太行.网上查了一下别人的解法. 首先将边从小到大排序,然后从最小边开始枚举,每次取比它大的边,直到start.end属于同一个集合,即可以连通时停止.过程类似于 ...
- HDU 1598 find the most comfortable road (最小生成树) >>
Problem Description XX明星有许多城市,通过与一个陌生的城市高速公路SARS(Super Air Roam Structure---超级空中漫游结构)进行交流.每条SARS都对行驶 ...
随机推荐
- PHP 将Base64图片保存到 Sae storage
<?php $file_dir='tu/'.date("Y/m/d").'/'; $fileName=create_guid(); $storage = new SaeSto ...
- D3画图学习一
一.D3画图简介 D3 是最流行的可视化库之一,它被很多其他的表格插件所使用.它允许绑定任意数据到DOM,然后将数据驱动转换应用到Document中.你可以使用它用一个数组创建基本的HTML表格,或是 ...
- linux系统中中断已连接的用户
1.用w命令查看当前系统登录的用户 [root@rhel7 ~]# w :: up :, users, load average: 0.00, 0.01, 0.05 USER TTY FROM LOG ...
- python复制--笔记
对象引用: >>> songs = ["Bee","Core","Love"] >>> bat = so ...
- HTML5中class选择器属性的解释
设置有class属性值的元素,可以被css中的选择器调用,也可以在javascript中以getElementsByClassName()方法调用. 可以给各个元素添加class而且名称可以相同与id ...
- JAVA小项目之五子棋
五子棋V1.0 功能: 人人对战,人机对战(初级) 记录双方分数: 主要知识点: 二维坐标系中,各方向坐标的关系及规律. 效果图: 主框架类: package com.gxlee.wzq; /** * ...
- php防止重复提交问题
php防止重复提交问题 用户提交表单时可能因为网速的原因,或者网页被恶意刷新,致使同一条记录重复插入到数据库中,这是一个比较棘手的问题.我们可以从客户端和服务器端一起着手,设法避免同一表单的重复提交. ...
- java socket报文通信(一)socket的建立
java socket报文通信(一) socket的建立 今天来和大家分享一下java中如何使用socket进行通信.先来啰嗦两句,看看Tcp/ip和udp: TCP是Transfer Contro ...
- 如何动态修改grid的列名
有这样的需求,搜索时候会选择搜索类型,每种搜索类型展示的列名不一样 如何动态修改grid的列名 效果图:点击bColumn页面切换成bColumn 实现思路:通过grid的reconfigure方法, ...
- 手写总结:synchronized 和 lock 区别
1. synchronized 是jvm 层次的(可以会造成死锁), lock 可以写代码控制,一般在异常时在 finally 里可以 unlock 释放锁 2. lock 细度更细,synchro ...