lcbridge @ 2023-04-01 21:40:45
RT,subtast1都过了,求助,谢谢!
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int maxn=10000+5;
const int maxm=50000+5;
int n,m,s,dis[maxn],f[maxn],b,l=0x7ffffffffff,r;
struct edge{
int to,w;
};
vector <edge> g[maxm];
priority_queue <pair<int,int> > q;
bool vis[maxn];
bool dij(int k){
if(k<f[1])return false;
for(int i=1;i<=n;i++){
dis[i]=0x7ffffffffff;
vis[i]=0;
}
dis[1]=0;
q.push(make_pair(0,1));
while(!q.empty()){
int x=q.top().second;
q.pop();
if(vis[x])continue;
vis[x]=1;
for(int i=0;i<g[x].size();i++){
int to=g[x][i].to;
if(f[to]>k)continue;
if(dis[to]>dis[x]+g[x][i].w){
dis[to]=dis[x]+g[x][i].w;
q.push(make_pair(-dis[to],to));
}
if(to==n){
if(dis[n]>b)return false;
else return true;
}
}
}
return false;
}
signed main(){
scanf("%lld%lld%lld",&n,&m,&b);
for(int i=1;i<=n;i++){
scanf("%lld",&f[i]);
r=max(r,f[i]);
l=min(l,f[i]);
}
for(int i=1;i<=m;i++){
int u,v,w;
scanf("%lld%lld%lld",&u,&v,&w);
g[u].push_back({v,w});
g[v].push_back({u,w});
}
int mid,ans=-1;
while(l<=r){
mid=(l+r)>>1;
if(dij(mid)){
r=mid-1;
ans=mid;
}
else l=mid+1;
}
if(ans==-1)printf("AFK");
else printf("%lld",ans);
return 0;
}
by chenjliang @ 2023-07-20 18:57:45
@Super_Dabubu dij()里dis[n]可能被更新多次,不能直接判断,要在n出队时再判断,改成这样就过了。
while(!q.empty()){
int x=q.top().second;
q.pop();
if(vis[x])continue;
vis[x]=1;
if(x==n){
if(dis[n]>b)return false;
else return true;
}
for(int i=0;i<g[x].size();i++){
int to=g[x][i].to;
if(f[to]>k)continue;
if(dis[to]>dis[x]+g[x][i].w){
dis[to]=dis[x]+g[x][i].w;
q.push(make_pair(-dis[to],to));
}
}
}
by lcbridge @ 2023-07-21 10:54:35
@chenjliang thx!
by Chinshyo @ 2023-08-14 00:03:27
@chenjliang 为什么出队的时候判断就可以啊,如果要更新多次的话那不也是要出队多次,那这么说这样判断也不能保证一定是最后的情况啊