Figure layered shortest explanation + examples Luo Gu P4568 [JLOI2011] flight path

Layered graph shortest. . . It sounds a bit on the tall, in fact, only the equivalent of three-dimensional shortest path only, and three-dimensional maze, we dis [i] [k] represents the starting point S to the shortest level k i, where FIG layer if it is the same undirected graph, maintained, and then a layer of the layer directed to the segment and has cost 0, the layer as the layer to the road, this fact:

The layer built by the upwardly adjacent dot point change one side, directly after completing construction can begin shortest run, of course, there are two kinds of run method, (a) point k layer may be all listed, i.e. a total of k * n points, then built a lot of edge, and finally run the shortest, when the results of the visit to answer:

 

for (int i=0; i<=k; i++)
        years = min (years say [t + i * n]);

 

(Ii) General Construction edge, which run in the shortest queue of operation, the more convenient, the code is given below:

void fig ( you st, you're one, you k, you're n)
{
    priority_queue<node>q;
    q.push(node{st,0,0});
    dis[st][0]=0;
    while (!q.empty()){
        node now=q.top();
        q.pop();
        int id=now.id%n,fl=now.floor;
        if (vis[id][fl]) continue;
        vis[id][fl]=1;
        for (int i=head[id]; i!=-1; i=eg[i].next){//同层最短路
            int v=eg[i].to;
            if (!vis[v][fl] && dis[v][fl]>dis[id][fl]+eg[i].w){
                dis [v] [in] = dis [the] [in] + eg [i] .w;
                q.push(node{v+n*fl,dis[v][fl],fl});
            }
        }
        if (fl<k){
            for (int i=head[id]; i!=-1; i=eg[i].next){//i层到i+1层最短路
                int v=eg[i].to;
                if (!vis[v][fl+1] && dis[v][fl+1]>dis[id][fl]){
                    dis [v] in [ + 1 ] = dis [the] [in];
                    q.push(node{v+n*(fl+1),dis[v][fl+1],fl+1});
                }
            }
        }
    }
}

 

Once you understand this core code so it is not hard to write code for a hierarchical diagram of the shortest given "Luo Valley P4568 [JLOI2011] flight path," the title and face AC the following code:

Title Description

Alice and Bob now travel by air, they chose a relatively cheap airline. The airline in total n with business cities, the city set labeled 0 to n-. 1 , a total of m kinds of routes, each route connecting the two cities, and routes certain price.

Alice and Bob are now arriving from one city to another city along the route, the way you can be a turning point. Airlines also launched on this trip they offer, they can free up to in the k kinds of routes to fly. Alice and Bob then this trip is the minimum amount to spend?

Input Format

The first line has three integer data, the n- , m , k, respectively, the number of cities, the number of routes and free rides.
The second line has two integers, S , t, represent the starting point of their travel city ID number and the end of the city.
Then there are m rows of three integers, a , b , C, indicates the presence of a route, from the city a city arrival b, or from the city b city reaches a , price c.

Output Format

Only one line containing an integer, for the least cost.

Sample input and output

Entry 
5 6 1
0 4
0 1 5
1 2 5
2 3 5
3 4 5
2 3 3
0 2 100
Export
8

Description / Tips

For 30% of the data, 2 n- . 5 0 , . 1 m . 3 0 0 , K = 0;
for 50% of data, 2 n- . 6 0 0 , . 1 m . 6 0 0 0 , 0 K . 1;
to 100% of the data, 2 n- . 1 0 0 0 0 , . 1 m . 5 0000,0k10,0s,t<n,0a,b<n,ab,0c1000

 

 The following is the AC codes:

#include <bits/stdc++.h>
using namespace std;

const  int mac = 5e4 + 10 ;
const  int inf = 5E8 + 10 ;

struct Edge
{
    int to,w,next;   
}eg[mac<<1];
struct node
{
    int id,d,floor;
    bool operator <(const node &a)const{
        return d>a.d;
    }
};
int head [Mac], n = 0 , via [Mac] [ 15 ], push [Mac] [ 15 ];

void add(int u,int v,int w)
{
    eg[++num]=Edge{v,w,head[u]};
    head[u]=num;
}

void fig ( you st, you're one, you k, you're n)
{
    priority_queue<node>q;
    q.push(node{st,0,0});
    dis[st][0]=0;
    while (!q.empty()){
        node now=q.top();
        q.pop();
        int id=now.id%n,fl=now.floor;
        if (vis[id][fl]) continue;
        vis[id][fl]=1;
        for (int i=head[id]; i!=-1; i=eg[i].next){
            int v=eg[i].to;
            if (!vis[v][fl] && dis[v][fl]>dis[id][fl]+eg[i].w){
                dis [v] [in] = dis [the] [in] + eg [i] .w;
                q.push(node{v+n*fl,dis[v][fl],fl});
            }
        }
        if (fl<k){
            for (int i=head[id]; i!=-1; i=eg[i].next){
                int v=eg[i].to;
                if (!vis[v][fl+1] && dis[v][fl+1]>dis[id][fl]){
                    dis [v] in [ + 1 ] = dis [the] [in];
                    q.push(node{v+n*(fl+1),dis[v][fl+1],fl+1});
                }
            }
        }
    }
}

int main ()
{
    int n,m,k,st,ed;
    scanf ("%d%d%d",&n,&m,&k);
    scanf ("%d%d",&st,&ed);
    memset(head,-1,sizeof head);
    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);
    }
    for (int i=0; i<n; i++)
        for (int j=0; j<=k; j++)
            dis[i][j]=inf;
    dij (st ed, k, n);
    int ans=inf;
    for (int i=0; i<=k; i++)
        years = min (years say [ed] [i]);
    printf ("%d\n",ans);
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/lonely-wind-/p/11777377.html