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个点编号从1到n,共有m条有向边,问从1到n的最短路,如果有权利使路径上的k条边的花费变为0。

分层图dijkstra。

令dis[i][j]为i点使路径上j条边的花费变为0的情况下的最小距离。这样在更新dis数表的时候,遇到一条边,可以考虑用或不用这条边。这样跑dijkstra就可以了。(感觉上建图直接分k+1层也可以)

dijkstra堆优化算法需要掌握。在边权为正值时,它比spfa速度更稳定(spfa速度会退化)。实际上,这道题就是卡spfa的。

#include<cstdio>
#include<algorithm>
#include<cstring>
#include<queue> using namespace std; const int maxn=;
const int maxm=;
const int inf=; int to[maxm+];
int w[maxm+];
int nex[maxm+];
int head[maxn+]; int dis[maxn+][];
struct tnode
{
int a,b;
int dis;
bool operator<(const tnode& y) const
{
return dis>y.dis;
}
}; int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int n,m,k;
scanf("%d%d%d",&n,&m,&k);
memset(head,-,sizeof(head));
for(int i=,u,v,c;i<m;i++)
{
scanf("%d%d%d",&u,&v,&c);
to[i]=v;w[i]=c;
nex[i]=head[u];head[u]=i;
} for(int i=;i<=n;i++)
{
for(int j=;j<=k;j++)
dis[i][j]=inf;
}
priority_queue<tnode> q;
dis[][]=;
q.push((tnode){,,});
while(!q.empty())
{
tnode node=q.top();q.pop();
int a=node.a,b=node.b,d=node.dis;
if(d!=dis[a][b]) continue;
for(int i=head[a];i!=-;i=nex[i])
{
int u=a,v=to[i],c=w[i];
if(dis[v][b]>dis[u][b]+c)
{
dis[v][b]=dis[u][b]+c;
q.push((tnode){v,b,dis[v][b]});
}
if(b!=k&&dis[v][b+]>dis[u][b])
{
dis[v][b+]=dis[u][b];
q.push((tnode){v,b+,dis[v][b+]});
}
}
} int ans=inf;
for(int i=;i<=k;i++)
{
ans=min(ans,dis[n][i]);
}
printf("%d\n",ans);
}
return ;
}

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

  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 (分层迪杰斯特拉)

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

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

    262144K   There are NN cities in the country, and MM directional roads from uu to v(1\le u, v\le n)v ...

  4. 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, ...

  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. Vue_声明周期

    Vue生命周期 在vue2.0的时候,声明钩子发生了改变,具体有八个 <!-- HTML部分 --> <div id="app"> <div>{ ...

  2. 50.Qt-QJsonDocument读写json

    QJsonDocument: 提供一种读取和写入JSON文档的方法,可以通过它的的成员函数array()或object()检索文档中包含的数组或对象,然后读取JSON数据,或者修改数据. QJsonO ...

  3. Linux(CentOS65)

    首先下载VMware,然后下载CentOS镜像文件,VM的版本尽量高一点,因为软件一般都有向下兼容性,如果版本太低,可能匹配不了CentOS. 安装VMTools工具 主要用于虚拟主机显示优化与调整, ...

  4. PHP基于Redis实现轻量级延迟队列

    延迟队列,顾名思义它是一种带有延迟功能的消息队列. 那么,是在什么场景下我才需要这样的队列呢? 一.背景 先看看一下业务场景: 1.会员过期前3天发送召回通知 2.订单支付成功后,5分钟后检测下游环节 ...

  5. 2019牛客暑期多校训练营(第九场)Quadratic equation——二次剩余(模奇素数)

    题意:给定p=1e9+7,构造x,y使其满足(x+y) mod p = b,(x*y) mod p = c . 思路:不考虑取模的情况下, .在取模的意义下,,因为a是模p的二次剩余的充分必要条件为  ...

  6. 浏览器中的 Event Loop

    当我们执行 JS 代码的时候其实就是往执行栈中放入函数,那么遇到异步代码的时候该怎么办?其实当遇到异步的代码时,会被挂起并在需要执行的时候加入到 Task(有多种 Task) 队列中.一旦执行栈为空, ...

  7. scrapy框架安装配置

    scrapy框架 scrapy安装(win) 1.pip insatll wheel 2.下载合适的版本的twisted:http://www.lfd.uci.edu/~gohlke/pythonli ...

  8. spring boot 一个项目启动多个实例

    0.前言 在开发中,我们经常需要以不同端口启动同一个项目的多个实例,IDEA中启动多个实例很简单 1.方法 1.1.在项目中,选择编辑配置,然后点选允许并行运行,如下图: 1.2.调出RunDashb ...

  9. 从React 编程到"好莱坞"

    目录 概念 面向流设计 异步化 响应式宣言 参考文档 概念 Reactive Programming(响应式编程)已经不是一个新东西了. 关于 Reactive 其实是一个泛化的概念,由于很抽象,一些 ...

  10. Spring MVC中的Controller是Serlvet吗?

    1. Controller不是Servlet DispatcherServler是Spring MVC中的唯一Servlet,(这点可通过查看FrameworkServlet的子类确认) Servle ...