标签:struct memset string while main [1] lse 答案 fine
二分答案,大于二分值的边直接不走,看最后是否存在一条路径即可
#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdio>
#include <queue>
using namespace std;
#define debug(x) cerr << #x << "=" << x << endl;
const int MAXN = 10000 + 10;
const int MAXM = 500000 + 10;
int n,m,last[MAXN],tot,dist[MAXN],s,vis[MAXN],g[MAXN],f[MAXN],b;
queue <int> q;
struct Edge{
int u,v,w,to;
Edge(){}
Edge(int u, int v, int w, int to) : u(u), v(v), w(w), to(to) {}
}e[MAXM];
inline void add(int u, int v, int w) {
e[++tot] = Edge(u,v,w,last[u]);
last[u] = tot;
}
bool SPFA(int s) {
memset(dist,0x3f,sizeof(dist));
memset(vis,0,sizeof(vis));
dist[1] = 0;
q.push(1);
vis[1] = 1;
while(!q.empty()) {
int x = q.front();
q.pop();
vis[x] = 0;
for(int i=last[x]; i; i=e[i].to) {
int v = e[i].v;
int w = e[i].w;
if(dist[v] > dist[x] + w) {
if(f[v] <= s) {
dist[v] = dist[x] + w;
if(!vis[v]) q.push(v), vis[v] = 1;
}
}
}
}
if(dist[n] >= b) return false;
else return true;
}
int main() {
cin >> n >> m >> b;
for(int i=1; i<=n; i++) {
cin >> f[i];
g[i] = f[i];
}
for(int i=1; i<=m; i++) {
int a,b,c;
cin >> a >> b >> c;
add(a,b,c);
add(b,a,c);
}
int l=1, r = n;
sort(g+1,g+n+1);
int ans=0;
int mid;
if(!SPFA(1<<30)) {
cout << "AFK" << endl;
return 0;
}
while(l <= r) {
mid = l+r>>1;
if(SPFA(g[mid])) {
ans = g[mid];
r = mid-1;
} else {
l = mid+1;
}
}
cout << ans;
return 0;
}
标签:struct memset string while main [1] lse 答案 fine
原文地址:https://www.cnblogs.com/Zolrk/p/9918259.html