![](https://i-blog.csdnimg.cn/blog_migrate/ebd133efc7c81a71ee038a6d0a7fbab0.png)
最短路...多加一维表示更新了多少条路
----------------------------------------------------------------------------------
#include<cstdio>
#include<algorithm>
#include<queue>
#include<cstring>
#include<iostream>
#define rep( i , n ) for( int i = 0 ; i < n ; i++ )
#define clr( x , c ) memset( x , c , sizeof( x ) )
using namespace std;
typedef long long ll;
const int maxn = 10000 + 5;
const int maxm = 50000 + 5;
const int maxk = 20 + 5;
const ll INF = 1LL << 50;
struct edge {
int to , dist;
edge* next;
};
edge* pt , EDGE[ maxm << 1 ];
edge* head[ maxn ];
void init() {
pt = EDGE;
clr( head , 0 );
}
inline void add( int u , int v , ll d ) {
pt -> to = v;
pt -> dist = d;
pt -> next = head[ u ];
head[ u ] = pt++;
}
#define add_edge( u , v , d ) add( u , v , d ) , add( v , u , d )
ll d[ maxn ][ maxk ];
int n , K;
struct Node {
int x , k;
ll d;
bool operator < ( const Node &o ) const {
return d > o.d;
}
};
priority_queue< Node > Q;
void dijkstra() {
rep( i , n ) {
if( i )
rep( j , K ) d[ i ][ j ] = INF;
else
rep( j , K )
d[ 0 ][ i ] = 0 , Q.push( ( Node ) { 0 , j , 0 } );
}
while( ! Q.empty() ) {
Node o = Q.top();
Q.pop();
int x = o.x , k = o.k;
ll dist = o.d;
if( dist != d[ x ][ k ] ) continue;
for( edge* e = head[ x ] ; e ; e = e -> next ) {
int to = e -> to;
if( d[ to ][ k ] > dist + e -> dist ) {
d[ to ][ k ] = dist + e -> dist;
Q.push( ( Node ) { to , k , d[ to ][ k ] } );
}
if( k + 1 < K && d[ to ][ k + 1 ] > dist ) {
d[ to ][ k + 1 ] = dist;
Q.push( ( Node ) { to , k + 1 , dist } );
}
}
}
}
int main() {
//
freopen( "test.in" , "r" , stdin );
init();
int m;
cin >> n >> m >> K;
K++;
while( m-- ) {
int u , v , d;
scanf( "%d%d%d" , &u , &v , &d );
u-- , v--;
add_edge( u , v , d );
}
dijkstra();
ll ans = INF;
rep( i , K )
ans = min( ans , d[ n - 1 ][ i ] );
cout << ans << "\n";
return 0;
}
----------------------------------------------------------------------------------
1579: [Usaco2009 Feb]Revamping Trails 道路升级
Time Limit: 10 Sec
Memory Limit: 64 MB
Submit: 1378
Solved: 374
[
Submit][
Status][
Discuss]
Description
每天,农夫John需要经过一些道路去检查牛棚N里面的牛. 农场上有M(1<=M<=50,000)条双向泥土道路,编号为1..M. 道路i连接牛棚P1_i和P2_i (1 <= P1_i <= N; 1 <= P2_i<= N). John需要T_i (1 <= T_i <= 1,000,000)时间单位用道路i从P1_i走到P2_i或者从P2_i 走到P1_i 他想更新一些路经来减少每天花在路上的时间.具体地说,他想更新K (1 <= K <= 20)条路经,将它们所须时间减为0.帮助FJ选择哪些路经需要更新使得从1到N的时间尽量少.
Input
* 第一行: 三个空格分开的数: N, M, 和 K * 第2..M+1行: 第i+1行有三个空格分开的数:P1_i, P2_i, 和 T_i
Output
Sample Input
4 4 1
1 2 10
2 4 10
1 3 1
3 4 100
Sample Output
1
HINT
K是1; 更新道路3->4使得从3到4的时间由100减少到0. 最新最短路经是1->3->4,总用时为1单位. N<=10000
Source