USACO09FEB改造路Revamping Trails

  • 题意
    有$N (1 \le N \le 10000)$个点,$M(1 \le M \le 50000)$条边的无向图。可以使$K$条边的边权变成0,求点$1$到点$N$的最短距离。
  • Solution

    • 设$dis[i][j]$代表到点$i$改造了$j$条路的最短路。
    • 数据卡spfa
  • tips

    • 改成Dijkstra竟然还WA了一次,说明好久没写生疏了。
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
#include <bits/stdc++.h>
using namespace std ;
void Read ( int &x, char c = getchar() ) {
for ( x = 0 ; !isdigit(c) ; c = getchar() ) ;
for ( ; isdigit(c) ; c = getchar() ) x = 10*x + c - '0' ;
}
const int maxn = 1e4+5, maxm = 1e6+5, zhf = 0x3f3f3f3f ;
int n, m, cnt, e, Begin[maxn], Next[maxm], To[maxm], W[maxm] ;
void add ( int x, int y, int z ) {
To[++e] = y ;
Next[e] = Begin[x] ;
Begin[x] = e ;
W[e] = z ;
}
int dis[maxn][25] ;
bool inq[maxn][25] ;
struct node {
int x, stp, dis ;
friend bool operator < ( node a, node b ) {
return a.dis > b.dis ;
}
} ;
priority_queue <node> Q ;
void Dijkstra( int x ) {
int i, j, u ;
for ( i = 1 ; i <= n ; i ++ )
for ( j = 0 ; j <= cnt ; j ++ )
dis[i][j] = zhf ;
dis[x][0] = 0 ;
Q.push( (node){x, 0, 0} ) ;
node t ;
while ( !Q.empty() ) {
t = Q.top() ;
Q.pop() ;
x = t.x ;
if ( inq[x][t.stp] ) continue ;
inq[x][t.stp] = 1 ;
for ( i = Begin[x] ; i ; i = Next[i] ) {
u = To[i] ;
for ( j = 0 ; j <= cnt ; j ++ ) {
if ( dis[u][j] > dis[x][j] + W[i] ) {
dis[u][j] = dis[x][j] + W[i] ;
Q.push( (node){u, j, dis[u][j]} ) ;
}
if ( j < cnt && dis[u][j+1] > dis[x][j] ) {
dis[u][j+1] = dis[x][j] ;
Q.push( (node){u, j+1, dis[u][j+1]} ) ;
}
}
}
}
printf ( "%d\n", dis[n][cnt] ) ;
}
int main() {
int i, x, y, z ;
Read(n) ; Read(m) ; Read(cnt) ;
for ( i = 1 ; i <= m ; i ++ ) {
Read(x) ; Read(y) ; Read(z) ;
add ( x, y, z ) ;
add ( y, x, z ) ;
}
Dijkstra(1) ;
return 0 ;
}