思路:平时我们学最短路dis[i]表示从1-i的最短路是多少,那么题设添加了一个条件我们也多一维dis[i][j](j<=k)表示从1-i经过j天的最小花费,在跑最短路的时候判断一下是否超过K次,最后在dis[n][1——k]遍历一遍找最小值
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <set>
#include<iostream>
#include<vector>
#include<queue>
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define SIS std::ios::sync_with_stdio(false)
#define space putchar(' ')
#define enter putchar('\n')
#define lson root<<1
#define rson root<<1|1
typedef pair<int,int> PII;
const int mod=1e9+7;
const int N=1e5+10;
const int inf=0x7f7f7f7f;
ll gcd(ll a,ll b)
{
return b==0?a:gcd(b,a%b);
}
ll lcm(ll a,ll b)
{
return a*(b/gcd(a,b));
}
template <class T>
void read(T &x)
{
char c;
bool op = 0;
while(c = getchar(), c < '0' || c > '9')
if(c == '-')
op = 1;
x = c - '0';
while(c = getchar(), c >= '0' && c <= '9')
x = x * 10 + c - '0';
if(op)
x = -x;
}
template <class T>
void write(T x)
{
if(x < 0)
x = -x, putchar('-');
if(x >= 10)
write(x / 10);
putchar('0' + x % 10);
}
int n,m,k;
int a[N],head[N],cnt=0;
int dis[N][20];
struct node{
int to,nex;
int w;
}edge[N<<1];
struct eva{
int u,step;
int w;
bool operator <(const eva &tem)const
{
return w>tem.w;
}
};
void add(int u,int v,int w)
{
edge[++cnt]={v,head[u],w};
head[u]=cnt;
}
void dij()
{
priority_queue<eva> q;
q.push({1,0,0});
dis[1][0]=0;
while(!q.empty())
{
//cout<<2333<<endl;
eva now=q.top();
q.pop();
int u=now.u,step=now.step;
if(u==n||step>=k||now.w>dis[u][step])continue;
for(int i=head[now.u];~i;i=edge[i].nex)
{
//cout<<23333<<endl;
int cost=dis[u][step]+edge[i].w+a[step+1];
if(dis[edge[i].to][step+1]>cost)
{
//cout<<1111<<endl;
dis[edge[i].to][step+1]=cost;
q.push({edge[i].to,step+1,cost});
}
}
}
int ans=6e6;
for(int i=0;i<=k;i++) ans=min(ans,dis[n][i]);
if(ans==6e6)puts("-1");
else write(ans);
}
int main()
{
SIS;
fill(head,head+N,-1);
//memset(head,0,sizeof head);
memset(dis,0x3f,sizeof dis);
read(n);read(m);read(k);
for(int i=1;i<=m;i++)
{
int u,v,w;
read(u),read(v),read(w);
add(u,v,w);
add(v,u,w);
}
for(int i=1;i<=k;i++)
{
read(a[i]);
}
dij();
return 0;
}

京公网安备 11010502036488号