free【分层图最短路】
free
传送门 来源: 牛客网
题目描述
Your are given an undirect connected graph.Every edge has a cost to pass.You should choose a path from S to T and you need to pay for all the edges in your path. However, you can choose at most k edges in the graph and change their costs to zero in the beginning. Please answer the minimal total cost you need to pay.
输入描述:
The first line contains five integers n,m,S,T,K.
For each of the following m lines, there are three integers a,b,l, meaning there is an edge that costs l between a and b.
n is the number of nodes and m is the number of edges.
输出描述:
An integer meaning the minimal total cost.
输入
3 2 1 3 1
1 2 1
2 3 2
输出
1
备注:
1≤n,m≤103,1≤S,T,a,b≤n,0≤k≤m,1≤l≤106.
Multiple edges and self loops are allowed.
题目描述:
给出n个点,和m条带权边,并且可以选定几条边令其权值为0,但选择的边数最多是k条,求s和t两点的最短距离。
思路:
有k次机会使边的权值为0,是分层最短路的经典问题。
具体方法看下图:(图中序号是从0开始的,下面讲解用从1开始的)

图源:sugarbliss
根据上图可以知道当k=2时,需要建k+1层的图,其中第一层序号是[1,n],往下依次是[1+n,n+n]、[1+2n+n+2*n]
在使用链式前向星存图的时候,要同时存下一列的权值,并将上下两层的权值设为0。
最终最短路的长度出现在每一行的最后,即:n、2*n、3*n。
例如(1,5)=10 要存(1+5,5+5)=10、(1+2*5,5+2*5)=1
(1,5+5+1)=0 等。
(看不懂直接看代码很好理解,找了一下午才找了个感觉好适应的板子)
AC代码:
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int MAX=5e6;
const int INF=0x3f3f3f3f;
LL n,m,s,t,k;
LL head[MAX+5],ans;
LL dis[MAX+5],vis[MAX+5];
struct note{
LL to;
LL len;
LL next;
}edge[MAX+5];
void addedge(LL u,LL v,LL w)
{
edge[ans].to=v;
edge[ans].len=w;
edge[ans].next=head[u];
head[u]=ans++;
}
void init()
{
memset(head,-1,sizeof(head));
ans=0;
}
struct node{
LL u,len;
node(LL a,LL b){
u=b;
len=a;
}
friend bool operator < (node a,node b){
return a.len>b.len;
}
};
priority_queue<node>q;
void diji(LL s)
{
for(LL i=0;i<=(k+1)*n;i++){
dis[i]=INF;
vis[i]=0;
}
dis[s]=0;
q.push(node(0,s));
while(!q.empty()){
int k=q.top().u;
q.pop();
if(vis[k]){
continue;
}
vis[k]=1;
for(LL i=head[k];~i;i=edge[i].next){
int t=edge[i].to;
if((dis[t]>dis[k]+edge[i].len&&edge[i].len!=INF+2)){
dis[t]=dis[k]+edge[i].len;
q.push(node(dis[t],t));
}
}
}
}
int main()
{
scanf("%lld%lld%lld%lld%lld", &n, &m, &s,&t,&k);
init();
while(m--)
{
LL u, v, w;
scanf("%lld%lld%lld",&u, &v, &w);
for(LL i = 0; i <= k; i++)
{
addedge(u + i * n, v + i * n, w);
addedge(v + i * n, u + i * n, w);
if(i != k)
{
addedge(u + i * n, v + (i + 1) * n, 0);
addedge(v + i * n, u + (i + 1) * n, 0);
}
}
}
diji(s);
LL ans = INF;
for(LL i = 0; i <= k; i++)
ans = min(ans,dis[t + i * n]);
printf("%lld\n",ans);
}
free【分层图最短路】的更多相关文章
- poj3635Full Tank?[分层图最短路]
Full Tank? Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7248 Accepted: 2338 Descri ...
- HDU 5669 线段树优化建图+分层图最短路
用线段树维护建图,即把用线段树把每个区间都标号了,Tree1中子节点有到达父节点的单向边,Tree2中父节点有到达子节点的单向边. 每次将源插入Tree1,汇插入Tree2,中间用临时节点相连.那么T ...
- BZOJ 2763 分层图最短路
突然发现我不会分层图最短路,写一发. 就是同层中用双向边相连,用单向边连下一层 #include <cstdio> #include <algorithm> #include ...
- 【网络流24题】 No.15 汽车加油行驶问题 (分层图最短路i)
[题意] 问题描述:给定一个 N*N 的方形网格,设其左上角为起点◎, 坐标为( 1, 1), X 轴向右为正, Y轴向下为正, 每个方格边长为 1, 如图所示. 一辆汽车从起点◎出发驶向右下角终点▲ ...
- 【网络流24题】 No.14 孤岛营救问题 (分层图最短路)
[题意] 1944 年,特种兵麦克接到国防部的命令,要求立即赶赴太平洋上的一个孤岛, 营救被敌军俘虏的大兵瑞恩. 瑞恩被关押在一个迷宫里, 迷宫地形复杂, 但幸好麦克得到了迷宫的地形图. 迷宫的外形是 ...
- BZOJ_2662_[BeiJing wc2012]冻结_分层图最短路
BZOJ_2662_[BeiJing wc2012]冻结_分层图最短路 Description “我要成为魔法少女!” “那么,以灵魂为代价,你希望得到什么?” “我要将有关魔法和奇迹的一切, ...
- BZOJ_1579_[Usaco2009 Feb]Revamping Trails 道路升级_分层图最短路
BZOJ_1579_[Usaco2009 Feb]Revamping Trails 道路升级_分层图最短路 Description 每天,农夫John需要经过一些道路去检查牛棚N里面的牛. 农场上有M ...
- Nowcoder contest 370H Rinne Loves Dynamic Graph【分层图最短路】
<题目链接> 题目大意:Rinne 学到了一个新的奇妙的东西叫做动态图,这里的动态图的定义是边权可以随着操作而变动的图.当我们在这个图上经过一条边的时候,这个图上所有边的边权都会发生变动. ...
- ACM-ICPC 2018 南京赛区网络预赛 L 【分层图最短路】
<题目链接> 题目大意: 有N个城市,这些城市之间有M条有向边,每条边有权值,能够选择K条边 边权置为0,求1到N的最短距离. 解题分析: 分层图最短路模板题,将该图看成 K+1 层图,然 ...
- BZOJ2662[BeiJing wc2012]冻结——分层图最短路
题目描述 “我要成为魔法少女!” “那么,以灵魂为代价,你希望得到什么?” “我要将有关魔法和奇迹的一切,封印于卡片之中„„” 在这个愿望被实现以后的世界里,人们享受着魔法卡片(Spe ...
随机推荐
- STM32F103出现CPU could not be halted问题的解决方案
问题描述: **JLink Warning: CPU could not be halted ***JLink Error: Can not read register 15 (R15) while ...
- POJ-2488 国际象棋马的走法 (深度优先搜索和回溯)
#include <stdio.h> #define MAX 27 void dfs(int i, int j); int dx[8] = {-1, 1, -2, 2, -2, 2, -1 ...
- Kubernetes学习笔记(四):服务
服务介绍 服务是一种为一组相同功能的pod提供单一不变接入点的资源.当服务存在时,他的IP和端口不会改变.客户端通过IP和端口建立连接,这些连接会被路由到任何一个pod上.如此,客户端不需要知道每个单 ...
- eclipse——Error exists in required project Proceed with launch?
运行java文件时报错: Error exists in required project Proceed with launch? 报错截图: 问题参生原因:开始Buildpath了一个jar ...
- MyBatis—— org.apache.ibatis.binding.BindingException: Type interface com.web.mybatis.i.PersonMapper is not known to the MapperRegistry.
报错信息: Exception in thread "main" org.apache.ibatis.binding.BindingException: Type interfac ...
- PHP cookie基本操作
PHP中Cookie的使用---添加/更新/删除/获取Cookie 及 自动填写该用户的用户名和密码和判断是否第一次登陆 什么是cookie 服务器在客户端保存用户的信息,比如登录名,密码等 这些数据 ...
- PHP文件上传案例和函数
$_FILES参数详解: $_FILES["file"]["name"] – 被上传文件的名称 $_FILES["file"][" ...
- 安卓全屏或沉浸式状态栏下输入框(EditText)被键盘遮挡解决方法
沉浸式状态栏用了一段时间了,一直没发现安卓在这方面的坑.最近在集成环信自定义UI的过程中,发现将环信界面设置为沉浸式之后最底部的消息输入框不随键盘弹起而变化了,一直显示在屏幕最下方,体验非常差. 后来 ...
- Elasticsearch系列---生产集群部署(上)
概要 本篇开始介绍Elasticsearch生产集群的搭建及相关参数的配置. ES集群的硬件特性 我们从开始编程就接触过各种各样的组件,而每种功能的组件,对硬件要求的特性都不太相同,有的需要很强的CP ...
- 基于nodejs+express+mysql+webstorm+html的 增删改查
一.工具准备 Nodejs框架,WebStorm.Mysql服务.Navicat.此篇文章只讲项目的搭建过程,至于Nodejs,WebStorm.Mysql的下载.安装与配置网上资源很多,请自行查阅, ...