codeforces1473 E.Minimum Path (the shortest path of the hierarchical map)

E - Minimum Path

The shortest path of
the layered graph The
first layered graph is to connect edges according to the points given in the question. From layer 0 to layer 1, we connect an edge with an edge weight of 0, from layer 1 to layer 2. Even an edge whose weight is twice the original edge weight. Of course, the first layer and the second layer are connected according to the original image. The 0th layer of the
second layered graph
is to connect edges according to the points given in the question. From layer 0 to layer 1, we connect an edge whose weight is twice the original edge weight, and connect from layer 1 to layer 2. An edge with an edge weight of 0. Of course, the first layer and the second layer are connected according to the original image.

Then the final answer is compared in the second layer of the two hierarchical graphs. If there is only one edge from 1 to a certain point, then the shortest path should be the same as the answer of the 0th layer, and then compare it by the way.

Why are there two hierarchical graphs?
In the first hierarchical graph, the cost from 0->1 layer is 0 means one less edge is passed, and the cost from 1—>2 layer is twice the original edge weight. The combination of the two means that the edge weight of one edge is used to replace the other edge.的边权. And such a map shows that the longest side appears before the shortest side.
The second layered graph represents the shortest side before the longest side. The shortest path of all points is nothing more than these two cases.

For a path, we did not consider subtracting the maximum value and adding the minimum value, but replaced the other side with one side of the road, and the shortest path must be the smallest side instead of the longest side

#define IO ios::sync_with_stdio(false);cin.tie();cout.tie(0)
#pragma GCC optimize(2)
#include<set>
#include<map>
#include<cmath>
#include<stack>
#include<queue>
#include<bitset>
#include<random>
#include<bitset>
#include<string>
#include<vector>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<unordered_map>
#include<unordered_set>
using namespace std;
typedef long long ll;
typedef pair<ll,int> pli;
const int N=600010;
const int M=4000010;
int h1[N],h2[N],e[M],ne[M],idx;
int n,m;
ll w[M],d1[N],d2[N];
bool st[N];
void add(int h[],int a,int b,ll c)
{
    
    
    e[idx]=b;
    w[idx]=c;
    ne[idx]=h[a];
    h[a]=idx++;
}
void dijkstra(int h[],ll dist[])
{
    
    
    //memset(dist,0x3f,8*N);
    memset(st,0,sizeof st);
    dist[1]=0;
    priority_queue<pli,vector<pli>,greater<pli> >q;
    q.push({
    
    0,1});
    while(q.size())
    {
    
    
        ll d=q.top().first,t=q.top().second;q.pop();
        if(st[t]) continue;
        st[t]=1;
        for(int i=h[t];i!=-1;i=ne[i])
        {
    
    
            int j=e[i];
            if(dist[j]>d+w[i]) 
            {
    
    
                dist[j]=d+w[i];
                q.push({
    
    dist[j],j});
            }
        }
    }
}

int main()
{
    
    
    IO;
    int T=1;
    //cin>>T;
    while(T--)
    {
    
    
        cin>>n>>m;
        memset(h1,-1,sizeof h1);
        memset(h2,-1,sizeof h2);
        memset(d1,0x3f,sizeof d1);
        memset(d2,0x3f,sizeof d2);
        while(m--)
        {
    
    
            int a,b,c;
            cin>>a>>b>>c;
            add(h1,a,b,c),add(h1,b,a,c);
            add(h1,a+n,b+n,c),add(h1,b+n,a+n,c);
            add(h1,a+2*n,b+2*n,c),add(h1,b+2*n,a+2*n,c);
            add(h1,a,b+n,0);add(h1,b,a+n,0);
            add(h1,a+n,b+2*n,2*c);add(h1,b+n,a+2*n,2*c);
            
            add(h2,a,b,c),add(h2,b,a,c);
            add(h2,a+n,b+n,c),add(h2,b+n,a+n,c);
            add(h2,a+2*n,b+2*n,c),add(h2,b+2*n,a+2*n,c);
            add(h2,a,b+n,2*c);add(h2,b,a+n,2*c);
            add(h2,a+n,b+2*n,0);add(h2,b+n,a+2*n,0);
        }
        dijkstra(h1,d1);
        dijkstra(h2,d2);
        for(int i=2;i<=n;i++)
            cout<<min(min(d1[i],d1[i+2*n]),d2[i+2*n])<<' ';
        cout<<'\n';
    }
    return 0;
}

It’s a pity that when I came up with a method with my teammates, there were 10 minutes left.

Guess you like

Origin blog.csdn.net/Fighting_Peter/article/details/112666260
Recommended