Moving Both Hands---反向dij

任意门

Pak Chanek is playing one of his favourite board games. In the game, there is a directed graph with N vertices and M edges. In the graph, edge i connects two different vertices Ui and Vi with a length of Wi. By using the i-th edge, something can move from Ui to Vi, but not from Vi to Ui.

To play this game, initially Pak Chanek must place both of his hands onto two different vertices. In one move, he can move one of his hands to another vertex using an edge. To move a hand from vertex Ui to vertex Vi, Pak Chanek needs a time of Wi seconds. Note that Pak Chanek can only move one hand at a time. This game ends when both of Pak Chanek’s hands are on the same vertex.

Pak Chanek has several questions. For each p satisfying 2≤p≤N, you need to find the minimum time in seconds needed for Pak Chanek to end the game if initially Pak Chanek’s left hand and right hand are placed on vertex 1 and vertex p, or report if it is impossible.

Input
The first line contains two integers N and M (2≤N≤105, 0≤M≤2⋅105) — the number of vertices and edges in the graph.

The i-th of the next M lines contains three integers Ui, Vi, and Wi (1≤Ui,Vi≤N, Ui≠Vi, 1≤Wi≤109) — a directed edge that connects two different vertices Ui and Vi with a length of Wi. There is no pair of different edges i and j such that Ui=Uj and Vi=Vj.

Output
Output a line containing N−1 integers. The j-th integer represents the minimum time in seconds needed by Pak Chanek to end the game if initially Pak Chanek’s left hand and right hand are placed on vertex 1 and vertex j+1, or −1 if it is impossible.

Example
input

5 7
1 2 2
2 4 1
4 1 4
2 5 3
5 4 1
5 2 4
2 1 1

output

1 -1 3 4

Note
If initially Pak Chanek’s left hand is on vertex 1 and his right hand is on vertex 5, Pak Chanek can do the following moves:

Move his right hand to vertex 4 in 1 second.
Move his left hand to vertex 2 in 2 seconds.
Move his left hand to vertex 4 in 1 second.

In total it needs 1+2+1=4 seconds. It can be proven that there is no other way that is faster.

题目的意思就是将两个手分别放在有向图的两个点上,然后怎么样走的最短,然后相遇
原题等价于,从点1出发,到达点i,中间可以选择一个点x,并从点x开始,走反向边。
求min(dist[i][x] + rev_dist[x][i])
最短路径变形,dijkstra的基础上,计算从1到点i的最短距离时,添加一个方向,用于扭转反向。

最终就是要从1走到建立的反向图的i点去。
因为要1与i要相遇,相当于从1走到中间点p,然后p走到反向图的i点。

#include<iostream>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std;
 
#define int long long
 
const int INF = 9223372036854775807;
const int maxn = 1000100;
int n, m;
int tot;
int head[maxn];
int dis[maxn];
bool vis[maxn];
 
struct edge{
    int to;
    int from;
    int nxt;
    int val;
}e[4*maxn];
 
void add(int x, int y, int val){
    tot++;
    e[tot].to = y;
    e[tot].val = val;
    e[tot].from = x;
    e[tot].nxt = head[x];
    head[x] = tot;
}
 
void dij(int x){
    for(int i = 1; i <= 2 * n; i++) dis[i] = INF;
    priority_queue<pair<int, int> > q;
    dis[x] = 0;
    q.push(make_pair(-dis[x], x));
    while(!q.empty()){
        int u = q.top().second;
        q.pop();
        if(vis[u]) continue;
        vis[u] = 1;
        for(int i = head[u]; i; i = e[i].nxt){
            int v = e[i].to;
            if(dis[v] > dis[u] + e[i].val){
                dis[v] = dis[u] + e[i].val;
                q.push(make_pair(-dis[v], v));
            }
        }
    }
}
 
signed main(){
    cin >> n >> m;
    int x, y, z;
    for(int i = 1; i <= m; i++){
        cin >> x >> y >> z;
        add(x, y, z);
        add(y + n, x + n, z);//反向建图
    }
    for(int i = 1; i <= n; i++) add(i, i + n, 0);
    dij(1);
    for(int i = 2; i <= n; i++){
        if(dis[i + n] == INF) cout << "-1 ";
        else cout << dis[i + n] << " ";
    } 
}
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值