• 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. Python开发 第01课 Python 简介

    一.Python 介绍 python的创始人为吉多·范罗苏姆(Guido van Rossum).1989年的圣诞节期间,吉多·范罗苏姆为了在阿姆斯特丹打发时间,决心开发一个新的脚本解释程序,作为AB ...

  2. python入门之排序,文件操作

    排序 li.sort() 对li列表从小到大排序,直接更新li列表 sorted(li) 返回一个li排序后的列表,并非直接对li作更新 列表元素必须是同一种数据类型 文件操作 打开文件: f = o ...

  3. Linux scp 命令使用方法

    scp 命令: 1.将本地文件拷贝到远程:scp  文件名 用户名@计算机IP或者计算机名称:远程路径 2.从远程将文件拷回本地:scp  用户名@计算机IP或者计算机名称:文件名 本地路径 3.将本 ...

  4. nodejs 不是单线程

    nodejs 不是单线程 在我机器上,nodejs 起了近 20 个线程. 对,你没有看错,20个线程.

  5. GCD 使用说明

    GCD提供的一些操作队列的方法 名称 说明 dispatch_set_target_queue 将多个队列添加到目标队列中 dispatch_group 将多个队列放入组中,监听所有任务完成状 dis ...

  6. php通过类名查找这个类所在的路径(即实际引用的是哪个类)

    实际上就是应用了类的反射机制 class a{ public $a; protected $b; private $c; } $func = new ReflectionClass('a'); //所 ...

  7. iOS:让UIView覆盖导航栏

    当我们想做一个弹出式菜单时,想将导航栏也一起盖住不显示的话,可以用如下语句实现: UIView* myView = /* 你自定义的view */; UIWindow* currentWindow = ...

  8. [jQuery] Cannot read property ‘msie’ of undefined错误的解决方法 --转

    初用Yii的srbac模块.出现 Cannot read property ‘msie’ of undefined 错误.上网查询,找到如下的文章.使用文末的打补丁的方法,成功搞定.感谢. ===== ...

  9. sysbench0.5安装和使用介绍

    sysbench是一个模块化的.跨平台.多线程基准测试工具,主要用于评估测试各种不同系统参数下的数据库负载情况,sysbench支持MySQL.PostgreSQL.Oracle数据库OLTP测试.它 ...

  10. 使用python模拟登陆百度

    #!/usr/bin/python # -*- coding: utf- -*- """ Function: Used to demostrate how to use ...