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属于同一个集合,即可以连通时停止.过程类似于 ...
随机推荐
- 尖峰冲击测试(spike Testing)
与可靠性测试类似,尖峰冲击测试这种方法也是从其他行业借鉴而来.在电力工业,有一种冲击测试,用来验证设备在刚刚接通电源时能否经受住涌流的破坏.所谓涌流,通俗地说,就是电源接通瞬间,电流突然变大的现象.涌 ...
- 约瑟夫环用php实现
百度百科的解释:约瑟夫环(约瑟夫问题)是一个数学的应用问题:已知n个人(以编号1,2,3...n分别表示)围坐在一张圆桌周围.从编号为k的人开始报数,数到m的那个人出列:他的下一个人又从1开始报数,数 ...
- SpringSecurity---javaconfig:Hello Web Security
© 版权声明:本文为博主原创文章,转载请注明出处 本文根据官方文档加上自己的理解,仅供参考 官方文档:https://docs.spring.io/spring-security/site/docs/ ...
- Winform 动态 画图 不闪
一.问题:解决winform动态画图闪的问题,网上搜的方法,大部分都是: “this.SetStyle(ControlStyles.OptimizedDoubleBuffer | ControlSty ...
- mybatis 单一参数时的动态语句
public void getBookList(String publisher,String author){ Map<String,Object> maps = new HashMap ...
- cxf + spring + maven 开发webservice
1.maven 配置 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://ww ...
- Java以指定格式输入数字
package com.ylx; import java.text.DecimalFormat; public class Test { public static void main(String[ ...
- Google Code Jam 2014 资格赛:Problem D. Deceitful War
This problem is the hardest problem to understand in this round. If you are new to Code Jam, you sho ...
- Codeforces 38G Queue 伸展树
题目链接:点击打开链接 题意: 给定n个人来排队 每一个人有2个參数.身份优先级和脸皮厚度 == 来的那个人会排到队尾 假设这个人的优先级比他前面那个人的优先级大就会和前面那个人交换位置. 交换一次脸 ...
- 什么是 Service Mesh
作者|敖小剑 微服务方兴未艾如火如荼之际,在 spring cloud 等经典框架之外,Service Mesh 技术正在悄然兴起.到底什么是 Service Mesh,它的出现能带来什么,又能改变什 ...