USACO09FEB改造路Revamping Trails 发表于 2017-10-03 题意有$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了一次,说明好久没写生疏了。 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364#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 ;}