HDU-1598-find the most comfortable road
来源:互联网 发布:adidas跑鞋系列 知乎 编辑:程序博客网 时间:2024/06/08 05:01
ACM模版
描述
题解
克鲁斯卡尔+并查集……
代码
#include <cstdio>#include <iostream>#include <algorithm>using namespace std;const int MAXN = 210; // 最大点数const int MAXM = 1010; // 最大边数const int INF = 0x3f3f3f3f;int pre[MAXN]; // 并查集使用struct Edge{ int u; // 起点 int v; // 终点 int w; // 权值} edge[MAXM]; // 存储边的信息bool cmp(Edge a, Edge b){ // 排序函数,将边按照权值从小到大排序 return a.w < b.w;}int find(int x){ if (pre[x] == x) { return x; } else { return pre[x] = find(pre[x]); }}void join(int x, int y){ int fx = find(x); int fy = find(y); if (fx != fy) { pre[fx] = fy; }}void init(int n){ for (int i = 0; i <= n; i++) { pre[i] = i; }}int main(){ int n, m; int Q; int start, end; while (scanf("%d%d", &n, &m) != EOF) { for (int i = 0; i < m; i++) { scanf("%d%d%d", &edge[i].u, &edge[i].v, &edge[i].w); } sort(edge, edge + m, cmp); scanf("%d", &Q); while (Q--) { int res = INF; int temp; scanf("%d%d", &start, &end); for (int i = 0; i < m; i++) { init(n); temp = INF; int flag = 1; for (int j = i; j < m; j++) { join(edge[j].u, edge[j].v); if (find(start) == find(end)) { temp = edge[j].w - edge[i].w; flag = 0; break; } } if (flag) { break; } res = min(res, temp); } if (res == INF) { cout << "-1\n"; } else { cout << res << '\n'; } } } return 0;}
参考
《最小生成树(森林)》
0 0
- find the most comfortable road hdu 1598
- HDU-1598-find the most comfortable road
- hdu 1598 find the most comfortable road
- hdu 1598 find the most comfortable road
- HDU 1598 find the most comfortable road
- HDU 1598 find the most comfortable road
- hdu 1598 find the most comfortable road
- HDU 1598 find the most comfortable road
- hdu 1598 find the most comfortable road
- HDU 1598 find the most comfortable road
- hdu 1598 find the most comfortable road
- HDU 1598 find the most comfortable road
- hdu 1598 find the most comfortable road
- hdu 1598 find the most comfortable road
- HDU 1598 find the most comfortable road
- HDU 1598 ( find the most comfortable road )
- HDU 1598 find the most comfortable road
- hdu 1598 find the most comfortable road
- ext4 学习笔记(三) Ext.window.Window(白鹤翔第一季)
- 【Lightoj 1414 - February 29 】
- UVA - 10870 Recurrences (矩阵快速幂)
- POJ-3281 Dining(最大流)
- dnotify监控文件行为
- HDU-1598-find the most comfortable road
- mac关于.bash_profile环境变量配置的问题
- 简单的停车场程序
- 关于进程虚拟地址为什么是4G的讨论
- 作用域(基础篇)
- uboot源码分析
- 关于杨辉三角的解决方案
- Git SSHl链接检出仓库出错,提示权限不足
- 【CODEVS】2618 核电站问题