• 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 ;
}

ACM-ICPC 2018 南京赛区网络预赛 L. Magical Girl Haze的更多相关文章

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

    There are N cities in the country, and M directional roads from u to v(1≤u,v≤n). Every road has a di ...

  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. ACM-ICPC 2018 南京赛区网络预赛 - L Magical Girl Haze (分层迪杰斯特拉)

    题意:N个点,M条带权有向边,求可以免费K条边权值的情况下,从点1到点N的最短路. 分析:K<=10,用dist[i][j]表示从源点出发到点i,免费j条边的最小花费.在迪杰斯特拉的dfs过程中 ...

  4. ACM-ICPC 2018 南京赛区网络预赛 L. Magical Girl Haze (分层dijkstra)

    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). ...

  5. ACM-ICPC 2018 南京赛区网络预赛 L题(分层最短路)

    题目链接:https://nanti.jisuanke.com/t/31001 题目大意:给出一个含有n个点m条边的带权有向图,求1号顶点到n号顶点的最短路,可以使<=k条任意边的权值变为0. ...

  6. ACM-ICPC 2018 南京赛区网络预赛 L 【分层图最短路】

    <题目链接> 题目大意: 有N个城市,这些城市之间有M条有向边,每条边有权值,能够选择K条边 边权置为0,求1到N的最短距离. 解题分析: 分层图最短路模板题,将该图看成 K+1 层图,然 ...

  7. ACM-ICPC 2018 南京赛区网络预赛 L题(分层图,堆优化)

    题目链接: https://nanti.jisuanke.com/t/31001 超时代码: #include<bits/stdc++.h> using namespace std; # ...

  8. ACM-ICPC 2018 南京赛区网络预赛 L && BZOJ 2763 分层最短路

    https://nanti.jisuanke.com/t/31001 题意 可以把k条边的权值变为0,求s到t的最短路 解析  分层最短路  我们建立k+1层图 层与层之间边权为0,i 向 i+1层转 ...

  9. 【ACM-ICPC 2018 南京赛区网络预赛 L】Magical Girl Haze

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 定义dis[i][j]表示到达i这个点. 用掉了j次去除边的机会的最短路. dis[1][0]= 0; 在写松弛条件的时候. 如果用 ...

随机推荐

  1. [软件工程基础]2017.10.31 第四次 Scrum 会议

    具体事项 项目交接燃尽图 每人工作内容 成员 已完成的工作 计划完成的工作 工作中遇到的困难 游心 #7 掌握 PHP:#6 阅读分析 PhyLab 数据处理相关代码 #10 搭建可用的开发测试环境: ...

  2. Shortest Path Codeforces - 59E || 洛谷P1811 最短路_NOI导刊2011提高(01)

    https://codeforces.com/contest/59/problem/E 原来以为不会..看了题解发现貌似自己其实是会的? 就是拆点最短路..拆成n^2个点,每个点用(i,j)表示,表示 ...

  3. centos 6.x下pxe+tftp+http+kickstart无人值守安装操作系统

    1.1 什么是PXE PXE(Pre-boot Execution Environment,预启动执行环境)是由Intel公司开发的最新技术,工作于Client/Server的网络模式,支持工作站通过 ...

  4. 541 Reverse String II 反转字符串 II

    给定一个字符串和一个整数 k,你需要对从字符串开头算起的每个 2k 个字符的前k个字符进行反转.如果剩余少于 k 个字符,则将剩余的所有全部反转.如果有小于 2k 但大于或等于 k 个字符,则反转前 ...

  5. HackerRank Super Six Substrings dp

    https://www.hackerrank.com/contests/hourrank-18/challenges/super-six-substrings 能被6整除的数有一个特点,就是能同时被3 ...

  6. android的handle

    Handler的定义:  用来接收子线程发送过来的数据,并利用该数据直接更新主线程的UI. 安卓中,一个应用启动时会开启一个主线程(UI线程),他的责任是负责管理界面中的控件.比如当你点击一个Butt ...

  7. Objective-C Composite Objects

    We can create subclass within a class cluster that defines a class that embeds within it an object. ...

  8. path与classpath区别(转)

    转自http://blog.csdn.net/mydreamongo/article/details/8155408 1.path的作用 path是系统用来指定可执行文件的完整路径,即使不在path中 ...

  9. LR中订单流程脚本

    Action(){ /* 主流程:登录->下订单->支付订单->获取订单列表 定义事物 1)登录 2)下订单 3)支付订单 4)获取订单列表 接口为:application/json ...

  10. UVA1660 Cable TV Network (无向图的点连通度)

    题意:求一个无向图的点连通度. 把一个点拆成一个入点和一个出点,之间连一条容量为1的有向边,表示能被用一次.最大流求最小割即可. 一些细节的东西:1.源点固定,汇点要枚举一遍,因为最小割割断以后会形成 ...