【luogu P1462 通往奥格瑞玛的道路】 题解

题目链接:https://www.luogu.org/problemnew/show/P1462
记住HP=0也叫死。

#include <queue>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int maxn = 5000001;
const int inf = 0x7f;
long long n, m, s, dis[maxn], hp, f[maxn], u[maxn];
bool vis[maxn<<2];
struct edge{
    int to, from, len, next;
}e[maxn];
int head[maxn], cnt;
void add(int u, int v, int w)
{
    e[++cnt].from = u;
    e[cnt].len = w;
    e[cnt].to = v;
    e[cnt].next = head[u];
    head[u] = cnt;
}
int SPFA(int top)
{
    memset(dis,inf,sizeof(dis));
    memset(vis,0,sizeof(vis));
    queue<int> q;
    vis[1] = 1;
    dis[1] = 0;
    q.push(1);
    while(!q.empty())
    {
        int now = q.front(); q.pop();
        vis[now] = 0;
        for(int i = head[now]; i != -1; i = e[i].next)
        {
            if(dis[e[i].to] > dis[now]+e[i].len && f[e[i].to]<=top)
            {
                dis[e[i].to] = dis[now]+e[i].len;
                if(vis[e[i].to] == 0)
                {
                    vis[e[i].to] = 1;
                    q.push(e[i].to);
                }
            }
        }
    }
    if(dis[n] < hp)
    return true;
    else
    return false;
}
int main()
{
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);
    memset(head,-1,sizeof(head));
    scanf("%d%d%d",&n,&m,&hp);
    for(int i = 1; i <= n; i++)
    {
        scanf("%d",&f[i]);
        u[i] = f[i];
    }
    for(int i = 1; i <= m; i++)
    {
        int u, v, w;
        scanf("%d%d%d",&u,&v,&w);
        add(u,v,w);
        add(v,u,w);
    }
    if(SPFA(0x7fffffff) == false)
    {
        printf("AFK\n");
        return 0;
    }
    sort(u+1,u+1+n);
    int l = 1, r = n, mid, ans = 0; 
    while(l <= r)
    {
        mid = (l+r)/2;
        if(SPFA(u[mid]) == true)
        {
            ans = u[mid];
            r = mid-1;
        }
        else
        l = mid+1;
    }
    printf("%d\n",ans);
    return 0;
}
posted @ 2018-06-26 21:44  Misaka_Azusa  阅读(154)  评论(0编辑  收藏  举报
Live2D