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属于同一个集合,即可以连通时停止.过程类似于 ...
随机推荐
- 【Excle数据透视表】如何新建数据透视表样式
如果觉得Excle给出的数据透视表样式不符合自己的心意,可以自己定义一个数据透视表样式 步骤1 单击数据透视表区域任意单元格→数据透视表工具→设计→样式组中的下拉按钮,打开数据透视表样式库→新建数据透 ...
- 网页抓取工具Teleport Ultra简介及如何使用
Teleport Ultra是一款专业的离线浏览器,能够快速.准确地从网络抓取数据并保存到本地,实现离线浏览的目的.它可以从Internet的任何地方抓回你想要的任何文件,它可以在你指定的时间自动登录 ...
- SQLServer 2017安装时的错误:Polybase要求安装Oracle JRE 7更新51或更高版本
2016应该也有这个问题 下载JDK7就可以了(我装10是不可以解决的) 重新运行下 安装完后再安装SSMS 附: MS SQL SERVER 2017全套下载地址(含JDK7.SSMS.KEY): ...
- cxf + spring + maven 开发webservice
1.maven 配置 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://ww ...
- centos关机与重启命令详解
Linux centos关机与重启命令详解与实战 Linux centos重启命令: 1.reboot 普通重启 2.shutdown -r now 立刻重启(root用户使用) 3.shutdo ...
- erlang 求N以内的质数
素数,又称质数,在一个大于1的自然数中,除了1和此整数自身之外,不能被其他自然数整除的数. 比1大但不是素数的数称为合数. 1和0既不是素数,也不是合数. 算术基本定理证明每个大于1的正整数都可以写成 ...
- Android BroadcastReceiver介绍 (转)
原文地址:http://www.cnblogs.com/trinea/archive/2012/11/09/2763182.html 本文主要介绍BroadcastReceiver的概念.使用.生命周 ...
- Shell Error: -bash: ./test.sh: /bin/bash^M: bad interpreter: No such file or directory (转)
错误原因可能有以下几种: 1.在WIN底下用文本编辑工具修改过参数变量,在保存的时候没注意编码格式造成的, 2.也有可能是在VIM里修改,第一行末尾按到ctrl_v 查看文件是DOS格式.UNIX格式 ...
- GoogleMap-------API KEY申请流程
前言:此文是关于Google Maps Android API v2 KEY的申请流程介绍. 1.首先访问https://code.google.com/apis/console/?pli=1#pro ...
- centos7.0 安装redis 3.2.9
wget http://download.redis.io/releases/redis-3.2.9.tar.gz tar xzf redis-3.2.9.tar.gz cd redis-3.2.9 ...