[HDU1598]find the most comfortable road
思路:
考虑一个暴力:枚举最大的边权和最小的边权,然后将边权在这之间的边全拿出来构成一张无向图,剩下的就是判断是否存在一条从$S$到$T$的路径。
相当于判$S$和$T$是否连通,用并查集连一下即可。
时间复杂度:O(m³α(n))
考虑优化,枚举了最小边权之后,从小到大枚举最大边权,每次能新添加一条边。
因为并查集是支持动态加边的,所以复杂度就降到O(m²α(n))了。
一开始存边的vector忘记每次清零,一直Wrong Answer。
#include<cstdio>
#include<vector>
#include<algorithm>
const int inf=0x7fffffff;
struct Edge {
int from,to,w;
bool operator < (const Edge &x) const {
return w<x.w;
}
};
std::vector<Edge> e;
inline void add_edge(const int u,const int v,const int w) {
e.push_back((Edge){u,v,w});
}
const int N=;
class DisjointSet {
private:
int anc[N];
int Find(const int x) {
return x==anc[x]?x:anc[x]=Find(anc[x]);
}
public:
void reset(const int n) {
for(int i=;i<=n;i++) anc[i]=i;
}
bool isConnected(const int x,const int y) {
return Find(x)==Find(y);
}
void Union(const int x,const int y) {
anc[Find(x)]=Find(y);
}
};
DisjointSet s;
int main() {
int n,m;
while(~scanf("%d%d",&n,&m)) {
e.clear();
while(m--) {
int s,t,w;
scanf("%d%d%d",&s,&t,&w);
add_edge(s,t,w);
}
std::sort(e.begin(),e.end());
int q;
scanf("%d",&q);
while(q--) {
int S,T;
scanf("%d%d",&S,&T);
int ans=inf;
for(unsigned i=;i<e.size();i++) {
s.reset(n);
for(unsigned j=i;j<e.size();j++) {
int &u=e[j].from,&v=e[j].to;
if(!s.isConnected(u,v)) {
s.Union(u,v);
if(s.isConnected(S,T)) {
ans=std::min(ans,e[j].w-e[i].w);
break;
}
}
}
}
printf("%d\n",ans!=inf?ans:-);
}
}
return ;
}
[HDU1598]find the most comfortable road的更多相关文章
- HDU-1598 find the most comfortable road
find the most comfortable road Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K ...
- HDU1598 find the most comfortable road 【并查集】+【枚举】
find the most comfortable road Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K ...
- hdu1598 find the most comfortable road (枚举)+【并查集】
<题目链接> 题目大意: XX星有许多城市,城市之间通过一种奇怪的高速公路SARS(Super Air Roam Structure---超级空中漫游结构)进行交流,每条SARS都对行驶在 ...
- hdu1598 find the most comfortable road 枚举+最小生成树
#include<cstdio> #include<cstring> #include<algorithm> #define MAXN 210 #define IN ...
- find the most comfortable road(hdu1598)不错的并查集
find the most comfortable road Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K ...
- 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(枚举+卡鲁斯卡尔最小生成树)
find the most comfortable road Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K ...
- 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 (MST)
find the most comfortable road Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d ...
随机推荐
- 利用capability特征加强Linux系统安全【转】
转自:https://blog.csdn.net/fivedragon/article/details/676849 1.简介 UNIX是一种安全操作系统,它给普通用户尽可能低的权限,而把全部的系统权 ...
- Linux内核驱动之GPIO子系统(一)GPIO的使用【转】
转自:http://blog.csdn.net/tommy_wxie/article/details/9427047 一 概述 Linux内核中gpio是最简单,最常用的资源(和 interrupt ...
- Visual Studio 2017 + Python3.6安装scipy库
Windows10下安装scipy很麻烦,直接在命令行下使用pip install scipy无法安装,但可以借助VS2017的集成环境来安装. (1)首先在Visual Studio Install ...
- C:详解C中volatile关键字
原文地址:http://www.cnblogs.com/yc_sunniwell/archive/2010/06/24/1764231.html volatile提醒编译器它后面所定义的变量随时都有可 ...
- Android数据存储:Shared Preferences
Android数据存储之SharedPreferences 在Android系统中提供了多种存储技术.通过这些存储技术可以将数据存储在各种存储介质上, Android 为数据存储提供了如下几种方式:1 ...
- Ex 6_12 凸多边形的最优三角剖分..._第六次作业
假设顶点的总数为n,从0到n-1. 从序号为0的顶点开始以逆时针方向排序,对于 令子问题A[i,j]为包含顶点i,i+1, . . . j的凸多边形的最小三角剖分代价,dist(i,j)为顶点i到顶点 ...
- Python-CSS进阶
0. 什么时候该用什么布局 <!-- 定位布局: 以下两种布局不易解决的问题, 盒子需要脱离文档流处理 --> <!-- 浮动布局: 一般有block特性的盒子,水平排列显示 --& ...
- 1、Appium安装
1.安装node.js 访问node js官网 https://nodejs.org/en/ 下载并安装node js,找到你系统适合的node js一步步安装即可 2.安装Appium 在cmd中执 ...
- Oracle 网络配置与管理
[学习目标] Oracle 监听器是一个服务器端程序,用于监听所有来自客户端的请求,并为其提供数 据库服务.因此对监听器的管理与维护相当重要. 本章主要内容是描述对Oracle 监听器 ...
- bzoj1150 堆应用,好题
#include<bits/stdc++.h> using namespace std; #define maxn 100005 #define INF 0x3fffffff #defin ...