There are N N cities in the country, and directional roads from u u to . Every road has a distance ci c i . Haze is a Magical Girl that lives in City 1, she can choose no more than K K roads and make their distances become 0. Now she wants to go to City , please help her calculate the minimum distance.
Input
The first line has one integer
T(1≤T≤5)
T
(
1
≤
T
≤
5
)
, then following
T
T
cases.
For each test case, the first line has three integers and K K .
Then the following M lines each line has three integers, describe a road, . There might be multiple edges between u u and .
It is guaranteed that N ≤100000,M≤200000,K≤10 ≤ 100000 , M ≤ 200000 , K ≤ 10 , 0≤Ci≤1e9 0 ≤ C i ≤ 1 e 9 . There is at least one path between City 1 and City N.
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
思路:DP+最短路。 d[i][j] d [ i ] [ j ] 表示到达第 i i 个城市,且已经使用了次机会的最短距离。
然后其它的就和最短路一样求就行了。
#include<bits/stdc++.h>
const int MAX=2e5+10;
const int MOD=1e9+7;
using namespace std;
typedef long long ll;
vector<pair<int,int> >e[MAX];
struct lenka
{
ll now,cost,t;
bool operator<(const lenka& p)const{return p.cost<cost;}
};
priority_queue<lenka>p;
int Time;
ll d[MAX][14];
void bfs()
{
d[1][0]=0;
p.push({1,0,0});
while(!p.empty())
{
lenka A=p.top();p.pop();
if(A.cost!=d[A.now][A.t])continue;
for(int i=0;i<e[A.now].size();i++)
{
pair<int,int>nex=e[A.now][i];
if(d[nex.first][A.t]==-1||d[nex.first][A.t]>A.cost+nex.second)//不把这条边的权值变为0
{
d[nex.first][A.t]=A.cost+nex.second;
p.push({nex.first,A.cost+nex.second,A.t});
}
if(A.t+1<=Time)
{
if(d[nex.first][A.t+1]==-1||d[nex.first][A.t+1]>A.cost)//把这条边的权值变为0
{
d[nex.first][A.t+1]=A.cost;
p.push({nex.first,A.cost,A.t+1});
}
}
}
}
}
int main()
{
int T;
cin>>T;
while(T--)
{
int n,m,k;
scanf("%d%d%d",&n,&m,&k);
for(int i=1;i<=n;i++)e[i].clear();
for(int i=1;i<=m;i++)
{
int x,y,z;
scanf("%d%d%d",&x,&y,&z);
e[x].push_back({y,z});
}
Time=k;
for(int i=1;i<=n;i++)
for(int j=0;j<=Time;j++)d[i][j]=-1;
bfs();
ll ans=1e18;
for(int i=0;i<=Time;i++)
{
if(d[n][i]!=-1)ans=min(ans,d[n][i]);
}
printf("%lld\n",ans);
}
return 0;
}