[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 ...
随机推荐
- Linux中THIS_MODULE宏定义详解
一直都在耿耿于怀,这个THIS_MODULE到底是个什么玩意,linux内核中无处不在的东西.今天上网搜了一下,算是基本明白了.网上牛人写的已经比较详细,另外目前暂时没有时间往更深层次分析,所以直接贴 ...
- windows命令行中英文切换
Windows下cmd命令提示符窗口的语言设置(中英) 打开cmd命令提示窗口 输入 chcp 936 使用ping 命令 显示中文 2 同样 输入chcp 437 3 使用ping 命令
- centos7版本设置OS启动默认进入图形界面还是文本界面
相比7之前的版本,在centos7版本中,设置OS启动默认进入图形界面还是文本界面有了点变化 检查当前默认设置 [root@rems2 ~]# systemctl get-default graphi ...
- openstack 安全策略权限控制等api接口
computer API: 创建安全组 /os-security-groups 创建安全组规则 /os-security-group-default-rules Netw ...
- 【转】OpenCV—imread读取数据为空
之前遇到一个很郁闷的问题,因为从用OpenCV2.3.1改成OpenCV2.4.4,开始改用Mat和imread来代替Iplimage和cvLoadImage,出了点小问题:imread读入数据总是为 ...
- php封装的sqlite操作类
sqlite在php中是默认安装的本地小型化数据库,类似于xml的小型数据库,但sqlite功能更强. sqlite.class.php文件: <?php class sqliteDB{ pri ...
- (网络编程)socketserver模块服务端实现并发
基于tcp的套接字(实现并发),关键就是两个循环,一个链接循环,一个通信循环 基于udp的套接字(不是正真意义上的并发,实现真并发) socketserver模块中分两大类:server类(解决链接问 ...
- python操作haproxy.cfg文件
需求 1.查 输入:www.oldboy.org 获取当前backend下的所有记录 2.新建 输入: arg = { 'bakend': 'www.oldboy.org', 'record':{ ' ...
- SeaJS入门教程系列之完整示例(三)
一个完整的例子上文说了那么多,知识点比较分散,所以最后我打算用一个完整的SeaJS例子把这些知识点串起来,方便朋友们归纳回顾.这个例子包含如下文件: 1.index.html——主页面.2.sea.j ...
- 用Python实现Excel的读写
一.读excel文件的简单示例 #!/usr/bin/env python # -*- coding:utf-8 -*- import xlrd from xlrd.book import Book ...