Flip Edge

Atcoder Beginner Contest 395 E

Problem Statement

You are given a directed graph with $N$ vertices and $M$ edges. The $i$-th edge $(1 \leq i \leq M)$ is a directed edge from vertex $u _ i$ to vertex $v _ i$.

Initially, you are at vertex $1$. You want to repeat the following operations until you reach vertex $N$:

  • Perform one of the two operations below:
    • Move along a directed edge from your current vertex. This incurs a cost of $1$. More precisely, if you are at vertex $v$, choose a vertex $u$ such that there is a directed edge from $v$ to $u$, and move to vertex $u$.
    • Reverse the direction of all edges. This incurs a cost of $X$. More precisely, if and only if there was a directed edge from $v$ to $u$ immediately before this operation, there is a directed edge from $u$ to $v$ immediately after this operation.

It is guaranteed that, for the given graph, you can reach vertex $N$ from vertex $1$ by repeating these operations.

Find the minimum total cost required to reach vertex $N$.

Constraints

  • $2 \leq N \leq 2 \times 10^5$
  • $1 \leq M \leq 2 \times 10^5$
  • $1 \leq X \leq 10^9$
  • $1 \leq u _ i \leq N \ (1 \leq i \leq M)$
  • $1 \leq v _ i \leq N \ (1 \leq i \leq M)$
  • For the given graph, it is guaranteed that you can reach vertex $N$ from vertex $1$ by the operations described.
  • All input values are integers.

Input

The input is given from Standard Input in the following format:

$N$ $M$ $X$

$u _ 1$ $v _ 1$

$u _ 2$ $v _ 2$

$\vdots$

$u _ M$ $v _ M$

Output

Print the minimum total cost required to reach vertex $N$.

Solving

求解分层图最短路,我们需要将图分解成两层,一层的边正序,一层的边倒序(反转),随后对于每层的节点,都可以与另一层的相同节点相通,其通向另一层本身节点的权值为X,随后我们在节点1处跑一边Dijkstra,比较最后通向的终点n在哪一层的路程最短,输出即可。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#include <bits/stdc++.h>
#define int long long
#define endl '\n'
#define INF 0x3f3f3f3f
#define NINF -0x3f3f3f3f
#define LINF 0x3f3f3f3f3f3f3f3fLL
#define Single
using namespace std;
using ll = long long;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
const int N = 2e5+10;
const int M = 2e5+10; 
int cnt = 0;
//两层图,双倍的节点数量
int head[N<<1];
bool vis[N<<1];
int dis[N<<1];
int n,m,x;
struct node
{
    int to,w,next;
}edge[M<<2];//注意下方代码的加边次数,最多可加4*M个边(两层图的边,和连接两层之间的双向边),故最多可有4*M条边.
void add(int u,int v,int w)
{
    cnt++;
    edge[cnt].w = w;
    edge[cnt].to = v;
    edge[cnt].next = head[u];
    head[u] = cnt;
}
void solve()
{
    cin >> n >> m >> x;
    for(int i=1;i<=m;i++)
    {
        int u,v;
        cin >> u >> v;
        add(u,v,1);
        add(v+n,u+n,1);//第二层建反向边
    }
    for(int i=1;i<=n;i++)
    {	
        //在两层图的同一节点之间建边,转换花费:X
        add(i,i+n,x);
        add(n+i,i,x);
    }
    for(int i=1;i<=n*2;i++)
    {
        dis[i] = LINF;//LLONG_MAX
    }
    priority_queue<pii,vector<pii>,greater<pii>> q;
    auto dijkstra = [&](int start)
    {
        dis[start] = 0;
        q.push({0,start});
        while(!q.empty())
        {
            int u = q.top().second;
            q.pop();
            if(vis[u]) continue;
            vis[u] = true;
            for(int i = head[u];i;i=edge[i].next)
            {
                int v = edge[i].to;
                int w = edge[i].w;
                if(dis[v] > dis[u] + w)
                {
                    dis[v] = dis[u] + w;
                    q.push({dis[v],v});
                } 
            } 
        }
    };
    dijkstra(1);
    int ans = min(dis[n],dis[n*2]);
    cout << ans;
}

signed main()
{
    ios::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr);
    int T;
#ifdef Single
    T = 1;
#else
    cin >> T;
#endif
    while(T--)
    {
        solve();
    }
    return 0;
}
Licensed under CC BY-NC-SA 4.0
Member of the Qilu University Of Technology ACM-ICPC Association
Built with Hugo
Theme Stack designed by Jimmy