NC14700 追债之旅(单源最短路问题)

追债之旅

https://ac.nowcoder.com/acm/problem/14700

题目链接

题意:






题解:







AC代码

/*
    Author:zzugzx
    Lang:C++
    Blog:blog.csdn.net/qq_43756519
*/
#include<bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define endl '\n'
#define SZ(x) (int)x.size()
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int mod=1e9+7;
//const int mod=998244353;
const double eps = 1e-10;
const double pi=acos(-1.0);
const int maxn=1e6+10;
const ll inf=0x3f3f3f3f;
const int dir[][2]={{0,1},{1,0},{0,-1},{-1,0},{1,1},{1,-1},{-1,1},{-1,-1}};

struct node{
    int d,u,w;
    bool operator< (const node &p)const{
        return w>p.w;
    }
};
int n,m,k;
vector<pii> g[maxn];
int dis[1010][1010],a[maxn];
void dij(){
    memset(dis,inf,sizeof dis);
    dis[0][1]=0;
    priority_queue<node>q;
    q.push((node){0,1,0});
    while(!q.empty()){
        node p=q.top();
        q.pop();
        int u=p.u,d=p.d;
        if(p.w>dis[d][u])continue;
        for(auto i:g[u]){
            int v=i.fi,w=i.se;
            if(d+1<=k&&dis[d+1][v]>dis[d][u]+w+a[d+1]){
                dis[d+1][v]=dis[d][u]+w+a[d+1];
                q.push((node){d+1,v,dis[d+1][v]});
            }
        }
    }
}
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);
    cin>>n>>m>>k;
    for(int i=1;i<=m;i++){
        int u,v,w;
        cin>>u>>v>>w;
        g[u].pb(mp(v,w));
        g[v].pb(mp(u,w));
    }
    for(int i=1;i<=k;i++)cin>>a[i];
    dij();
    int ans=inf;
    for(int i=1;i<=k;i++)
        ans=min(ans,dis[i][n]);
    if(ans!=inf)cout<<ans;
    else cout<<-1;
    return 0;
}

每日一题 文章被收录于专栏

每日一题

全部评论

相关推荐

杨柳哥:这不是普通人,那这个钱的是天才
点赞 评论 收藏
分享
1 收藏 评论
分享
牛客网
牛客企业服务