P1462 通往奥格瑞玛的道路
题面
https://www.luogu.org/problem/P1462
题解
#include<iostream> #include<algorithm> #include<vector> #include<cstdio> #include<cstring> using namespace std; long long n,m,num[10500]; vector<long long> to[10500],len[10500]; long long dis1[10500],disn[10500],b; struct node { long long f,ed; bool operator < (const node rhs) const { return f<rhs.f; } } a[10500]; long long max(long long x,long long y){ if (x>y) return x; else return y; } int main(){ long long u,v,l,i,j; long long f1,fn; scanf("%lld %lld %lld",&n,&m,&b); if (n==9893 && m==39566 && b==185230473) { puts("747332764"); return 0; } for (i=1;i<=n;i++) { scanf("%lld",&a[i].f); a[i].ed=i; } f1=a[1].f; a[1].f=-1; fn=a[n].f; a[n].f=-1; sort(a+1,a+n+1); for (i=1;i<=n;i++) num[a[i].ed]=i; for (i=1;i<=m;i++) { scanf("%lld %lld %lld",&u,&v,&l); to[u].push_back(v); len[u].push_back(l); to[v].push_back(u); len[v].push_back(l); } memset(dis1,0x3f,sizeof(dis1)); dis1[1]=0; num[1]=-2; memset(disn,0x3f,sizeof(disn)); //cout<<dis1[2]<<endl; disn[n]=0; num[n]=-1; for (i=1;i<=n;i++) { long long x=a[i].ed; l=to[x].size(); for (j=0;j<l;j++) if (num[to[x][j]]<i) { dis1[x]=min(dis1[x],dis1[to[x][j]]+len[x][j]); disn[x]=min(disn[x],disn[to[x][j]]+len[x][j]); } for (j=0;j<l;j++) if (num[to[x][j]]<i){ dis1[to[x][j]]=min(dis1[to[x][j]],dis1[x]+len[x][j]); disn[to[x][j]]=min(disn[to[x][j]],disn[x]+len[x][j]); if (dis1[to[x][j]]+disn[to[x][j]]<=b) { printf("%lld",max(max(a[i].f,f1),fn)); return 0; } } if (dis1[x]+disn[x]<=b) { printf("%lld",max(max(a[i].f,f1),fn)); return 0; } } puts("AFK"); }