Luo Gu P1462-half spfa road leading to Orgrimmar Shortest

Topic links:

https://www.luogu.org/problem/P1462

Reference blog:

https://www.luogu.org/blog/user37455/solution-p1462

No comments in your code, if necessary, can refer to this blog again, to understand the meaning of a specific code for each variable

Ideas:

1: This is one-half of the answer, the answer is the essence of binary enumeration, with the means to find half of the solution in the case of the known range of the solution from the scope of the solution

2: Title: "? The minimum value of all the cities he passes most of the time in the fees charged by how much." What this means in fact means: a path for a, define the function f (a). For the set of all points s right configuration on the path, satisfying f (a) = max (s), while for a map, there are multiple paths a1, a2, a3 ... from start to finish for all possible paths a1, a2, a3 ..., exist in the corresponding f (a1), f (a2), f (a3) ​​... demand f (a1), f (a2), f (a3) ​​... in minimum

3: The basic idea is that half, half of what is it? Be divided by two must be a collection solution comprises, firstly, you f (a) is equal to max (s) a, f (a) must be a point right, i.e., we seek solutions to a point right, and you right minimum point f (a) in each path traveled within a certain range: i.e. between the lowest point of the right and the highest point of the entire map, passed the point of the right sort entire map, and then do the two points find a point right at the right point of the entire set of drawing, this point is right min (f (a1) f (a2) f (a3) ​​...) is to answer this question,

4: We have found understanding of the collection: point to the collection. Equinox to the collection, each will get first-hand point right, this point right will be the ceiling all the little right on the path, looking for the path on the entire map, but because we got a ceiling, so the point right above the point of this ceiling We can not choose, to find the shortest path it should be, because you will be deducted from the blood, so it is necessary to find the path of least buckle blood,

5: If the sum of the buckle blood on the path to find the remains of death, then we have found the right point is invalid, continue up the half, to widen the scope of the right, (This question is the purpose spfa algorithm through the right point there will be a limit , meaning that, at a point less than the weight limit of the premise through to find the shortest path to reach the end point n, is the minimum amount of blood loss)

6: if not death, then the separation of the right point is valid, continue down half, narrowing the scope of the right point

#include <bits/stdc++.h>

using namespace std;
const int maxn=1e4+1,inf=0x3f3f3f3f;
vector<pair<int,int> >e[maxn];
int n,m,f,a,b,c,d[maxn],ing[maxn],point[maxn],u[maxn],l,r,mid,ans;

inline bool spfa(int top)
{
    memset(d,inf,sizeof(d));
    memset(ing,0,sizeof(ing));
    queue<int>q;
    q.push(1);
    d[1]=0;
    ing[1]=1;
    while(!q.empty())
    {
        int now=q.front();
        q.pop();
        ing[now]=0;
        for(int i=0;i<e[now].size();i++)
        {
            int v=e[now][i].first;
            if(d[v]>d[now]+e[now][i].second&&point[v]<=top)
            {
                d[v]=d[now]+e[now][i].second;
                if(ing[v])
                    continue;
                q.push(v);
                ing[v]=1;
            }
        }
    }
    if(d[n]<=f)return true;
    else return false;
}

int main()
{
    ios::sync_with_stdio(0);
    scanf("%d%d%d",&n,&m,&f);
    for(int i=1;i<=n;i++)
    {
        scanf("%d",&point[i]);
        u[i]=point[i];
    }
    for(int i=1;i<=m;i++)
    {
        scanf("%d%d%d",&a,&b,&c);
        e[a].push_back(make_pair(b,c));
        e[b].push_back(make_pair(a,c));
    }
    sort(u+1,u+n+1);
    if(spfa(inf)==false)
    {
        printf("AFK\n");
        return 0;
    }
    l=1;
    r=n;
    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;
}

 

Published 117 original articles · won praise 37 · views 6607

Guess you like

Origin blog.csdn.net/aiwo1376301646/article/details/100609968