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 distance ci. Haze is a Magical Girl that lives in City 1, she can choose no more than K roads and make their distances become 0. Now she wants to go to City N, please help her calculate the minimum distance.
Input
The first line has one integer T(1≤T≤5), then following T cases.
For each test case, the first line has three integers N,M and K.
Then the following M lines each line has three integers, describe a road, Ui,Vi,Ci. There might be multiple edges between u and v.
It is guaranteed that N≤100000,M≤200000,K≤10,0≤Ci≤1e9. There is at least one path between City 1 and City N.
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
题意
给你一个图,问你从1到N最多让K条边为0的最短路
题解
d[i][j]代表到j点已经删去i条边的最小值
跑一遍dij
代码
#include<bits/stdc++.h>
using namespace std; #define ll long long
const int maxn=1e5+,maxm=2e5+;
ll d[][maxn];
int n,m,k;
int head[maxn],cnt;
struct edge
{
int v,next;
ll w;
}edges[maxm];
struct node
{
ll w;
int v,k;
bool operator<(const node &D)const{
return w>D.w;
}
};
void dij()
{
memset(d,0x3f,sizeof d);
priority_queue<node>q;
q.push({,,});
d[][]=;
while(!q.empty())
{
auto u=q.top();q.pop();
if(u.v==n)return;
for(int i=head[u.v];i!=-;i=edges[i].next)
{
edge &v=edges[i];
if(u.k<k&&d[u.k+][v.v]>d[u.k][u.v])
q.push({d[u.k+][v.v]=d[u.k][u.v],v.v,u.k+});
if(d[u.k][v.v]>d[u.k][u.v]+v.w)
q.push({d[u.k][v.v]=d[u.k][u.v]+v.w,v.v,u.k});
}
}
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
memset(head,-,sizeof head);
cnt=;
scanf("%d%d%d",&n,&m,&k);
for(int i=,u,v,w;i<m;i++)
{
scanf("%d%d%d",&u,&v,&w);
edges[cnt].v=v;
edges[cnt].w=1LL*w;
edges[cnt].next=head[u];
head[u]=cnt++;
}
dij();
ll minn=0x3f3f3f3f3f3f3f3f;
for(int i=;i<=k;i++)
minn=min(minn,d[i][n]);
printf("%lld\n",minn);
}
return ;
}
ACM-ICPC 2018 南京赛区网络预赛 L.Magical Girl Haze(分层最短路)的更多相关文章
- 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
262144K There are NN cities in the country, and MM directional roads from uu to v(1\le u, v\le n)v ...
- 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 (分层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; 在写松弛条件的时候. 如果用 ...
随机推荐
- day24-抽象类与接口类
接口类 1.继承有两种用途:一:继承基类的方法,并且做出自己的改变或者扩展(代码重用) 二:声明某个子类兼容于某基类,定义一个接口类,接口类中定义了一些接口名(就是函数名)且并未实现接口的功能,子类继 ...
- newCachedThreadPool使用案例
newCachedThreadPool 缓存默认60s 猜下你的结果 package com.juc.threadpool; import java.util.concurrent.ExecutorS ...
- hadoop-3
结合https://blog.csdn.net/zhangjun5965/article/details/76796998,自己过一遍感受下 public class DFSZKFailoverCon ...
- js 正则函数初级
1.test :正则匹配字符串,如果成功则返回true,若果失败则返回false 格式:/正则表达式/.test(字符串) 默认吗匹配规则,区分大小写:如果不区分大小写,则加修饰符 i 例子: < ...
- Linux命令:zip
语法: zip [选项] zip文件 源文件s 选项 全称 含义 举例 -r recursive 递归压缩子目录里的文件(包括子目录里的子目录) zip -r target.z ...
- avalon1与avalon2的异同点
avalon2并不向下兼容avalon1,但许多API与指令很相似,迁移成本比较低.此外,良好的性能与强大的功能是你迁移的动力.下面是一个列表,如有提问尽管提出. avalon1与avalon2的异同 ...
- [福大2018高级软工教学]团队Alpha阶段成绩汇总
一.作业地址: https://edu.cnblogs.com/campus/fzu/AdvancedSoftwareEngineerning2018/homework/2396 https://ed ...
- [C语言]使用数组
----------------------------------------------------------------------------- // main.c 例 // Created ...
- 译:SQL Server的Missing index DMV的 bug可能会使你失去理智---慎重看待缺失索引DMV中的信息
注: 本文译自https://www.sqlskills.com/blogs/paul/missing-index-dmvs-bug-that-could-cost-your-sanity/ 原文作者 ...
- ArcGIS特殊标注效果的简单实现
1. 普通纯色背景:例如望仙亭,水垄沟: 方法: 2. 背景+边框 例如进入点 方法: