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

题解:分层图思想+spfa+优先队列。

代码:

#include <iostream>
#include <stdlib.h>
#include <algorithm>
#include <stdio.h>
#include <string.h>
#include <vector>
#include <queue>
#include <bits/stdc++.h>
#define inf 0x3f3f3f3fusing namespace std;typedef pair<int,int> p;struct node
{int u,v,w,next;
}h[5000000];struct node1
{int s,k;
}m,n;int head[100200];
int a,b,c,t;
int vis[100200][12];
long long int dis[100200][12];void addre(int u, int v,int w)
{h[t].u=u;h[t].v=v;h[t].w=w;h[t].next=head[u];head[u]=t++;
}void spfa()
{priority_queue<p,vector<p>,greater<p> > Q;memset(dis,inf,sizeof(dis));Q.push(make_pair(1,0));dis[1][0]=0;while(Q.size()){p now=Q.top();Q.pop();n.s=now.first;n.k=now.second;int i;for(i=head[n.s];i!=-1;i=h[i].next){if(dis[h[i].v][n.k]>dis[n.s][n.k]+h[i].w){dis[h[i].v][n.k]=dis[n.s][n.k]+h[i].w;Q.push(make_pair(h[i].v,n.k));}if(n.k+1<=c){if(dis[h[i].v][n.k+1]>dis[n.s][n.k]){dis[h[i].v][n.k+1]=dis[n.s][n.k];Q.push(make_pair(h[i].v,n.k+1));}}}}
}int main()
{int i,j;int u,v,w;scanf("%d",&j);while(j--){scanf("%d %d %d",&a,&b,&c);t=0;memset(head,-1,sizeof(head));for(i=1;i<=b;i++){scanf("%d %d %d",&u,&v,&w);addre(u,v,w);}spfa();printf("%lld\n",dis[a][c]);}return 0;
}

Magical Girl Haze相关推荐

  1. 2018 ACM-ICPC南京网络赛 Magical Girl Haze(分层最短路)

    2018 ACM-ICPC南京网络赛 Magical Girl Haze There are NN cities in the country, and MM directional roads fr ...

  2. ACM-ICPC 2018 南京赛区网络预赛 L. Magical Girl Haze 最短路+分层图

    类似题解 There are NN cities in the country, and MM directional roads from uu to v(1\le u, v\le n)v(1≤u, ...

  3. 2018南京网络赛L题 Magical Girl Haze(分层图+优先队列优化的dijkstra)

    使用优先队列优化过的dijkstra时间复杂度可以达到O(v*logn),还是很快的. #include <iostream>                //最好是用long long ...

  4. 【分层最短路】Magical Girl Haze

    https://nanti.jisuanke.com/t/31001 有K次机会可以让一条边的权值变为0,求最短路. 在存储单源最短路的数组上多开一维状态,d[i][k]表示走到序号i的点,且让k条边 ...

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

    轻轻松松也能拿到区域赛名额,CCPC真的好难 An Olympian Math Problem 问答 只看题面 54.76% 1000ms 65536K Alice, a student of gra ...

  6. Why is OFDMA a Magical Feature in the 802.11ax Standard?

    OFDMA significantly reduces contention and preamble overhead, especially for short packets prevalent ...

  7. codeforces654题解_[codeforces 1374A] Magical Sticks 棍子拼接

    Codeforces Round #654 (Div. 2)   参与排名人数14349   本场比赛主要心思放在观摩高手如何打比赛,发现初中生真的很厉害. [codeforces 1374A]    ...

  8. Magical Sticks 棍子拼接

    Magical Sticks 棍子拼接 A penguin Rocher has n sticks. He has exactly one stick with length i for all 1≤ ...

  9. 【CodeForces - 270C】Magical Boxes (思维,进制,有坑)

    题干: Emuskald is a well-known illusionist. One of his trademark tricks involves a set of magical boxe ...

最新文章

  1. Spring Boot 中的 @EnableAutoConfiguration 是如何处理的?
  2. Android 关于::app:clean :app:preBuild UP-TO-DATE :app:preDebugBuild UP-TO-DATE,引用jar冲突问题...
  3. ActionBarSherlock包的使用
  4. mask - 使用 * 遮蔽字符串
  5. 正则表达式——全部符号解释(详解)
  6. (chap1 网络基础知识)OSI参考模型举例
  7. dubbo管控台安装
  8. win7安装mysql 5.7.25_Windows下安装mysql-5.7.25-winx64.zip
  9. 云原生数据湖分析DLA 2020年年度总结
  10. java 8 lambda_异常作弊– Java 8 Lambdas
  11. gdb 调试java进程_使用GDB调试JNI代码
  12. AcWing 1057. 股票买卖 IV
  13. 宅男、游戏、美女,一场不一样的技术公开课让你老泪纵横
  14. PyCharm 重构(refactor)快捷键
  15. GitHub 实现了子资源完整性(SRI)
  16. 基于STM32MINI板步进电机程序(有代码)
  17. c语言用后缀字母表示不同数制,C语言基础知识总结
  18. 硬盘分区表错误与解决办法
  19. rxjava背压_RxJava背压
  20. 3.2 电话号码对应英语单词

热门文章

  1. MySQL基础知识笔记
  2. 北京内推 | 腾讯IEG内容推荐中心计算广告算法组招聘算法实习生
  3. Linux下 SpeedTest 工具测速
  4. 鲲鹏服务器主板销售策略,市场伙伴优先:以行践言,华为鲲鹏做到了
  5. sys文件系统的创建和初始化过程
  6. Elasticseach实践1
  7. Swan Song 第七周Scrum Meeting
  8. 献礼20周年! 互联网研发管理现状主题分享
  9. 一个人赶着鸭子去每个村庄卖,每经过一个 村子卖去所赶鸭子的一半又//一只。 这样他经过了 七个村子后还剩 两只鸭子,问问他出发时共赶多少//只鸭子?经过每个村子卖出多少只鸭子?
  10. 春节没有年味?今年就去这些年味浓的地方过年吧!