P1462 通往奥格瑞玛的道路

题面

好久没写过博客了,水一篇博客qwq。

直接二分答案就好了。

代码:

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

char buf[1<<21], *p1, *p2;
#define getchar() (p1 == p2 and (p2 = (p1 = buf) +\
fread(buf, 1, 1<<21, stdin), p1 == p2)? EOF : *p1++)
template<typename temp>
temp read(temp& x){
	x = 0; temp f = 1; char ch;
	while(!isdigit(ch = getchar())) (ch == '-') and (f = -1);
	for(x = ch^48; isdigit(ch = getchar()); x = (x<<3)+(x<<1)+(ch^48));
	return x *= f;
}
template<typename temp, typename ...Args>
void read(temp& a, Args& ...args){read(a), read(args...);}

const int maxn = 1e4+10, inf = 1<<30;

int n, m, maxnum, k, val[maxn], dis[maxn], vis[maxn], cannot[maxn];

vector<pair<int,int> > v[maxn];

inline bool check(int x);

signed main(){
	read(n, m, k);
	for(int i = 1; i <= n; i ++) maxnum = max(read(val[i]), maxnum);
	for(int i = 1, x, y, z; i <= m; i ++){
		read(x, y, z);
		v[x].push_back(make_pair(y,z));
		v[y].push_back(make_pair(x,z));
	}
	int l = 0, r = maxnum;
	while(l < r){
		int mid = (l+r)>>1;
		if(check(mid)) r = mid;
		else l = mid+1;
	}
	check(l) ? printf("%d", l) : printf("AFK");
	return 0;
}

inline bool check(int x){
	for(int i = 1; i <= n; i ++){
		vis[i] = cannot[i] = 0, dis[i] = inf;
		cannot[i] = val[i] > x;
	}
	priority_queue<pair<int,int>, vector<pair<int,int> >, greater<pair<int,int> > > q;
	q.push(make_pair(dis[1]=0,1));
	while(q.size()){
		int now = q.top().second, len = q.top().first;
		q.pop();
		if(vis[now]) continue;
		vis[now] = 1;
		for(unsigned int i = 0; i < v[now].size(); i ++){
			int to = v[now][i].first, weight = v[now][i].second;
			if(cannot[to]) continue;
			if(dis[to] > len + weight) q.push(make_pair(dis[to]=len+weight,to));
		}
	}
	return dis[n] <= k;
} 
posted @ 2020-10-16 11:03  Vanyun  阅读(129)  评论(0编辑  收藏  举报