思路:spfa 转移dp
#include <cstdio>
#include <cstring>
#include <utility>
#include <queue>
using namespace std;
const int maxn = 1005;
const int maxm = 20005;
const int maxk = 1005;
const int inf = 0x3f3f3f3f;
typedef pair<int,int> pii;
int he[maxn],ne[maxm],ver[maxm],tot,cost[maxm];
void add( int x,int y,int z ){
ver[++tot] = y;
ne[tot] = he[x];
he[x]= tot;
cost[tot] = z;
}
int dp[maxn][maxk],vis[maxn][maxk];
void init( int n,int k ){
memset( he,0,sizeof(int)*(n+1) );
for( int i = 0;i <= n;i++ ){
for( int j = 0;j <= k;j++ ){
dp[i][j] = inf;
}
}
}
queue<pii> que;
int ans,n,p,k;
void spfa(int s){
que.push( pii( s,0 ) );
dp[s][0] = 0;
vis[s][0] = 1;
while( que.size() ){
int x= que.front().first;
int y = que.front().second;
int v = dp[x][y];
if( x == n ){
ans = min( ans,v );
}
que.pop();
vis[x][y] = 0;
for( int cure = he[x];cure;cure = ne[cure] ){
int yy = ver[cure];
int cc = cost[cure];
if( dp[yy][y] > max(v,cc) ){
dp[yy][y] = max( v,cc );
if( !vis[yy][y] ) {
que.push(pii(yy, y));
vis[yy][y] = 1;
}
}
if( y < k && dp[yy][y+1] > v ){
dp[yy][y+1] = v;
if( !vis[yy][y+1] ) {
que.push(pii(yy, y + 1));
vis[yy][y + 1] = 1;
}
}
}
}
}
int main(){
int x,y,z;
while( 3 == scanf("%d%d%d",&n,&p,&k) ){
init(n,k);
for(int i = 1;i <= p;i++){
scanf("%d%d%d",&x,&y,&z);add(x,y,z);add(y,x,z);
}
ans = inf;
spfa(1);
if( ans == inf ) puts("-1");
else printf("%d\n",ans);
}
return 0;
}