Codeforces 938D: Buy a Ticket Virtual Source + Shortest Path

Portal

Title description

There are n cinemas now.
There are m roads between these n cinemas, forming an undirected graph.
The price of watching movies in each cinema is different.
The toll for each road is also different.
Now I want you to find the minimum value of each movie theater.

analysis

The data range of this topic implies that you can only run the shortest path once, or the shortest path of heap optimization.

The path of each answer is from one point to another, plus all the edge weights, plus the point weight of a point, so we can find a way to convert this point weight into a side weight
so we go to create a virtual source Point, connect an edge to each point, the edge weight is the point weight, and then you can build the graph according to the given edge.
Because it needs to go back and forth, it needs twice

Code

#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <queue>
#include <cstring>
#define debug(x) cout<<#x<<":"<<x<<endl;
#define _CRT_SECURE_NO_WARNINGS
#pragma GCC optimize("Ofast","unroll-loops","omit-frame-pointer","inline")
#pragma GCC option("arch=native","tune=native","no-zero-upper")
#pragma GCC target("avx2")
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll,int> PII;
const int INF = 0x3f3f3f3f;
const int N = 4e5 + 10,M = N * 2;
int h[N],ne[M],e[M],idx;
ll w[M];
ll d[N];
bool st[N];
int n,m;

void add(int x,int y,ll z){
    
    
    ne[idx] = h[x],e[idx] = y,w[idx] = z,h[x] = idx++;
}

void dij(){
    
    
    d[0] = 0;
    priority_queue<PII,vector<PII>,greater<PII> > Q;
    Q.push(PII(0,0));
    while(Q.size()){
    
    
        PII p = Q.top();
        Q.pop();
        int t = p.second;
        if(st[t]) continue;
        st[t] = true;
        for(int i = h[t];i != -1;i = ne[i]){
    
    
            int j = e[i];
            if(d[j] > d[t] + w[i]){
    
    
                d[j] = d[t] + w[i];
                Q.push(PII(d[j],j));
            }
        }
    }
}

int main(){
    
    
    scanf("%d%d",&n,&m);
    for(int i = 0;i <= n;i++) h[i] = -1,d[i] = 0x3f3f3f3f3f3f;
    while(m--){
    
    
        int x,y;
        ll z;
        scanf("%d%d%lld",&x,&y,&z);
        add(x,y,2 * z),add(y,x,2 * z);
    }
    for(int i = 1;i <= n;i++){
    
    
        ll z;
        scanf("%lld",&z);
        add(0,i,z);
    }
    dij();
    for(int i = 1;i <= n;i++) printf("%lld ",d[i]);
    return 0;
}

/**
*  ┏┓   ┏┓+ +
* ┏┛┻━━━┛┻┓ + +
* ┃       ┃
* ┃   ━   ┃ ++ + + +
*  ████━████+
*  ◥██◤ ◥██◤ +
* ┃   ┻   ┃
* ┃       ┃ + +
* ┗━┓   ┏━┛
*   ┃   ┃ + + + +Code is far away from  
*   ┃   ┃ + bug with the animal protecting
*   ┃    ┗━━━┓ 神兽保佑,代码无bug 
*   ┃        ┣┓
*    ┃        ┏┛
*     ┗┓┓┏━┳┓┏┛ + + + +
*    ┃┫┫ ┃┫┫
*    ┗┻┛ ┗┻┛+ + + +
*/

Guess you like

Origin blog.csdn.net/tlyzxc/article/details/112534523