There are NN cities in the country, and MMdirectional roads from uu to v(1\le u, v\le n)v(1≤u,v≤n). Every road has a distance c_ici. Haze is a Magical Girl that lives in City 11, she can choose no more than KK roads and make their distances become 00. Now she wants to go to City NN, please help her calculate the minimum distance.
Input
The first line has one integer T(1 \le T\le 5)T(1≤T≤5), then following TT cases.
For each test case, the first line has three integers N, MN,M and KK.
Then the following MM lines each line has three integers, describe a road, U_i, V_i, C_iUi,Vi,Ci. There might be multiple edges between uu and vv.
It is guaranteed that N \le 100000, M \le 200000, K \le 10N≤100000,M≤200000,K≤10,
0 \le C_i \le 1e90≤Ci≤1e9. There is at least one path between City 11 and City NN.
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的最短距离。
解题思路:
因为题目是从始点1到终点n,可用dijkstra求出每一点到起点的最短距离。因为可使k条路的距离变为0,所以用dp的方法推出所有的情况。
#include<cstdio>
#include<queue>
#include<vector>
#include<utility>
#include<iostream>
#include<cstring>
using namespace std;
typedef long long ll;
vector<pair<int,ll> >ve[100005];
ll dis[10000+5][10+2]; //dis[i][j]表示到达城市i消除j条路的最短距离
bool visited[100005][12];
int m,n,k;
typedef struct node{
int a,b; //a表示到达城市a,b表示消除b条边
ll dis; //距离
node(int aa=0,int bb=0,ll dd=0):a(aa),b(bb),dis(dd){}
bool operator<(const node&n)const{
return dis > n.dis;
}
}node;
void dijkstra()
{
memset(dis,0x3f3f3f3f3f,sizeof(dis)); //必须用16进制表示无穷大
memset(visited,false,sizeof(visited));
priority_queue<node>pq;
pq.push(node(1,0,0));
dis[1][0] = 0;
while(!pq.empty()){
node n = pq.top();
pq.pop();
if(visited[n.a][n.b]){
continue;
}
visited[n.a][n.b] = true;
int len = ve[n.a].size();
for(int i = 0;i < len;i++){ //dp思想推出每一种情况
int x = ve[n.a][i].first;
if(dis[x][n.b] > n.dis+ve[n.a][i].second){ //当前这条路不消除的情况
dis[x][n.b] = n.dis+ve[n.a][i].second;
pq.push(node(x,n.b,dis[x][n.b]));
}
if(n.b+1 <= k){ //消除当前这条路
if(dis[x][n.b+1] > n.dis){
dis[x][n.b+1] = n.dis;
pq.push(node(x,n.b+1,dis[x][n.b+1]));
}
}
}
}
}
int main()
{
int t,u,v;
ll d;
scanf("%d",&t);
while(t--){
scanf("%d%d%d",&n,&m,&k);
for(int i = 0;i < n;i++){
ve[i].clear();
}
for(int i = 0;i < m;i++){
scanf("%d%d%lld",&u,&v,&d);
ve[u].push_back(make_pair(v,d));
}
dijkstra();
printf("%d\n",dis[n][k]);
}
}