通往奥格瑞玛的道路(二分 + Dijkstra)

通往奥格瑞玛的道路(二分 + 最短路)

在这里插入图片描述
思路:

1.二分可解决最大最小或者最小最大一类的问题
2.题目意思可以说我根本看不懂,其实题意:从1到n所有可行路径中,每个路径都有某个城市收费最大,求所有城市收费最大在所有路径的最小值
3.用二分查找该最小值,每次设置一个可行最大费用若dijkstra过程中某城市的收费大于设置的值则不走这该城市。
4.堆优化dijkstra堆维护消耗的血量,最后返回血量是否为正(是否走的到终点)

样例输入:

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

样例输出:

10

代码:

#include <iostream>
#include <cstring>
#include <algorithm>
#include <queue>
#include <vector>
using namespace  std;

const int N = 1e4 + 10, M = 1e5 + 10, INF = 1e9 + 10;
typedef long long LL;
typedef pair<LL,LL> PII;
LL h[N], e[M], w[M], ne[M], idx;
LL n, m, b;
LL city[N], dist[N];
bool st[N];

void add(LL a, LL b, LL c)
{
	e[idx] = b, w[idx] = c, ne[idx] = h[a], h[a] = idx ++;
}

bool dijk(LL x)
{
	//若出发城市收费已经大于设置的值直接返回 false 
	if(x < city[1]) return false;
	//堆维护消耗的血量 
	priority_queue<PII, vector<PII>, greater<PII>> q;
	memset(dist, 0x7f, sizeof dist);
	memset(st, false, sizeof st);
	dist[1] = 0;
	q.push({0, 1});
	
	while(q.size())
	{
		auto t = q.top();
		q.pop();
		LL dis = t.first, ver = t.second;
		
		if(st[ver]) continue;
		st[ver] = true;
		for(int i = h[ver]; ~i; i = ne[i])
		{
			LL j = e[i];
			//若经过的城市收费大于设置的最大值则不走该城市 
			if(x < city[j]) continue;
			if(dist[j] > dist[ver] + w[i])
			{
				dist[j] = dist[ver] + w[i];
				q.push({dist[j], j});
			}
		}
	}
	//返回剩下的血量是否为负数 
	return dist[n] <= b;
}

int main()
{
	scanf("%lld%lld%lld", &n, &m, &b);
	
	memset(h, -1, sizeof h);
	LL r = 0;
	for(int i = 1; i <= n; i ++ )
		scanf("%lld", &city[i]), r = max(r, city[i]);
	for(int i = 1; i <= m; i ++ )
	{
		LL a, b, c;
		scanf("%lld%lld%lld", &a, &b, &c);
		add(a, b, c), add(b, a, c);
	}
	
	LL l = 0, ans = INF;
	r ++;
	while(l < r)
	{
		int mid = l + r >> 1;
		if(dijk(mid))
		{
			r = mid;
			ans = min(ans, r);
		}
		else l = mid + 1;
			
	}
	
	if(ans == INF) cout << "AFK" << endl;
	else cout << ans << endl;
	
	return 0;
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值