There are N cities in the country, and M directional roads from u to v(1<=u, v<=n).Every road
has a distance ci. Haze is a Magical Girl that lives in City 1, she can choose no more than K roads
and make their distances become 0. Now she wants to go to City N, please help her calculate
the minimum distance.
Input
The first line has one integer T(1<=T<=5), then following T cases.
For each test case, the first line has three integers N, M and K. Then the following M lines each
line has three integers, describe a road, Ui, Vi, Ci. There might be multiple edges between u and v.
It is guaranteed that N<=100000, M<=200000, K<=10, 0<=c[i]<=1e9. There is at least one path
between City 1 and City N.
Output
For each test case, print the minimum distance.
Sample Input
1
5 6 1
1 2 2
1 3 4
2 4 3
3 4 1
3 5 6
4 5 2
Sample Output
3
题目链接:https://nanti.jisuanke.com/t/31001
#include<iostream>
#include<cstdio>
#include<queue>
#include<cstring>
#define INF 1000000000000000000
using namespace std;
int gcd(int p, int q) {return q==0?p:gcd(q,p%q);}
const int N=2000000+10;
int n,m,k, st,ed,cnt,head[N], v[N];
long long dis[N];
struct node
{
int from,to,next;
long long val;
}edge[N*4+10];
struct element
{
int no;
long long val;
};
bool operator < (element a,element b)
{
if(a.val==b.val)return a.no<b.no;
return a.val>b.val;
}
priority_queue<element>q;
void dijikstra(int s,int e)
{
for(int i=0;i<N;i++)
{
dis[i]=INF;
}
element fir;
fir.val=0,fir.no =s;
dis[s]=0;
q.push(fir);
while(!q.empty()){
element u=q.top();
q.pop();
if(v[u.no])continue;
v[u.no]=1;
for(int i=head[u.no];i!=-1;i=edge[i].next)
{
int to=edge[i].to;
if(dis[u.no]+edge[i].val<dis[to])
{
dis[to]=dis[u.no]+edge[i].val;
element pus;
pus.no =to,pus.val=dis[to];
q.push(pus);
}
}
}
long long ans = INF;
for(int i=0;i<=k;i++){
ans=min(ans,dis[e+i*n]);
}
printf("%lld\n",ans);
}
void init(){
memset(head,-1,sizeof(head));
cnt=1;
memset(v,0,sizeof v);
while(!q.empty()){
q.pop();
}
}
void EdgeAdd(int from,int to,long long val){
edge[cnt].from=from;
edge[cnt].to=to;
edge[cnt].val=val;
edge[cnt].next=head[from];
head[from]=cnt++;
}
int main(){
int t;
scanf("%d",&t);
while(t--){
init();
scanf("%d%d%d",&n,&m,&k);
st=1,ed=n;
for(int i=1;i<=m;i++)
{
int x,y;
long long gbk;
scanf("%d%d%lld",&x,&y,&gbk);
for(int i=0;i<=k;i++)
{
EdgeAdd(x+i*n,y+i*n,gbk);
if(i!=k){
EdgeAdd(x+i*n,y+(i+1)*n,0);
}
}
}
dijikstra(st,ed);
}
}