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; 在写松弛条件的时候. 如果用 ...
随机推荐
- laravel之null替换空字符串中间件
在laravel写接口的时候免不了数据库中保存null,可用诸如设置ORM的访问器或以下方法处理 $goods->name?$goods->name:''; 其实可以利用路由中间件,在需要 ...
- centOS+uwsgi+nginx 部署flask项目,问题记录
用flask做的项目想要部署到centOS系统上,填了一些坑,终于成功了,记录一下遇到的问题: 此次部署主要是按照这个博客进行的 https://www.cnblogs.com/Ray-liang/p ...
- c/c++学习系列之内存对齐
1.C++内存对齐规则 每个特定平台上的编译器都有自己的默认“对齐系数”(也叫对齐模数).程序员可以通过预编译命令#pragma pack(n),n=1,2,4,8,16来改变这一系数,其中的n就是你 ...
- Jquery EasyUI 中ValidateBox验证框使用讲解(转)
Validatebox(验证框)的设计目的是为了验证输入的表单字段是否有效.如果用户输入了无效的值,它将会更改输入框的背景颜色,并且显示警告图标和提示信息.该验证框可以结合form(表单)插件并防止表 ...
- Java编程基础-面向对象(中)
本章承接Java编程基础-面向对象(上)一文. 一.static关键字 在java中,定义了一个static关键字,它用于修饰类的成员,如成员变量.成员方法以及代码块等,被static修饰的成员具备一 ...
- 在webconfig放置固定值
通常的,为了布置到服务器后修改的方便通常把一些会改变的值放在webconfig: 首先在web.ocnfig中放入如下值 <appSettings> <add key="A ...
- JavaScript中,关于class的调用
PS:class的调用,其实是可以叠加的,当然了这要求样式不同的情况下,如果样式相同,则后一个样式会覆盖前一个样式. 1.举例如下: <div id="test" class ...
- linux各文件夹的作用(转)
转自:http://www.cnblogs.com/amboyna/archive/2008/02/16/1070474.html linux下的文件结构,看看每个文件夹都是干吗用的/bin 二进制可 ...
- SPM-软件项目管理之感想
这学期开始选择选修课的时候是需要把每节课都过一遍的.当我上完SPM那节课的时候,我就已经决定要选这门课了,尽管还有其他的课我都还没上过.由于这节课是双语教学-中文老师兼外籍老师,这样的方式感觉很新颖, ...
- 关于ubuntu终端全屏的时候不能显示底部
最近在win7的电脑上装了ubuntu,也就是双系统.打算之后工作就直接进入ubuntu,减少之前win7和虚拟机之间的切换.进入ubuntu后,发现一个奇怪的问题是,在终端全屏的时候,底部总是有几行 ...