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(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
题目来源
- #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的更多相关文章
- 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 ...
- 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, ...
- ACM-ICPC 2018 南京赛区网络预赛 - L Magical Girl Haze (分层迪杰斯特拉)
题意:N个点,M条带权有向边,求可以免费K条边权值的情况下,从点1到点N的最短路. 分析:K<=10,用dist[i][j]表示从源点出发到点i,免费j条边的最小花费.在迪杰斯特拉的dfs过程中 ...
- 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). ...
- ACM-ICPC 2018 南京赛区网络预赛 L题(分层最短路)
题目链接:https://nanti.jisuanke.com/t/31001 题目大意:给出一个含有n个点m条边的带权有向图,求1号顶点到n号顶点的最短路,可以使<=k条任意边的权值变为0. ...
- ACM-ICPC 2018 南京赛区网络预赛 L 【分层图最短路】
<题目链接> 题目大意: 有N个城市,这些城市之间有M条有向边,每条边有权值,能够选择K条边 边权置为0,求1到N的最短距离. 解题分析: 分层图最短路模板题,将该图看成 K+1 层图,然 ...
- ACM-ICPC 2018 南京赛区网络预赛 L题(分层图,堆优化)
题目链接: https://nanti.jisuanke.com/t/31001 超时代码: #include<bits/stdc++.h> using namespace std; # ...
- ACM-ICPC 2018 南京赛区网络预赛 L && BZOJ 2763 分层最短路
https://nanti.jisuanke.com/t/31001 题意 可以把k条边的权值变为0,求s到t的最短路 解析 分层最短路 我们建立k+1层图 层与层之间边权为0,i 向 i+1层转 ...
- 【ACM-ICPC 2018 南京赛区网络预赛 L】Magical Girl Haze
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 定义dis[i][j]表示到达i这个点. 用掉了j次去除边的机会的最短路. dis[1][0]= 0; 在写松弛条件的时候. 如果用 ...
随机推荐
- netty与MQ使用心得
最近在做分布式的系统,使用netty与mq进行远程RPC调用,现将心得经验总结一下. 我们公司的服务器在云端机房,在每一个店面有一个服务器,店面服务器外网无法访问. 我们的做法是店面服务器在启动时与云 ...
- 牛客网Java刷题知识点之什么是迭代器
不多说,直接上干货! https://www.nowcoder.com/ta/review-java/review?query=&asc=true&order=&page=20 ...
- laravel 5.5 oauth2.0 跨域问题解决方案
一.laravel-Cors 安装 在终端执行安装命令如下: composer require barryvdh/laravel-cors 添加服务提供商 在Laravel配置文件app.php的pr ...
- 利用樹霉派採集溫濕度上傳到OneNET(非完整,僅參考)
看圖: Python代碼: #env /usr/bin/python3 #author Bruce import RPi.GPIO as GPIO import time import json im ...
- mongodb 正则
正则表达式常用来在所有语言中搜索字符串的任何模式或文字.MongoDB还提供了正则表达式功能的字符串模式使用正则表达式$regex操作符.MongoDB使用PCRE(Perl兼容正则表达式)为正则表达 ...
- 使用 Visual Studio 2017 部署 Azure 应用服务的 Web 应用
本快速入门介绍了如何使用 Visual Studio 2017 创建并部署 Azure Web 应用.在本教程中完成的所有操作均符合1元试用条件. 本快速入门介绍了如何使用 Visual Studio ...
- mybatis insert、update 、delete默认返回值解释与如何设置返回表主键
在使用mybatis做持久层时,insert.update.delete,sql语句默认是不返回被操作记录主键的,而是返回被操作记录条数: 那么如果想要得到被操作记录的主键,可以通过下面的配置方式获取 ...
- codevs 3129 奶牛代理商IX
时间限制: 1 s 空间限制: 32000 KB 题目等级 : 白银 Silver 题目描述 Description 小X从美国回来后,成为了USACO中国区的奶牛销售代理商,专门出售质优价廉的“ ...
- 成魔笔记1——先入IT,再成魔
关于我为什么要写这个博客的原因,做一个简单的解释.因为报考的一时兴起,我选择了软件专业.可是三年下来,感觉自己没做多少事,也没收获到多少东西.很多时候都是老师讲什么,都是完全陌生的东西,跟不上教学的思 ...
- npm scripts的生命周期管理
我们平时阅读一些开源项目,可能会发现有些项目的package.json里的scripts区域定义的脚本很复杂,令人眼花缭乱. 其实这些脚本是有规律可循的.让我们从最简单的一个例子开始学习. 新建一个空 ...