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【分层图最短路】的更多相关文章

  1. poj3635Full Tank?[分层图最短路]

    Full Tank? Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7248   Accepted: 2338 Descri ...

  2. HDU 5669 线段树优化建图+分层图最短路

    用线段树维护建图,即把用线段树把每个区间都标号了,Tree1中子节点有到达父节点的单向边,Tree2中父节点有到达子节点的单向边. 每次将源插入Tree1,汇插入Tree2,中间用临时节点相连.那么T ...

  3. BZOJ 2763 分层图最短路

    突然发现我不会分层图最短路,写一发. 就是同层中用双向边相连,用单向边连下一层 #include <cstdio> #include <algorithm> #include ...

  4. 【网络流24题】 No.15 汽车加油行驶问题 (分层图最短路i)

    [题意] 问题描述:给定一个 N*N 的方形网格,设其左上角为起点◎, 坐标为( 1, 1), X 轴向右为正, Y轴向下为正, 每个方格边长为 1, 如图所示. 一辆汽车从起点◎出发驶向右下角终点▲ ...

  5. 【网络流24题】 No.14 孤岛营救问题 (分层图最短路)

    [题意] 1944 年,特种兵麦克接到国防部的命令,要求立即赶赴太平洋上的一个孤岛, 营救被敌军俘虏的大兵瑞恩. 瑞恩被关押在一个迷宫里, 迷宫地形复杂, 但幸好麦克得到了迷宫的地形图. 迷宫的外形是 ...

  6. BZOJ_2662_[BeiJing wc2012]冻结_分层图最短路

    BZOJ_2662_[BeiJing wc2012]冻结_分层图最短路 Description “我要成为魔法少女!”     “那么,以灵魂为代价,你希望得到什么?” “我要将有关魔法和奇迹的一切, ...

  7. BZOJ_1579_[Usaco2009 Feb]Revamping Trails 道路升级_分层图最短路

    BZOJ_1579_[Usaco2009 Feb]Revamping Trails 道路升级_分层图最短路 Description 每天,农夫John需要经过一些道路去检查牛棚N里面的牛. 农场上有M ...

  8. Nowcoder contest 370H Rinne Loves Dynamic Graph【分层图最短路】

    <题目链接> 题目大意:Rinne 学到了一个新的奇妙的东西叫做动态图,这里的动态图的定义是边权可以随着操作而变动的图.当我们在这个图上经过一条边的时候,这个图上所有边的边权都会发生变动. ...

  9. ACM-ICPC 2018 南京赛区网络预赛 L 【分层图最短路】

    <题目链接> 题目大意: 有N个城市,这些城市之间有M条有向边,每条边有权值,能够选择K条边 边权置为0,求1到N的最短距离. 解题分析: 分层图最短路模板题,将该图看成 K+1 层图,然 ...

  10. BZOJ2662[BeiJing wc2012]冻结——分层图最短路

    题目描述 “我要成为魔法少女!”     “那么,以灵魂为代价,你希望得到什么?” “我要将有关魔法和奇迹的一切,封印于卡片之中„„”     在这个愿望被实现以后的世界里,人们享受着魔法卡片(Spe ...

随机推荐

  1. SpringBoot之整合MongoDB

    MongoDB官网安装:https://www.mongodb.com/download-center/community MongoDB客户端工具(Mongo Management Studio)安 ...

  2. 面试中很值得聊的二叉树遍历方法——Morris遍历

    Morri遍历 通过利用空闲指针的方式,来节省空间.时间复杂度O(N),额外空间复杂度O(1).普通的非递归和递归方法的额外空间和树的高度有关,递归的过程涉及到系统压栈,非递归需要自己申请栈空间,都具 ...

  3. Element Form表单实践(下)

    作者:小土豆biubiubiu 博客园:https://www.cnblogs.com/HouJiao/ 掘金:https://juejin.im/user/58c61b4361ff4b005d9e8 ...

  4. Linux系统部署JavaWeb项目(超详细tomcat,nginx,mysql)

    转载自:Linux系统部署JavaWeb项目(超详细tomcat,nginx,mysql) 我的系统是阿里云的,香港的系统,本人选择的是系统镜像:CentOS 7.3 64位. 具体步骤: 配置Jav ...

  5. C# 数据操作系列 - 19 FreeSql 入坑介绍

    0. 前言 前几天FreeSql的作者向我推荐了FreeSql框架,想让我帮忙写个文章介绍一下.嗯,想不到我也能带个货了.哈哈,开个玩笑-看了下觉得设计的挺有意思的,所以就谢了这篇文章. 简单介绍一下 ...

  6. [JavaWeb基础] 014.Struts2 标签库学习

    在Struts1和Struts2中都有很多很方便使用的标签库,使用它可以让我们的页面代码更加的简洁,易懂,规范.标签的形式就跟html的标签形式一样.上面的篇章中我们也讲解了自定义标签那么在如何使用标 ...

  7. ms-setting是什么

    ms-settings 遇到了两个问题,记录一下 1)windows桌面右键菜单-->显示设置或者个性化-->报错:ms-settings:personalization-backgrou ...

  8. Rocket - interrupts - NullIntSource

    https://mp.weixin.qq.com/s/Fn3u2OSLAzPDrlZTiLfikg 简单介绍NullIntSource的实现. 1. 简单介绍 NullIntSource实现一个不会发 ...

  9. Rocket - util - ReduceOthers

    https://mp.weixin.qq.com/s/gbR5fuDbE_nUFVxw-p4rsA   简单介绍ReduceOthers的实现.   ​​   1. 基本介绍   输入一组Bool元素 ...

  10. Shell脚本 (三) 条件判断 与 流程控制

    个人博客网:https://wushaopei.github.io/    (你想要这里多有) 六.条件判断 1.基本语法 [ condition ](注意condition 前后要有空格) 注意:条 ...