题目链接

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 【最小生成树】的更多相关文章

  1. HDU 1598 find the most comfortable road(最小生成树之Kruskal)

    题目链接: 传送门 find the most comfortable road Time Limit: 1000MS     Memory Limit: 32768 K Description XX ...

  2. HDU 1598 find the most comfortable road 并查集+贪心

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1598 find the most comfortable road Time Limit: 1000 ...

  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(枚举+卡鲁斯卡尔最小生成树)

    find the most comfortable road Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K ...

  5. HDU 1589 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 (MST)

    find the most comfortable road Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d ...

  8. hdu 1598 find the most comfortable road(并查集+枚举)

    find the most comfortable road Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K ...

  9. HDU 1598 find the most comfortable road(枚举+并查集,类似于最小生成树)

    一开始想到用BFS,写了之后,发现有点不太行.网上查了一下别人的解法. 首先将边从小到大排序,然后从最小边开始枚举,每次取比它大的边,直到start.end属于同一个集合,即可以连通时停止.过程类似于 ...

随机推荐

  1. Nginx:HTTP框架是如何介入请求

    参考资料 <深入理解Nginx>(陶辉) Nginx事件模块博客地址:http://www.cnblogs.com/runnyu/p/4914698.html Nginx是一个事件驱动构架 ...

  2. DB2 锁问题分析与解释

    DB2 锁问题分析与解释 DB2 应用中常常会遇到锁超时与死锁现象,那么这样的现象产生的原因是什么呢.本文以试验的形式模拟锁等待.锁超时.死锁现象.并给出这些现象的根本原因. 试验环境: DB2 v9 ...

  3. EasyUI datagrid 过滤事件段

    //dateTimeRange过滤扩展 $.extend($.fn.datagrid.defaults.filters, { dateRange: { /*onInit: function(targe ...

  4. 基于React的PC网站前端架构分析

    代码地址如下:http://www.demodashi.com/demo/12252.html 本文适合对象 有过一定开发经验的初级前端工程师: 有过完整项目的开发经验,不论大小: 对node有所了解 ...

  5. ASP.NET MVC深入浅出(被替换) 第一节: 结合EF的本地缓存属性来介绍【EF增删改操作】的几种形式 第三节: EF调用普通SQL语句的两类封装(ExecuteSqlCommand和SqlQuery ) 第四节: EF调用存储过程的通用写法和DBFirst模式子类调用的特有写法 第六节: EF高级属性(二) 之延迟加载、立即加载、显示加载(含导航属性) 第十节: EF的三种追踪

    ASP.NET MVC深入浅出(被替换)   一. 谈情怀-ASP.NET体系 从事.Net开发以来,最先接触的Web开发框架是Asp.Net WebForm,该框架高度封装,为了隐藏Http的无状态 ...

  6. BNU 34990 Justice String (hash+二分求LCP)

    思路:枚举第一个字符串的位置,然后枚举最长公共前缀的长度,时间即会下降-- #pragma comment(linker, "/STACK:1024000000,1024000000&quo ...

  7. lua学习笔记(三)

    类型与值   lua是一种动态类型的语言,在语言中没有类型定义的语法,每个值都携带了它自身的类型信息   lua中有8种基础类型     nil       只与自身相等assert(nil==nil ...

  8. DDD架构Sample

    http://dddsamplenet.codeplex.com/SourceControl/latest#DDDSample-Vanilla/Application/IBookingService. ...

  9. [译]GLUT教程 - 移动镜头1

    Lighthouse3d.com >> GLUT Tutorial >> Input >> Move the Camera I 下面来看一个更有趣的GLUT应用.本 ...

  10. Centos7 install RabbitMQ

    安装rabbitmq 需要环境上有erlang,没有安装的可以参照下面的内容进行安装: https://www.erlang-solutions.com/resources/download.html ...