Konjac tree-lined review --K shorts A * Solution

Look at the title knows what is talking about, but why is A * solution especially on short circuit K of it?

Because the tree-lined not others.

Can see the estimate of this blog also we know that A * K short circuit and what are the right, just tell us about the following evaluation function

Since the tree-lined and without academic training, so what about A * are emotional understanding, little information is only available as a quick OIer knowledge,

As the algorithm to refrain from academic basis.

First think about for any short-circuit a K, can be written in is not part of the shortest path and the other part of the composition, and the two parts of the path there is one and only one common point.

Description: 1. Any path can be regarded as the shortest path to the end by the other end to end and composition

   2. A ring even in front of the path, must also be connected to the shortest path and the other point on the ring. Because the other points on the intersection once the shortest path and, on behalf of the ring has the shortest path, then this path is not the shortest.

Well, introducing the concept of the following: evaluation function!

In fact nothing special, to a formula F (x) = G (x) + H (x), F (x) is the desired route cost, G (x) is the cost from the start point to the current point, H (x) is the optimal cost of the current point to the end point.

So, when we have to solve is not a general idea of?

The closer the smaller the value of K X-point route from the starting point (probably mean, you can think more closer shortest shortest route used)

Then we can consider it first H treatment (anti-built map, find the single source shortest path), and then maintain a priority queue, the queue which holds a Pair (F (x) and x point), then every time we According to F ( under x) the value of the execution of a decision point, if x is already the end, the answer in relation to statistical statistical answer, the return, or after x calculated for the point to be connected to each of the obtained F (to) pressure into the queue until the completion of the K current path or find the shortest path needs have been satisfied.

The following put a bar code, examples COGS3227

Strictly speaking this is not a short circuit K, but given special restrictions, seeking to meet the conditions of the route

#include<iostream>
#include<cstdio>
#include<queue>
#include<cstring>
using namespace std;
vector<pair<int,int> > b[1001],fb[1001];
int vis[1001],dis[1001];
int n,m,a1,a2,a3,S,F,ans,MX,T;
void DIJ()
{
    memset(dis,0x3f3f3f3f,sizeof(dis));
    memset(vis,0,sizeof(vis));
    priority_queue<pair<int,int> > q; 
    dis[F]=0;
    q.push(make_pair(0,F));
    while(q.size())
    {
        int u=q.top().second;
        q.pop();
        if(vis[u])
            continue;
        vis[u]=1;
        for(int i=0;i<fb[u].size();i++)
        {
            int to=fb[u][i].first;
            if(dis[to]>dis[u]+fb[u][i].second)
            {
                dis[to]=dis[u]+fb[u][i].second;
                q.push(make_pair(-dis[to],to));
            }
        }
    }
}
void ASTAR()
{
    priority_queue<pair<int,int> > q;
    q.push(make_pair(-dis[S],S));
    while(q.size())
    {
        int u=q.top().second;
        int v=q.top().first;
        q.pop();
        int dist = -v-DIS [u]; // actually u distance from the origin to the 
        IF (dist> the MX)
             Continue ;
         IF (u == F.)
        {
            if(dist<=MX)
            {
                years ++ ;
            }
            else 
                return ;
        } 
        for(int i=0;i<b[u].size();i++)
        {
            int to=b[u][i].first;
            q.push(make_pair(-(dist+b[u][i].second+dis[to]),to));
        }
    }
}
int Linyin ()
{
    years = 0 ;
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++)
    {
        b[i].clear();
        fb[i].clear();
    }
    for(int i=1;i<=m;i++)
    {
        scanf("%d%d%d",&a1,&a2,&a3);
        fb[a2].push_back(make_pair(a1,a3));
        b[a1].push_back(make_pair(a2,a3));
    }
    scanf("%d%d",&S,&F);
    (DIJ);
    MX=dis[S]+1;
    LINING();
    printf("%d\n",ans);
    return 0;
}
int LWH()
{
    freopen("sightseeing.in","r",stdin);
    freopen("sightseeing.out","w",stdout);
    scanf("%d",&T);
    while(T--)
    {
        LINYIN ();
    }
    return 0;
}
int MYLOVE=LWH();
int main()
{
    ;
}

End Sahua! ! !

 

Guess you like

Origin www.cnblogs.com/XLINYIN/p/11828725.html