Figure layered learning shortest path problem On layered graph

https://www.cnblogs.com/acioi/p/11716483.html

https://www.cnblogs.com/lijilai-oi/p/11285708.html

https://wenku.baidu.com/view/b15e2fbea1c7aa00b52acb96.html examples to explain (Baidu Library)

http://www.doc88.com/p-61770953932.html Team paper

Los template Valley P2939 [USACO09FEB] Remaking Revamping Trails

#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
#define N 4100005
int n,m,head[N],tot,to[N],nxt[N],w[N],k,ans=0,dis[N];
bool vis[N];
void add(int u,int v,int w1)
{
    nxt[++tot]=head[u];head[u]=tot;to[tot]=v;w[tot]=w1;
}
priority_queue < pair<int,int> >q;
void dij()
{
    memset(dis,0x3f3f3f3f,sizeof(dis));
    dis[1]=0;q.push(make_pair(0,1));
    while(!q.empty())
    {
        int u=q.top().second;q.pop();
        if(vis[u])continue;vis[u]=1;
        for(int e=head[u];e;e=nxt[e])
        {
            int v=to[e];
            if(dis[v]>dis[u]+w[e])
            {
                dis[v]=dis[u]+w[e];
                q.push(make_pair(-dis[v],v));
            }
        }
    }
}
int main()
{
    int u,v,w1;
    cin>>n>>m>>k;
    for(int i=1;i<=m;i++)
    {
        scanf("%d%d%d",&u,&v,&w1);
        add(u,v,w1);add(v,u,w1);
        for(int j=1;j<=k;j++)
        {
           add(n*j+u,n*j+v,w1);add(n*j+v,n*j+u,w1);
           add(n*(j-1)+u,n*j+v,0);add(n*(j-1)+v,n*j+u,0);               
        }        
    }
    dij();
    ans=0x3f3f3f3f;
    for(int i=1;i<=k+1;i++)
    ans=min(ans,dis[i*n]);
    cout<<ans<<endl;
    return 0;
}
View Code

 

[Figure] stratified

[What] is a hierarchical diagram

FIG layered, as the name implies
is a lot of sub-layers FIG
i.e. similar to a two-dimensional array
is no longer a single plane
but a three-dimensional thing
not only in length and width, but also has its own thickness
is the number of layers

Hierarchical graph can be used to do [it]

Some topics will be when it comes to
let you build free k edges
this time we can use the layered graph

The same or similar establishment FIG multilayer, and even the edges between FIG Fig
may be achieved between the transfer of FIG both properties,
or have limitations FIG transfer between FIG.

But to build a multi-layer view of time and space consumption is huge,
therefore applicable data range is generally small

FIG multilayer, different states
between which there can be transferred
a state where somewhat similar dynamic programming
showing a state where the number of layers and
assuming k edges free to establish
so-k layer can have a free view showing been established to a free edge the establishment of a k edges
if you are now out of the i-th point, you have established a free edge strip j
your next point is acioi, but takes a lot to acioi
it is necessary to establish a free side
this time acioi connected to the first layer j + 1 this is the
first point j + 1 layer in this state is used acioi j + 1 edges
than before the transfer of a multi-edge
is to acioi i of this edge
which is layered FIG. the

[Tiered map]

While each of the two connecting points of the
two points corresponding to the k-th additional copy FIG connected out
to be connected to the corresponding point on the i and j in FIG. FIG
i.e. to let this edge weight value of zero where
core code

for(register int i = 1;i <= m;++ i)
{
    int x,y,z; cin >> x >> y >> z; add(x,y,z),add(y,x,z); for(register int j = 1;j <= k;++ j) { add(j * n + x,j * n + y,z);//将其他图对应的边连接起来 add(j * n + y,j * n + x,z); add((j - 1) * n + x,j * n + y,0);//两张图的连接点 add((j - 1) * n + y,j * n + x,0); } }

【example】

P2939 [USACO09FEB] Remaking Revamping Trails
hierarchical diagram template topic
explanations stamp here

P4822 [BJWC2012] freeze
layered graph templates to mention
explanations stamp here

P4568 [JLOI2011] flight path
layered graph templates to mention
explanations stamp here

To sum up:
a hierarchical view of open space need to take a look at discretion

————————————————————————————————————————————————————————————————

 

On the shortest path problem layered graph

 

Layered graph shortest path problem, is to a hierarchical diagram and then run the shortest path (nonsense).

Layered graph shortest path problem the key is how layered, layered usually play a limited role in a condition of title, here we combine example to look at.

Luogu P4568 flight path

Roughly meaning of the questions is to give a weighted undirected graph, allowing cost flights k is 0, find the minimum cost.

FIG k, this is divided into layers, each corresponding to i-1 from the first layer to the i-th layer is to take a free flight path. If then back to the first i-1 from the i-layer is a layer of "regret" process. Therefore a method is normally built FIG connected between each edge, between the upper layer and the lower layer is 0 weight, down to the right side of the normal. Such direct Dijkstra ran just fine. Here is the stratification of the k th free to restrict, and even reverse side to ensure the program has a chance to go back.

A solution to a problem in handling big brother Fig.

  P4568

Luogu P1073 optimal trade

Meaning of the questions asked us to buy from a point, a point of sale, the biggest cost of obtaining, and from 1 to reach n.

This problem has a lot of problem solution emmmm metaphysical approach only discuss the layered graph practice here. Think about the violence, are likely to buy every point, every point are likely to sell, but you must first buy and then sell (apparently a thing). So that in fact there is a dependency. This can be divided into three layers in FIG., The first layer represents not buy and sell, it has bought a second layer represents, represents a third layer has been sold. So even normal internal each side (right side as 0), between the layers, the first layer to the second layer to be connected to the right side -a [u] (u starting from the inner edge of the first layer, a [ u] as expenses) represented buy, the second to third layers is connected to the right side a [u] represents a sell side. While the second and third layers back to the first layer is less than we limit the number of trade, trade only once. Figure it this way built.

Figure solution to a problem is still carrying big brother.

  P1073

In summary, hierarchical diagram according to the shortest path is usually carried out in a hierarchical topic restricted relation, through the communication between layers or not, the right side to be limiting, in order to achieve the shortest when running can take into account the constraints. And because the shortest path algorithm is an enumeration point relaxation can not be guaranteed once the optimal solution, it is often to achieve "go back" operation through a number of methods, examples 1 through back to the original layer is achieved, and by example 2 spfa continue to achieve relaxation.

Layered graph shortest path problem, is to a hierarchical diagram and then run the shortest path (nonsense).

Layered graph shortest path problem the key is how layered, layered usually play a limited role in a condition of title, here we combine example to look at.

Luogu P4568 flight path

Roughly meaning of the questions is to give a weighted undirected graph, allowing cost flights k is 0, find the minimum cost.

FIG k, this is divided into layers, each corresponding to i-1 from the first layer to the i-th layer is to take a free flight path. If then back to the first i-1 from the i-layer is a layer of "regret" process. Therefore a method is normally built FIG connected between each edge, between the upper layer and the lower layer is 0 weight, down to the right side of the normal. Such direct Dijkstra ran just fine. Here is the stratification of the k th free to restrict, and even reverse side to ensure the program has a chance to go back.

A solution to a problem in handling big brother Fig.

  P4568

Luogu P1073 optimal trade

Meaning of the questions asked us to buy from a point, a point of sale, the biggest cost of obtaining, and from 1 to reach n.

This problem has a lot of problem solution emmmm metaphysical approach only discuss the layered graph practice here. Think about the violence, are likely to buy every point, every point are likely to sell, but you must first buy and then sell (apparently a thing). So that in fact there is a dependency. This can be divided into three layers in FIG., The first layer represents not buy and sell, it has bought a second layer represents, represents a third layer has been sold. So even normal internal each side (right side as 0), between the layers, the first layer to the second layer to be connected to the right side -a [u] (u starting from the inner edge of the first layer, a [ u] as expenses) represented buy, the second to third layers is connected to the right side a [u] represents a sell side. While the second and third layers back to the first layer is less than we limit the number of trade, trade only once. Figure it this way built.

Figure solution to a problem is still carrying big brother.

  P1073

In summary, hierarchical diagram according to the shortest path is usually carried out in a hierarchical topic restricted relation, through the communication between layers or not, the right side to be limiting, in order to achieve the shortest when running can take into account the constraints. And because the shortest path algorithm is an enumeration point relaxation can not be guaranteed once the optimal solution, it is often to achieve "go back" operation through a number of methods, examples 1 through back to the original layer is achieved, and by example 2 spfa continue to achieve relaxation.

Guess you like

Origin www.cnblogs.com/snowyhair118/p/11906141.html