P1462 通往奧格瑞瑪的道路
阿新 • • 發佈:2020-10-16
好久沒寫過部落格了,水一篇部落格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){ dis[to] = len + weight; q.push(make_pair(dis[to],to)); } } } return dis[n] <= k; }