HDU - 1598 find the most comfortable road 【最小生成树】
题目链接
http://acm.hdu.edu.cn/showproblem.php?pid=1598
思路
用kruskal 算法
将边排序后 跑 kruskal
然后依次将最小边删除 再去跑 kruskal 直到不能成功跑成通路
为什么要删掉最小边 因为边是按从小到大排序的
那么也就是说 我每次加入的边 都是必须加入的 最小的边
那么如果 最高速与最低速的差 还大了
我就要让尽量大的边去跑 kruskal
举个栗子吧。。
假设存在一系列边
1 2 3 4 5 5 7 8 9
排序后是这样的
假如我用 1 2 3 4 5 这五条边 可以跑成通路 这五条边的 最高速-最低速就是 4
那么当我删去1 这条边后 我再去跑 发现 2 3 4 5 5 这五条边也可以跑成通路,那么最高速-最低速就是3
然后发现答案其实是更优的 也存在一点贪心的思想
AC代码
#include <cstdio>
#include <cstring>
#include <ctype.h>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <deque>
#include <vector>
#include <queue>
#include <string>
#include <map>
#include <stack>
#include <set>
#include <list>
#include <numeric>
#include <sstream>
#include <iomanip>
#include <limits>
#define CLR(a, b) memset(a, (b), sizeof(a))
#define pb push_back
#define bug puts("***bug***");
#define fi first
#define se second
#define stack_expand #pragma comment(linker, "/STACK:102400000,102400000")
//#define bug
//#define gets gets_s
using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
typedef pair <string, int> psi;
typedef pair <string, string> pss;
typedef pair <double, int> pdi;
const double PI = acos(-1.0);
const double E = exp(1.0);
const double eps = 1e-8;
const int INF = 0x3f3f3f3f;
const int maxn = 1e3 + 10;
const int MOD = 6;
int n, m;
int pre[maxn];
int st, ed;
int find(int x)
{
while (x != pre[x])
x = pre[x];
return x;
}
void join(int x, int y)
{
int fx = find(x), fy = find(y);
if (fx != fy)
pre[fx] = fy;
}
struct node
{
int u, v, w;
node(int _u = 0, int _v = 0, int _w = 0) : u(_u), v(_v), w(_w) {}
bool operator < (const node& r) const
{
return w < r.w;
}
};
node edge[maxn];
int tot;
void addedge(int u, int v, int w)
{
edge[tot].u = u;
edge[tot].v = v;
edge[tot].w = w;
tot++;
}
int Kruskal(int n, int vis)
{
for (int i = 0; i < n; i++)
pre[i] = i;
int Min = edge[vis].w;
int Max;
for (int i = vis; i < tot; i++)
{
node u = edge[i];
if (find(u.u) != find(u.v))
{
join(u.u, u.v);
Max = u.w;
}
if (find(st) == find(ed))
return Max - Min;
}
return -1;
}
int main()
{
while (scanf("%d%d", &n, &m) != EOF)
{
tot = 0;
int u, v, w;
for (int i = 0; i < m; i++)
{
scanf("%d%d%d", &u, &v, &w);
addedge(u, v, w);
}
sort(edge, edge + tot);
int q;
cin >> q;
while (q--)
{
scanf("%d%d", &st, &ed);
int ans = INF;
for (int i = 0; i < tot; i++)
{
int vis = Kruskal(n, i);
if (vis == -1)
break;
ans = min(ans, vis);
}
printf("%d\n", ans == INF ? -1 : ans);
}
}
}
HDU - 1598 find the most comfortable road 【最小生成树】的更多相关文章
- 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 并查集+贪心
题目链接: 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 find the most comfortable road Time Limit: 1000/ ...
- 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 1589 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 (MST)
find the most comfortable road Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d ...
- 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(枚举+并查集,类似于最小生成树)
一开始想到用BFS,写了之后,发现有点不太行.网上查了一下别人的解法. 首先将边从小到大排序,然后从最小边开始枚举,每次取比它大的边,直到start.end属于同一个集合,即可以连通时停止.过程类似于 ...
随机推荐
- 仿新浪首页、主题、详情页,纯html静态页面
仿新浪首页.主题.详情页.纯html静态页面,下载地址: http://download.csdn.net/detail/sweetsuzyhyf/8085535
- NYOJ-277-车牌号
车牌号 时间限制:3000 ms | 内存限制:65535 KB 难度:1 描写叙述 茵茵非常喜欢研究车牌号码,从车牌号码上能够看出号码注冊的早晚,据研究发现,车牌号码是按字典序发放的,如今她收集 ...
- 我是怎样理解web页面的
事实上web页面包括三部分东东 1.页面展示的元素(HTML) 2.页面元素展示的样式(CSS) 3.控制页面元素的交互(JavaScript) 不管页面多么复杂,从这三方面去看,都会得到清晰的认识的 ...
- 公网通过代理访问阿里云vpc redis
前提条件 如果您需要从本地 PC 端访问 Redis 实例进行数据操作,可以通过在 ECS 上配置端口映射或者端口转发实现.但必须符合以下前提条件: 若 Redis 实例属于专有网络(VPC),ECS ...
- GridControl 二次封装,自定义颜色样式风格
1.自定义颜色格式,分组,筛选 1.封装类 必须引用类库 using System; using System.Collections.Generic; using System.Linq; usin ...
- jvm性能调优常用命令
说明和名词解释: ① 只有进行的运行用户才可以调用命令查看相关信息 ② [pid] 为需要查看的进程的端口号 ③ [file] 为需要导出到的文件的具体地址 ④ [tid] 进程中线程的id 1 ...
- Mongodb基本操作入门,增删改查和索引
主要进程 mongod.exe为启动数据库实例的进程. mongo是一个与mongod进程进行交互的JavaScript shell进程,它提供了一些交互的接口函数用户对数据库的管理. 基本命令 sh ...
- centos7 配置ssh 免密码登陆
我只有一台机器,是因为要配置hadoop分布式环境用,需要配置ssh 两个用户: zhangxs, root 首先在切换到zhangxs用户下 执行[ ssh-keygen -t rsa] [zhan ...
- Mybatis_遇到的问题汇总
1.The setting logImpl is not known 我在参考某个网站学习mybatis时,出现这个错误,后来找到的原因是因为mybatis的版本(3.1.1)太低,换成3.3.1就没 ...
- Huffman编码(Huffman树)
[0]README 0.1) 本文总结于 数据结构与算法分析, 源代码均为原创, 旨在 理解 "Huffman编码(Huffman树)" 的idea 并用源代码加以实现: 0.2) ...