码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2018-06-26 22:40:06      阅读:194      评论:0      收藏:0      [点我收藏+]

标签:ems   next   add   struct   include   str   ios   lse   clu   

题目链接: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;
}

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

标签:ems   next   add   struct   include   str   ios   lse   clu   

原文地址:https://www.cnblogs.com/MisakaAzusa/p/9231388.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!