[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 ...
随机推荐
- 【vim】把当前文件转化为网页
这会生成一个 HTML 文件来显示文本,并在分开的窗口显示源代码: :%TOhtml (译者注:原文是 :%Tohtml,但在我的电脑上是 :%TOhtml) 转载自:https://linux.cn ...
- script & scriptreplay
script是什么 scirpt就是一个命令,可以制作一份记录输出到终端的记录.对于那些想要真实记录终端会话的人来说,这很有用.该记录可以保存并在以后再打印出来. 怎么用 默认情况下,我们可以通过在终 ...
- emmc基础技术8:操作模式2-device identification mode
1.前言 eMMC总线操作包含: boot mode, device identification mode interrupt mode data transfer mode 本文主要描述devic ...
- ARMV8 datasheet学习笔记4:AArch64系统级体系结构之Generic timer
1.前言 2.generate timer 2.1 概述 提供了一个系统计数器,用来实时测量流逝的时间: 提供了一个虚拟计数器,用来测量某个虚拟机上流逝的虚拟时间: 定时器,每隔一段时间会触发事件,支 ...
- 工作中bug笔记
1.报Cannot read property indexOf of undefined 错误的时候!!!报这种错的时候,一般是因为indexOf前面检查的东西是不存在的!!!!! 2.使用< ...
- Python3学习笔记18-访问限制
在Class内部,可以有属性和方法,而外部代码可以通过直接调用实例变量的方法来操作数据,这样,就隐藏了内部的复杂逻辑. 但是,从Student类的定义来看,外部代码还是可以自由地修改一个实例的name ...
- Ex 5_26 变量约束是否能同时满足(并查集)_第九次作业
利用并查集进行处理,定义一个维护数组components,components[i]表示变量序号为i的变量所处的集合,首先处理相等的变量,把它们放入同一个集合中,最后再处理不相等变量,若两个不相等的变 ...
- ThinkPHP 3.1,3.2中对IN和BETWEEN正则匹配不当导致的一个SQLi
// where子单元分析 protected function parseWhereItem($key,$val) { $whereStr = ''; if(is_array($val)) { if ...
- SpringBoot入门小案例
1.创建一个简单的maven project项目 2.下面来看一下项目结构: 3.pom.xml 配置jar包 <parent> <groupId>org.springfram ...
- java :: Java中的双冒号操作符
java中的双冒号操作符 定义 双冒号运算操作符是类方法的句柄,lambda表达式的一种简写,这种简写的学名叫eta-conversion或者叫η-conversion. 通常的情况下: 把 x -& ...