【每日一题】追债之旅
作者:互联网
追债之旅:最短路
https://ac.nowcoder.com/acm/problem/14700
思路:这题不是单纯的求1-n的最短路,而是求1-n种路径花费和+挥霍费和最小,所有我们修改dis[n]位dis[n][k],表示花费k天到到1-n的最小花费,同时标记数组为vis[n][k]。同样跑Dijktsra即可。
#include <bits/stdc++.h>
#define ull unsigned long long
#define ll long long
const int inf = 0x3f3f3f3f;
const int mod = 10007;
const int N = 1e3+7;
const int ds = 1e8+7;
const double PI = 3.141592653589793238462643383;
using namespace std;
struct node{
int d,day,val;
bool operator < (const node &x)const{
return val > x.val;
}
};
struct edge{
int v,next,val;
}edge[N*10*5];
int n,m,k;
int head[N],a[N],vis[N][55],cnt = 1;
int dis[N][55];
void add(int u,int v,int w)
{
edge[cnt].v = v;
edge[cnt].val = w;
edge[cnt].next = head[u];
head[u] = cnt++;
}
void init(){
for(int i = 0; i <= n; i++) head[i] = -1;
}
void dij(){
// for(int i = 0; i <= n; i++) vis[i] = 0;
for(int i = 0; i <= n; i++)
for(int j = 0; j <= k; j++)
dis[i][j] = ds,vis[i][j] = 0;
priority_queue<node>q;
q.push({1,0,0});
dis[1][0] = 0;
while(!q.empty()){
node t = q.top();
q.pop();
if(vis[t.d][t.day]) continue;
//if(t.d == n || t.day >= k) continue;
vis[t.d][t.day] = 1;
for(int i = head[t.d]; ~i; i = edge[i].next){
int v = edge[i].v;
if(t.day+1 > k) continue;
if(vis[v][t.day+1]) continue;
if(dis[v][t.day+1] > dis[t.d][t.day]+a[t.day+1]+edge[i].val){
dis[v][t.day+1] = dis[t.d][t.day]+a[t.day+1]+edge[i].val;
q.push({v,t.day+1,dis[v][t.day+1]});
}
}
}
}
void solve() {
int u,v,w;
cin >> n >> m >> k;
init();
for(int i = 1; i <= m; i++){
cin >> u >> v >> w;
add(u,v,w);
add(v,u,w);
}
for(int i = 1; i <= k; i++){
cin >> a[i];
}
dij();
int ans = ds;
for(int i = 0; i <= k; i++){
ans = min(ans,dis[n][i]);
}
ans == ds ? cout << "-1" : cout << ans;
}
int main() {
// int t;
// scanf("%d",&t);
// while(t--)
solve();
return 0;
}
标签:const,val,之旅,int,每日,追债,edge,day,dis 来源: https://blog.csdn.net/qq_46653910/article/details/115217551