poj 3662

 思路: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;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值