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; 在写松弛条件的时候. 如果用 ...
随机推荐
- 使用python实现人脸检测<转载>
原文地址:https://www.cnblogs.com/vipstone/p/8884991.html =============================================== ...
- 2 算法查找&排序问题
一.查找 1.查找的概念: 2 顺序查找(linear search) 从头找到尾 def linear_search(li,val): for ind ,v in enumerate(li): if ...
- 建立一个php 基础类
在些PHP文件的时候,一般首先都是要先写一下基础类: 主要包括以下几个方面: 1.服务器的链接:包括主机,用户名,密码 2.数据库的选择:要操作哪个数据库 3.字符集的设置:设置什么样的编码 4.查询 ...
- 使用exec函数将当前的信息输入到文件中
先来看看exec函数: exec函数族 fork创建子进程后执行的是和父进程相同的程序(但有可能执行不同的代码分支),子进程往往要调用一种exec函数以执行另一个程序.当进程调用一种exec函 ...
- 在Linux中简单实现回收子进程
学习到wait函数了,这个函数的作用是用来回收进程.一般来说,正常退出的进程是不需要我们来专门回收的.但是进程有这两种:孤儿进程和僵尸进程. 孤儿进程: 通俗点说就是父进程先于子进程死亡.此时子进程就 ...
- English: How to Pronounce R [ɹ] Consonant
English: How to Pronounce R [ɹ] Consonant Share Tweet Share Tagged With: Most Popular, Sound How-To ...
- python学习笔记之斐波拉契数列学习
著名的斐波拉契数列(Fibonacci),除第一个和第二个数外,任意一个数都可由前两个数相加得到: 1, 1, 2, 3, 5, 8, 13, 21, 34, ... 如果用Python的列表生成式, ...
- unity 2048Game
将游戏分为四个脚本,将数据和界面分开,这是开发模式常用的类似于mvc模式,但由于我们只用一个二位数组就可以保存数据,所以讲m省略 GameControllor 控制游戏数据的脚本, using Uni ...
- idea git 从github上拉取项目 更改上传
更改上传: 新增文件上传时注意:
- 吴裕雄 10-MySQL插入数据
语法以下为向MySQL数据表插入数据通用的 INSERT INTO SQL语法:INSERT INTO table_name ( field1, field2,...fieldN ) VALUES ( ...