***图***

解题思路:这题的原题似乎好像是NOI某年的题目,然后数据改水了

于是就可以用一些简单的最短路算法水掉.

因为他是要求max(a)+max(b)的值,所以单纯的最短路是不行的

我们可以枚举最大的a值,即能走的边a值要小于这个限制,然后对b跑一遍最短路,每次更新答案

当然这是我的辣鸡做法,只能满足这道题的数据,更优越的算法是用lct来维护

读者可以去各大OJ做 魔法森林这道题

https://www.luogu.org/problem/show?pid=2387 在此只贴了luogu的网址

 %:pragma GCC optimize()
#include<bits/stdc++.h>
using namespace std;
const int N=;
int to[N],fst[N],nxt[N],fa[N],t[N];
long long dis[N],a[N],b[N],ans=1e13;
int x,y,aa,bb,tot=,n,m;
bool vis[N];
inline void add(int x,int y,int aa,int bb){
to[++tot]=y; nxt[tot]=fst[x]; fst[x]=tot; a[tot]=aa; b[tot]=bb;
}
inline int ask(int x){
if (fa[x]==x) return x; fa[x]=ask(fa[x]); return fa[x];
}
struct cmp{bool operator ()(int a,int b){return dis[a]>dis[b];}};
priority_queue <int,vector<int>,cmp> q;
inline void dij(int lim){
for (int i=;i<=n;++i) dis[i]=1e13;
memset(vis,,sizeof(vis));
dis[]=; q.push();
while (!q.empty()){
int t=q.top(); q.pop();
if (vis[t]) continue; vis[t]=;
for (int i=fst[t];i;i=nxt[i])
if (a[i]<=lim&&dis[to[i]]>max(dis[t],b[i]))
dis[to[i]]=max(dis[t],b[i]),q.push(to[i]);
}
} int main(){
scanf("%d%d",&n,&m);
for (int i=;i<=n;++i) fa[i]=i;
for (int i=;i<=m;++i){
scanf("%d %d %d %d",&x,&y,&aa,&bb);
add(x,y,aa,bb); add(y,x,aa,bb); fa[ask(x)]=ask(y);
}
if (ask(fa[])!=ask(fa[n])){
printf("-1"); return ;
}
for (int i=;i<=tot;++i){
dij(a[i]); ans=min(ans,a[i]+dis[n]);
}
if (ans>=1e13) printf("-1");
else printf("%lld\n",ans);
}

总结:这道题其实也可以从最小生成树的思路考虑,一题多解,

这题作为提高组还是比较适宜的,想要练习最短路的同学,可以练练

当然更厉害的,也可以想想更优越的算法

最新文章

  1. beanstalkd 消息队列
  2. Oracle导出导入数据库的方式
  3. 一个js(javascript)使用案例
  4. Spring MVC的启动过程
  5. Spring Quartz结合Spring mail定期发送邮件
  6. SecureCRT自动登陆到服务器的脚本以及脚本编写简单说明
  7. Apache与php的整合(经典版),花了一天去配置成功经验
  8. bash shell学习-shell script基础 (笔记)
  9. php几个不起眼儿的小技巧
  10. 站点接入QQ登录
  11. BootStrap -- Grid System
  12. VMWare 11安装操作系统 - 初学者系列 - 学习者系列文章
  13. tostring方法
  14. if处理多分支结构
  15. Weblogic之简介
  16. 使用clipboard.js实现复制内容至剪贴板
  17. form表单获取多选的值
  18. linux-nc命令介绍
  19. python3 + selenium 之文件上传下载
  20. gateone安装使用

热门文章

  1. 三维重建7:Visual SLAM算法笔记
  2. Window8.1下安装Matplotlib库
  3. Java_Jdbc_连接mysql数据库_1.打通数据库
  4. MySQL NULL 值如何处理?
  5. Centos 修改主机名称
  6. vue+ElementUI 日期选择器 获取时间戳
  7. (34)Spring Boot的启动器Starter详解【从零开始学Spring Boot】
  8. (31)Spring Boot导入XML配置【从零开始学Spring Boot】
  9. elasticsearch 文档阅读笔记(三)
  10. 转载 - Pinyin4j的基本用法