前言

看到好多大佬都在跑分层图最短路,\(\text{DP}\) 解法的我瑟瑟发抖。。。

题目描述

给定一张 \(N\) 个点(点编号从 \(0\) 到 \(N-1\)),\(M\) 条边的无向带权图 \(G\)。给定常数 \(k\),你可以在图 \(G\) 中使不超过 \(k\) 条边的边权为 \(0\),求在该条件下点 \(s\) 到点 \(t\) 的最短路。

数据范围:\(2\le N\le10000,1\le M\le50000,0\le k\le10\)

基本思路

考虑 \(\text{DP}\)

我们设 \(dis[i][u]\) 表示从点 \(s\) 出发对 \(i\) 条边的边权清零时的最短路,答案就是 \(dis[k][t]\)

考虑转移:

首先跑一遍裸的最短路,求出 \(dis[0][u]\ u \in[0,n-1]\)

然后对于 \(\forall\ i \in [1,k]\)

\[dis[i][u]=min\{dis[i-1][v],dis[i][v]+(v,u)\}
\]

其中 \(u\) 是 \(v\) 的后继。

然后就可以开始转移了。

注意事项

  • 点编号从 \(0\) 到 \(N-1\)
  • 无向图边的空间开两倍

参考代码

/*--------------------------------
Author: The Ace Bee
Blog: www.cnblogs.com/zsbzsb
This code is made by The Ace Bee
--------------------------------*/
#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <cctype>
#include <cmath>
#include <ctime>
#include <queue>
#define rg register
#define mp make_pair
#define pii pair < int, int >
using namespace std;
template < typename T > inline void read(T& s) {
s = 0; int f = 0; char c = getchar();
while (!isdigit(c)) f |= (c == '-'), c = getchar();
while (isdigit(c)) s = s * 10 + (c ^ 48), c = getchar();
s = f ? -s : s;
}
const int _ = 10010;
const int __ = 100010;
int n, m, k, s, t, dis[11][_], exi[_];
int tot, head[_], nxt[__], ver[__], w[__];
inline void Add_edge(int u, int v, int d)
{ nxt[++tot] = head[u], head[u] = tot, ver[tot] = v, w[tot] = d; }
queue < int > q1, q2;
inline void spfa1(int x) {
memset(dis[x], 0x3f, sizeof dis[x]);
dis[x][s] = 0, exi[s] = 1;
q1.push(s), q2.push(s);
while (!q1.empty()) {
int u = q1.front(); q1.pop(), exi[u] = 0;
for (rg int i = head[u]; i; i = nxt[i]) {
int v = ver[i];
if (dis[x][v] > dis[x - 1][u]) {
dis[x][v] = dis[x - 1][u];
if (!exi[v])
exi[v] = 1, q1.push(v), q2.push(v);
}
}
}
}
inline void spfa2(int x) {
if (x == 0)
memset(dis[x], 0x3f, sizeof dis[x]), q2.push(s);
dis[x][s] = 0, exi[s] = 1;
while (!q2.empty()) {
int u = q2.front(); q2.pop(), exi[u] = 0;
for (rg int i = head[u]; i; i = nxt[i]) {
int v = ver[i];
if (dis[x][v] > dis[x][u] + w[i]) {
dis[x][v] = dis[x][u] + w[i];
if (!exi[v]) exi[v] = 1, q2.push(v);
}
}
}
}
int main() {
#ifndef ONLINE_JUDGE
freopen("in.in", "r", stdin);
#endif
read(n), read(m), read(k), read(s), read(t);
for (rg int u, v, d, i = 1; i <= m; ++i)
read(u), read(v), read(d), Add_edge(u, v, d), Add_edge(v, u, d);
spfa2(0);
// for (rg int u = 0; u < n; ++u)
// printf("%d%c", dis[0][u], " \n"[u == n - 1]);
for (rg int i = 1; i <= k; ++i) {
spfa1(i);
// for (rg int u = 0; u < n; ++u)
// printf("%d%c", dis[i][u], " \n"[u == n - 1]);
spfa2(i);
// for (rg int u = 0; u < n; ++u)
// printf("%d%c", dis[i][u], " \n"[u == n - 1]);
}
printf("%d\n", dis[k][t]);
return 0;
}

完结撒花 \(QwQ\)

「JLOI2011」飞行路线的更多相关文章

  1. 「JLOI2011」「LuoguP4568」飞行路线(分层图最短路

    题目描述 Alice和Bob现在要乘飞机旅行,他们选择了一家相对便宜的航空公司.该航空公司一共在nn个城市设有业务,设这些城市分别标记为00到n-1n−1,一共有mm种航线,每种航线连接两个城市,并且 ...

  2. 「译」JUnit 5 系列:条件测试

    原文地址:http://blog.codefx.org/libraries/junit-5-conditions/ 原文日期:08, May, 2016 译文首发:Linesh 的博客:「译」JUni ...

  3. 「译」JUnit 5 系列:扩展模型(Extension Model)

    原文地址:http://blog.codefx.org/design/architecture/junit-5-extension-model/ 原文日期:11, Apr, 2016 译文首发:Lin ...

  4. JavaScript OOP 之「创建对象」

    工厂模式 工厂模式是软件工程领域一种广为人知的设计模式,这种模式抽象了创建具体对象的过程.工厂模式虽然解决了创建多个相似对象的问题,但却没有解决对象识别的问题. function createPers ...

  5. 「C++」理解智能指针

    维基百科上面对于「智能指针」是这样描述的: 智能指针(英语:Smart pointer)是一种抽象的数据类型.在程序设计中,它通常是经由类型模板(class template)来实做,借由模板(tem ...

  6. 「JavaScript」四种跨域方式详解

    超详细并且带 Demo 的 JavaScript 跨域指南来了! 本文基于你了解 JavaScript 的同源策略,并且了解使用跨域跨域的理由. 1. JSONP 首先要介绍的跨域方法必然是 JSON ...

  7. 「2014-5-31」Z-Stack - Modification of Zigbee Device Object for better network access management

    写一份赏心悦目的工程文档,是很困难的事情.若想写得完善,不仅得用对工具(use the right tools),注重文笔,还得投入大把时间,真心是一件难度颇高的事情.但,若是真写好了,也是善莫大焉: ...

  8. 「2014-3-18」multi-pattern string match using aho-corasick

    我是擅(倾)长(向)把一篇文章写成杂文的.毕竟,写博客记录生活点滴,比不得发 paper,要求字斟句酌八股结构到位:风格偏杂文一点,也是没人拒稿的.这么说来,arxiv 就好比是 paper 世界的博 ...

  9. 「2014-3-17」C pointer again …

    记录一个比较基础的东东-- C 语言的指针,一直让人又爱又恨,爱它的人觉得它既灵活又强大,恨它的人觉得它太过于灵活太过于强大以至于容易将人绕晕.最早接触 C 语言,还是在刚进入大学的时候,算起来有好些 ...

随机推荐

  1. opencv:图像直方图均衡化

    // 直方图均衡化 Mat gray, dst; cvtColor(src, gray, COLOR_BGR2GRAY); equalizeHist(gray, dst); imshow(" ...

  2. 读书小记--<态度>

    前言 前段时间再读了吴军老师的<态度>,分享的是和女儿的日常书信.觉得收获很多,同时推荐他的<浪潮之巅><数学之美><大学之路>系列书籍. 下面是个人的 ...

  3. Educational Codeforces Round 81 (Rated for Div. 2) - D. Same GCDs(数学)

    题目链接:Same GCDs 题意:给你两个数$a$,$m(1 \leq a < m \leq 10^{10})$,求有多少个$x$满足:$0 \leq x < m$且$gcd(a,m)= ...

  4. PHP 站内消息的表结构设计

    1)添加全站通知:信息存入到 tb_message 2)用户点开信息或者设置信息为已阅读:插入记录到 tb_message_readlog 如何设计存储的表?求最佳方案 CREATE TABLE `m ...

  5. touchstart和click 自动区分

    var clickEvent = (function() { if ('ontouchstart' in document.documentElement === true) return 'touc ...

  6. HTML中的meta元素

    <meta>元素必须放在<head>标记内,而且必须写在HTML文件前1024B之内 <meta>元素的主要目的是提供有关这份HTML文件的相关信息.例如编码方式, ...

  7. 初学做uniapp项目过程梳理的一些记录

    1.uniapp不显示h5头部 第一种写法: { "path" : "pages/yunshi/yunshi", "style" : { & ...

  8. 【快学SpringBoot】Spring Cache+Redis实现高可用缓存解决方案

    前言 之前已经写过一篇文章介绍SpringBoot整合Spring Cache,SpringBoot默认使用的是ConcurrentMapCacheManager,在实际项目中,我们需要一个高可用的. ...

  9. Java 11 New Features

    前言 北京时间 2018年9 月 26 日,Oracle 官方宣布 Java 11 正式发布.这是 Java 大版本周期变化后的第一个长期支持版本,非常值得关注.从官网即可下载, 最新发布的 Java ...

  10. SprintBoot学习(二)

    Spring Boot的入口类 1.名为xxxApplication的就是入口类,在main方法中使用SpringApplication.run(SpringBootTestApplication.c ...