There are NNN cities in the country, and MMM directional roads from uuu to v(1≤u,v≤n)v(1\le u, v\le n)v(1≤u,v≤n). Every road has a distance cic_ici. Haze is a Magical Girl that lives in City 111, she can choose no more than KKK roads and make their distances become 000. Now she wants to go to City NNN, please help her calculate the minimum distance.
Input
The first line has one integer T(1≤T≤5)T(1 \le T\le 5)T(1≤T≤5), then following TTT cases.
For each test case, the first line has three integers N,MN, MN,M and KKK.
Then the following MMM lines each line has three integers, describe a road, Ui,Vi,CiU_i, V_i, C_iUi,Vi,Ci. There might be multiple edges between uuu and vvv.
It is guaranteed that N≤100000,M≤200000,K≤10N \le 100000, M \le 200000, K \le 10N≤100000,M≤200000,K≤10,
0≤Ci≤1e90 \le C_i \le 1e90≤Ci≤1e9. There is at least one path between City 111 and City NNN.
Output
For each test case, print the minimum distance.
样例输入
1 5 6 1 1 2 2 1 3 4 2 4 3 3 4 1 3 5 6 4 5 2
样例输出
3
题意:一共n个点,m条边,每条边是有向边,且含有权值,一共有k次机会,使得这条边的权值变成0,求从点1到点n最小需要花费多少。
思路:分层图的最短路,开一个二维数组dis[u][lev]表示走到u点,已经用了lev次机会的最小花费,然后跑SPFA
#include<cstdio>
#include<cstring>
#include<cmath>
#include<queue>
#include<algorithm>
#define Inf 0x7f7f7f7f
using namespace std;
typedef long long LL;
const int N = 100005;
struct node{
int v,w;
int next;
}edge[N*2];
struct Node{
int x;
LL w;
bool operator<(const Node other)const{
return w>other.w;
}
Node(){
}
Node(int vv,LL ww){
x=vv;
w=ww;
}
};
int id,head[N];
int n,m,k;
LL dis[N][12];
int vis[N][12];
void init(){
id=0;
memset(head,-1,sizeof(head));
}
void add(int u,int v,int w){
edge[id].v=v;
edge[id].w=w;
edge[id].next=head[u];
head[u]=id++;
}
void SPFA(){
memset(vis,0,sizeof(vis));
memset(dis,Inf,sizeof(dis));
priority_queue<Node>p;
p.push(Node(1,0));
dis[1][0]=0;
while(!p.empty()){
Node tmp=p.top();
p.pop();
int lev=tmp.x/n;
int u=tmp.x%n;
if(vis[u][lev]) continue; //已经在优先队列里
vis[u][lev]=1;
for(int i=head[u];i!=-1;i=edge[i].next){
int v=edge[i].v;
LL w=edge[i].w;
if(dis[u][lev]+w<dis[v][lev]){
dis[v][lev]=dis[u][lev]+w;
p.push(Node(lev*n+v,dis[v][lev]));
}
if(lev==k) continue; //最多k次机会
if(dis[u][lev]<dis[v][lev+1]){
dis[v][lev+1]=dis[u][lev];
p.push(Node(lev*n+v+n,dis[v][lev+1]));
}
}
}
printf("%lld\n",dis[n][k]);
}
int main(){
int T;
scanf("%d",&T);
while(T--){
init();
scanf("%d%d%d",&n,&m,&k);
for(int i=0;i<m;i++){
int u,v,w;
scanf("%d%d%d",&u,&v,&w);
add(u,v,w);
}
SPFA();
}
return 0;
}