洛谷P1462 通往奥格瑞玛的道路

二分费用。

用血量花费建图,用单源最短路判断 \(1\) 到 \(n\) 的最短路花费是否小于 \(b\) 。二分时需要不断记录合法的 \(mid\) 值。

这里建议使用while(l <= r),会避免出现答案为 \(r\) 时和答案AFK搞混,样例就是这种情况。

复杂度为 \(O(\log n) \times\) 最短路的复杂度。

  • 二分 + Dijkstra版本,复杂度为 \(O(\log n \times E\log E)\) 。
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<queue> using namespace std; const long long inf = 1e9+7;
const int maxn = 10005;
const int maxm = 50005;
int n, m, l, r, ai, bi, tot, head[maxn], vis[maxn], f[maxn];
long long b, ci, dist[maxn];
struct node{
int to, nex;
long long cost;
}edge[2 * maxm];
struct G{
int id;
long long cost;
G(){}
G(int _id, long long _cost){
id = _id; cost = _cost;
}
/******/
bool operator < (const G & _G) const {
return cost > _G.cost;
}
}now; inline void addedge(int u, int v, long long w){
edge[++tot].to = v;
edge[tot].cost = w;
edge[tot].nex = head[u];
head[u] = tot;
}
int dijkstra(int c)
{
for(int i = 1; i <= n; i++) vis[i] = 0, dist[i] = inf;
priority_queue<G> q;
while(!q.empty()) q.pop();
q.push(G(1, 0));
dist[1] = 0;
while(!q.empty()){
now = q.top(); q.pop();
if(vis[now.id] == 1) continue;
vis[now.id] = 1;
for(int j = head[now.id]; j > 0; j = edge[j].nex){
int tmp = edge[j].to;
if(vis[tmp] == 0 && dist[tmp] > dist[now.id] + edge[j].cost && f[tmp] <= c){
dist[tmp] = dist[now.id] + edge[j].cost;
q.push(G(tmp, dist[tmp]));
}
}
}
if(dist[n] < b) return 1;
else return 0;
}
int main()
{
scanf("%d%d%lld", &n, &m, &b);
for(int i = 1; i <= n; i++){
scanf("%d", &f[i]);
r = max(r, f[i]);
}
for(int i = 1; i <= m; i++){
scanf("%d%d%lld", &ai, &bi, &ci);
addedge(ai, bi, ci);
addedge(bi, ai, ci);
}
int ans = 0;
while(l <= r){
int mid = (l + r) >> 1;
if(dijkstra(mid)) ans = mid, r = mid - 1;
else l = mid + 1;
}
if(ans == 0) printf("AFK\n");
else printf("%d\n", ans);
return 0;
}
  • 二分 + spfa版本,复杂度为 \(O(\log n \times kE)\) ,\(k\) 通常为 \(2\) 。
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<queue> using namespace std; const long long inf = 1e9+7;
const int maxn = 10005;
const int maxm = 50005;
int n, m, l, r, ai, bi, tot, head[maxn], vis[maxn], f[maxn];
long long b, ci, dist[maxn];
struct node{
int to, nex;
long long cost;
}edge[2 * maxm];
queue<int> q; inline void addedge(int u, int v, long long w){
edge[++tot].to = v;
edge[tot].cost = w;
edge[tot].nex = head[u];
head[u] = tot;
}
int spfa(int c){
for(int i = 1; i <= n; i++) dist[i] = inf, vis[i] = 0;
q.push(1); vis[1] = 1; dist[1] = 0;
while(!q.empty()){
int now = q.front();
q.pop();
vis[now] = 0;
for(int i = head[now]; i > 0; i = edge[i].nex){
int v = edge[i].to;
if(dist[v] > dist[now] + edge[i].cost && f[v] <= c){
dist[v] = dist[now] + edge[i].cost;
if(vis[v] == 0){
/******/
vis[v] = 1;
q.push(v);
}
}
}
}
if(dist[n] < b) return 1;
else return 0;
}
int main()
{
scanf("%d%d%lld", &n, &m, &b);
for(int i = 1; i <= n; i++){
scanf("%d", &f[i]);
r = max(r, f[i]);
}
for(int i = 1; i <= m; i++){
scanf("%d%d%lld", &ai, &bi, &ci);
addedge(ai, bi, ci);
addedge(bi, ai, ci);
}
int ans = 0;
while(l <= r){
int mid = (l + r) >> 1;
if(spfa(mid)) ans = mid, r = mid - 1;
else l = mid + 1;
}
if(ans == 0) printf("AFK\n");
else printf("%d\n", ans);
return 0;
}

洛谷P1462 通往奥格瑞玛的道路(二分+spfa,二分+Dijkstra)的更多相关文章

  1. 洛谷 P1462 通往奥格瑞玛的道路(spfa+二分搜索)(4boy)

    原题:http://www.luogu.org/problem/show?pid=1462#sub 4boy: 大意:给出n个城市,有m条路,每经过一个城市都要交钱,每经过一条道路都要扣HP,有HP上 ...

  2. 洛谷 P1462 通往奥格瑞玛的道路 解题报告

    P1462 通往奥格瑞玛的道路 题目背景 在艾泽拉斯大陆上有一位名叫歪嘴哦的神奇术士,他是部落的中坚力量 有一天他醒来后发现自己居然到了联盟的主城暴风城 在被众多联盟的士兵攻击后,他决定逃回自己的家乡 ...

  3. 洛谷——P1462 通往奥格瑞玛的道路

    P1462 通往奥格瑞玛的道路 题目背景 在艾泽拉斯大陆上有一位名叫歪嘴哦的神奇术士,他是部落的中坚力量 有一天他醒来后发现自己居然到了联盟的主城暴风城 在被众多联盟的士兵攻击后,他决定逃回自己的家乡 ...

  4. 洛谷 P1462 通往奥格瑞玛的道路 题解

    P1462 通往奥格瑞玛的道路 题目背景 在艾泽拉斯大陆上有一位名叫歪嘴哦的神奇术士,他是部落的中坚力量 有一天他醒来后发现自己居然到了联盟的主城暴风城 在被众多联盟的士兵攻击后,他决定逃回自己的家乡 ...

  5. 洛谷 P1462 通往奥格瑞玛的道路

    洛谷 题意:要求在限定油耗内,求最小花费的最大值. 求最小值最大很容易想到二分答案.所以我们往二分的方向去想. 我们二分一个费用,然后要保证到终点时满足限定油耗,所以跑最短路. 不过松弛条件要改一下: ...

  6. 洛谷P1462 通往奥格瑞玛的道路

    题目背景 在艾泽拉斯大陆上有一位名叫歪嘴哦的神奇术士,他是部落的中坚力量 有一天他醒来后发现自己居然到了联盟的主城暴风城 在被众多联盟的士兵攻击后,他决定逃回自己的家乡奥格瑞玛 题目描述 在艾泽拉斯, ...

  7. 洛谷P1462 通往奥格瑞玛的道路[二分答案 spfa 离散化]

    题目背景 在艾泽拉斯大陆上有一位名叫歪嘴哦的神奇术士,他是部落的中坚力量 有一天他醒来后发现自己居然到了联盟的主城暴风城 在被众多联盟的士兵攻击后,他决定逃回自己的家乡奥格瑞玛 题目描述 在艾泽拉斯, ...

  8. 洛谷P1462 通往奥格瑞玛的道路(SPFA+二分答案)

    题目背景 在艾泽拉斯大陆上有一位名叫歪嘴哦的神奇术士,他是部落的中坚力量 有一天他醒来后发现自己居然到了联盟的主城暴风城 在被众多联盟的士兵攻击后,他决定逃回自己的家乡奥格瑞玛 题目描述 在艾泽拉斯, ...

  9. 洛谷 P1462 通往奥格瑞玛的道路——二分+spfa

    上一波链接 https://www.luogu.org/problem/P1462 这道题我们考虑二分答案 然后每次跑一次spfa判断是否能够到达n点 tips:在不考虑负权边的前提下我们写最短路最好 ...

随机推荐

  1. [Python3] 010 字符串:给你们看看我的内置方法 第二弹

    目录 少废话,上例子 1. isidentifier() 2. islower() 3. isnumeric() 4. isprintable() 5. isspace() 6. istitle() ...

  2. SQL基本语法和书写格式

    插入 insert [into] 表名 [(列名列表)] values (值列表) insert into 新表名 (列名列表) select 列名列表 from 表名 select 表名.列名 in ...

  3. SpringCloud Erueka配置异常

    com.netflix.discovery.shared.transport.TransportException: Cannot execute request on any known serve ...

  4. Elasticsearch7.X 入门学习第七课笔记-----Mapping多字段与自定义Analyzer

    原文:Elasticsearch7.X 入门学习第七课笔记-----Mapping多字段与自定义Analyzer 版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处 ...

  5. SQL SERVER SP命令及实现跨数据库查询

    1.数据库: (1)sp_helpdb:报告有关指定数据库或所有数据库的信息. 例:sp_helpdb   --显示所有数据库信息(名称.大小等) 例:sp_helpdb Recruitment   ...

  6. 2019ICPC网赛南京站B题 super_log(欧拉降幂

    https://nanti.jisuanke.com/t/41299 题意:让算a^(a^(a^(...))),一共b个a, (mod p)的结果. 思路:这是个幂塔函数,用欧拉降幂公式递归求解. # ...

  7. thinkphp 系统变量

    一.查看可用变量 dump($_SERVER); 可以直接在html输出系统变量的值 <p>{$Think.server.HTTP_HOST}</p>. 二.环境变量 1.查看 ...

  8. 【leetcode】1026. Maximum Difference Between Node and Ancestor

    题目如下: Given the root of a binary tree, find the maximum value V for which there exists different nod ...

  9. CentOS下安装Chrome浏览器

    1. 下载安装脚本, 在下载目录中,执行以下命令,将安装脚本下载到本地 wget https://intoli.com/install-google-chrome.sh 2.然后授予可执行权限 chm ...

  10. Windows系统安装教程

    身边很多朋友电脑系统因为这样那样的问题不能开机,需要重装系统,但是又不知道从何下手.很不建议去电脑店重装系统,因为电脑店用的操作系统很多都有捆绑软件和主页捆绑,对电脑体验影响很坏.这篇教程只是针对新手 ...