• 262144K
 

There are NN cities in the country, and MM directional 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

题目来源

ACM-ICPC 2018 南京赛区网络预赛

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <utility>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
using namespace std;
#define N 100009
#define M 200009
#define lowbit(x) x&(-x)
#define ll long long
const ll inf =9e18;
int head[N],t,n,m,k,cnt;
ll dp[N][];
struct Edge{
int from,to,nex;
ll w;
}e[M*];//当然本题不用*2(有向图)
struct Node{
int a,b;// a:终点 ,b : 已经用了几次免费路
ll dis;//1到a的当前最短距离
bool operator <(const Node &p)const{
return dis>p.dis;//因此下面只能用priority_queue<Node>que;
//return dis<p.dis; 是错的,不可以这么定义
}
};
void init()
{
for(int i=;i<=n;i++){
head[i]=-;
}
cnt=;
}
void add(int u,int v,ll val)// ll val
{
e[cnt].from=u;
e[cnt].to=v;
e[cnt].nex=head[u];
e[cnt].w=val;
head[u]=cnt++;
}
void bfs()
{
for(int i=;i<=n;i++)
{
for(int j=;j<=;j++){
dp[i][j]=inf;
}
}
dp[][]=;//dp[i][j] :从1到i ,已经有j次免费的路的最短路径
priority_queue<Node>que;
que.push(Node{,,});
while(!que.empty()){
Node tmp=que.top();
que.pop();
int u=tmp.a;
int b=tmp.b;
for(int i=head[u];i!=-;i=e[i].nex){
int v=e[i].to;
if(dp[v][b]>tmp.dis+e[i].w){//这条路不当作免费路
dp[v][b]=tmp.dis+e[i].w;
que.push(Node{v,b,dp[v][b]});
}
if(b+<=k){
if(dp[v][b+]>tmp.dis){//这条路当作免费路
dp[v][b+]=tmp.dis;
que.push(Node{v,b+,tmp.dis});
}
}
}
}
}
int main()
{
scanf("%d",&t);
while(t--)
{
scanf("%d%d%d",&n,&m,&k);
init();
int u,v;
ll w;
for(int i=;i<m;i++)
{
scanf("%d%d%lld",&u,&v,&w);
add(u,v,w);
}
bfs();
printf("%lld\n",dp[n][k]);
}
return ;
}

最新文章

  1. jQuery 学习笔记
  2. 错误:E:Unable to locate package ...
  3. java线程与并发(二)
  4. C#设计模式(23)——备忘录模式(Memento Pattern)
  5. C# 对MongoDB 进行增删改查的简单操作 (转)
  6. 谈谈 Mifare Classic 破解
  7. php eval函数用法总结
  8. js类型判断及鸭式辨型
  9. Zimbra8.x邮件服务器安装及配置
  10. nexus私服安装
  11. C++ 文件的复制、删除、重命名
  12. C语言数据结构-创建链表的四种方法
  13. oracle connect by 说明
  14. 挂载了Cinder Volume的实例无法动态迁移排错
  15. iOS8学习笔记-构建多视图应用程序
  16. 深入理解分布式调度框架TBSchedule及源码分析
  17. Android带有粘性头部的ScrollView
  18. [前端]如何写一个水平导航栏?(浮动、inline-block+消除间距)
  19. python接口自动化(九)--python中字典和json的区别(详解)
  20. python练习:实现一个整数数组里面两个数之和为183的所有整数对

热门文章

  1. 2017&quot;百度之星&quot;程序设计大赛 - 资格赛 寻找母串
  2. 洛谷P2514||bzoj2426 [HAOI2010]工厂选址
  3. PARTITION RANGE ALL 的优化
  4. Script to Monitor Current User Activity in the Database
  5. 085 Maximal Rectangle 最大矩形
  6. C# readonly和const的区别
  7. Unity Shader入门精要学习笔记 - 第11章 让画面动起来
  8. 一些API
  9. elasticsearch的模糊查询
  10. CF1060C Maximum Subrectangle