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

通往奥格瑞玛的道路

题目背景

在艾泽拉斯大陆上有一位名叫歪嘴哦的神奇术士,他是部落的中坚力量。

有一天他醒来后发现自己居然到了联盟的主城暴风城。

在被众多联盟的士兵攻击后,他决定逃回自己的家乡奥格瑞玛。

题目描述

在艾泽拉斯,有 \(n\) 个城市。编号为 \(1,2,3,\ldots,n\)

城市之间有 \(m\) 条双向的公路,连接着两个城市,从某个城市到另一个城市,会遭到联盟的攻击,进而损失一定的血量。

每次经过一个城市,都会被收取一定的过路费(包括起点和终点)。路上并没有收费站。

假设 \(1\) 为暴风城,\(n\) 为奥格瑞玛,而他的血量最多为 \(b\),出发时他的血量是满的。如果他的血量降低至负数,则他就无法到达奥格瑞玛。

歪嘴哦不希望花很多钱,他想知道,在可以到达奥格瑞玛的情况下,他所经过的所有城市中最多的一次收取的费用的最小值是多少。

输入格式

第一行 \(3\) 个正整数,\(n,m,b\)。分别表示有 \(n\) 个城市,\(m\) 条公路,歪嘴哦的血量为 \(b\)

接下来有 \(n\) 行,每行 \(1\) 个正整数,\(f_i\)。表示经过城市 \(i\),需要交费 \(f_i\) 元。

再接下来有 \(m\) 行,每行 \(3\) 个正整数,\(a_i,b_i,c_i\)\(1\leq a_i,b_i\leq n\))。表示城市 \(a_i\) 和城市 \(b_i\) 之间有一条公路,如果从城市 \(a_i\) 到城市 \(b_i\),或者从城市 \(b_i\) 到城市 \(a_i\),会损失 \(c_i\) 的血量。

输出格式

仅一个整数,表示歪嘴哦交费最多的一次的最小值。

如果他无法到达奥格瑞玛,输出 AFK

样例 #1

样例输入 #1

4 4 8
8
5
6
10
2 1 2
2 4 1
1 3 4
3 4 3

样例输出 #1

10

提示

对于 \(60\%\) 的数据,满足 \(n\leq 200\)\(m\leq 10^4\)\(b\leq 200\)

对于 \(100\%\) 的数据,满足 \(n\leq 10^4\)\(m\leq 5\times 10^4\)\(b\leq 10^9\)

对于 \(100\%\) 的数据,满足 \(c_i\leq 10^9\)\(f_i\leq 10^9\),可能有两条边连接着相同的城市。


题目要求的是途径所有点的过路费的最大值的最小值,我们可以想到二分答案来解决这个问题。

// Problem: P1462 通往奥格瑞玛的道路
// Contest: Luogu
// URL: https://www.luogu.com.cn/problem/P1462
// Memory Limit: 128 MB
// Time Limit: 1000 ms
//qwq


#include  <bits/stdc++.h>
using namespace std;
typedef long long ll;

#define rep(i, a, b) for(int i(a); i <= b; i ++)
#define dec(i, a, b) for(int i(a); i >= b; i --)

#ifdef LOCAL
#include <debugger>
#else
#define debug(...) 42
#endif


template <typename T> inline void chkmax(T &x, T y) { x = max(x, y); }
template <typename T> inline void chkmin(T &x, T y) { x = min(x, y); }

struct node {
	int ver, d;
	bool operator <(const node &T) const {
		return d > T.d;
	}
};

void solve() {
  int n, m, h; cin >> n >> m >> h;
  vector<int> f(n);
  for(int &x: f) cin >> x;
  vector<vector<array<int, 2> > > son(n);
  for(int i = 0; i < m; i ++ ) {
  	int a, b, c; cin >> a >> b >> c;
  	-- a, -- b;
  	son[a].push_back({b, c});
  	son[b].push_back({a, c});
  }
  
  int l = max(f[0], *min_element(f.begin(), f.end())), r = *max_element(f.begin(), f.end());
	
	auto chk = [&] (int x) {
		
		priority_queue<node> q;
		q.push({0, 0});
		vector<int> dist(n, 1e9);
		vector<bool> st(n);
		dist[0] = 0;
		
		while(q.size()) {
			auto t = q.top(); q.pop();
			int u = t.ver, d = t.d;
			if(st[u]) continue;
			st[u] = true;
			for(auto [v, w] : son[u]) {
				if(f[v] > x) continue;
				if(dist[v] > d + w) {
					dist[v] = d + w;
					q.push({v, d + w});
				}
			}
		}
		return dist.back() <= h;
	};
	
	while(l < r) {
		int mid = (l + r) / 2;
		if(chk(mid)) r = mid;
		else l = mid + 1;
	}
	if(!chk(l)) cout << "AFK\n";
	else cout << l << "\n";
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
	solve();

  return 0;
}
/*
 *
 *  ┏┓   ┏┓+ +
 * ┏┛┻━━━┛┻┓ + +
 * ┃       ┃
 * ┃   ━   ┃ ++ + + +
 *  ████━████+
 *  ◥██◤ ◥██◤ +
 * ┃   ┻   ┃
 * ┃       ┃ + +
 * ┗━┓   ┏━┛
 *   ┃   ┃ + + + +Code is far away from  
 *   ┃   ┃ + bug with the animal protecting
 *   ┃    ┗━━━┓ 神兽保佑,代码无bug 
 *   ┃        ┣┓
 *    ┃        ┏┛
 *     ┗┓┓┏━┳┓┏┛ + + + +
 *    ┃┫┫ ┃┫┫
 *    ┗┻┛ ┗┻┛+ + + +
 */

posted @ 2022-06-10 16:07  ccz9729  阅读(27)  评论(0编辑  收藏  举报