https://nanti.jisuanke.com/t/31001

有K次机会可以让一条边的权值变为0,求最短路。

在存储单源最短路的数组上多开一维状态,d[i][k]表示走到序号i的点,且让k条边权值为0时的最短路。

对于每个待更新的点,尝试不置零此边的状态和置零此边的状态,分别压入优先队列去更新其他状态。

另外,此题由于有重边,需要先去重,保留同起始点最短的边。

代码:

#include <iostream>
#include <algorithm>
#include <queue>
#include <cstring>
#include <vector>
#include <cstdio>
using namespace std;
typedef long long ll;
const ll INF = 0x3f3f3f3f3f3f3f3fLL;
const int MAX_V = 100005;
const int MAX_E = 200005;
int N, M, K;
struct Dijkstra {
struct Edge {
int to, cost, next;
} es[MAX_E];
struct Node {
int u, k;
ll d;
Node(int u, ll d, int k) : u(u), d(d), k(k) {}
bool operator< (const Node& n) const {
return d > n.d;
}
};
int head[MAX_V];
int V, E;
ll d[MAX_V][15];
bool vis[MAX_V][15];
void init(int V) {
this->V = V;
this->E = 0;
memset(head, -1, sizeof head);
}
void addEdge(int u, int v, int w) {
es[E].to = v;
es[E].cost = w;
es[E].next = head[u];
head[u] = E++;
}
void dijkstra(int s) {
priority_queue <Node> Q;
memset(d, 0x3f, sizeof d);
memset(vis, 0, sizeof(vis));
d[s][0] = 0;
Q.push(Node(s, 0, 0));
while (!Q.empty()) {
int u = Q.top().u, k = Q.top().k;
Q.pop();
if (vis[u][k])
continue;
vis[u][k] = true;
for (int i = head[u]; i != -1; i = es[i].next) {
int v = es[i].to, w = es[i].cost;
if (d[v][k] > d[u][k] + w) {
d[v][k] = d[u][k] + w;
Q.push(Node(v, d[v][k], k));
}
if (k + 1 <= K) {
if (d[v][k + 1] > d[u][k]) {
d[v][k + 1] = d[u][k];
Q.push(Node(v, d[v][k + 1], k + 1));
}
}
}
}
}
} dijk;
struct Elem {
int u, v, w;
bool operator< (const Elem& e) const {
if (u == e.u && v == e.v) {
return w < e.w;
}
if (u == e.u) {
return v < e.v;
}
return u < e.u;
}
} e[MAX_E];
int main() {
int T;
scanf("%d", &T);
while (T--) {
scanf("%d%d%d", &N, &M, &K);
dijk.init(N);
for (int i = 0; i < M; i++) {
scanf("%d%d%d", &e[i].u, &e[i].v, &e[i].w);
}
sort(e, e + M);
int preu = 0, prev = 0;
for (int i = 0; i < M; i++) {
if (preu != e[i].u || prev != e[i].v) {
dijk.addEdge(e[i].u, e[i].v, e[i].w);
preu = e[i].u, prev = e[i].v;
}
}
dijk.dijkstra(1);
ll ans = INF;
for (int i = 0; i <= K; i++) {
ans = min(ans, dijk.d[N][i]);
}
printf("%lld\n", ans);
}
}

【分层最短路】Magical Girl Haze的更多相关文章

  1. ROADS POJ - 1724(分层最短路)

    就是在最短路的基础上   多加了一个时间的限制 , 多一个限制多一维就好了  记住 分层最短路要用dijistra !!! #include <iostream> #include < ...

  2. 拯救大兵瑞恩 HDU - 4845(状压bfs || 分层最短路)

    1.状压bfs 这个状压体现在key上  我i们用把key状压一下  就能记录到一个点时 已经拥有的key的种类 ban[x1][y1][x2][y1]记录两个点之间的状态 是门 还是墙 还是啥都没有 ...

  3. ACM-ICPC 2018 南京赛区网络预赛 L && BZOJ 2763 分层最短路

    https://nanti.jisuanke.com/t/31001 题意 可以把k条边的权值变为0,求s到t的最短路 解析  分层最短路  我们建立k+1层图 层与层之间边权为0,i 向 i+1层转 ...

  4. 分层最短路(牛客第四场)-- free

    题意: 给你边权,起点和终点,有k次机会把某条路变为0,问你最短路是多长. 思路: 分层最短路模板题.题目有点坑(卡掉了SPFA,只能用dijkstra跑的算法). #include<iostr ...

  5. 牛客练习赛47 D DongDong坐飞机 (分层最短路)

    链接:https://ac.nowcoder.com/acm/contest/904/D 来源:牛客网 DongDong坐飞机 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 5242 ...

  6. 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 di ...

  7. 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, ...

  8. 2018icpc南京网络赛-L Magical Girl Haze (分层图最短路)

    题意: 有向图,可以把k条路的长度变为0,求1到n的最短路 思路: 将图复制k份,一共k+1层图,对于每一条i→j,都连一条低层的i→高层的j,并且权值为0 即对每一对<i,j,w>,都加 ...

  9. 2018南京网络赛L题:Magical Girl Haze(最短路分层图)

    题目链接:https://nanti.jisuanke.com/t/31001 解题心得: 一个BZOJ的原题,之前就写过博客了. 原题地址:https://www.lydsy.com/JudgeOn ...

随机推荐

  1. nginx详解反向代理、负载均衡、LNMP架构上线动态网站

    简介 Nginx是俄罗斯人编写的十分轻量级的HTTP服务器,Nginx,它的发音为“engine X”,是一个高性能的HTTP和反向代理服务器,同时也是一个IMAP/POP3/SMTP 代理服务器.N ...

  2. com.alibaba.druid检测排查数据库连接数不释放定位代码

    1.可能标题说的很不明白,其实就是这样一个情况,一个工程项目错误日志出现GetConnectionTimeoutException: wait millis 90000, active 22000的异 ...

  3. PHP Ajax跨域问题解决办法

    在项目开发中,经常会遇到跨域访问资源,上传图片等,那么这些都怎么解决呢,下面简单介绍一下ajax请求时,解决跨域问题. 原文地址:小时刻个人博客 > http://small.aiweimeng ...

  4. 每日Linux命令(2)-cal

    cal命令用来显示公历,公历是现在国际通用的历法. 一.格式 cal [选项] [参数] 二.功能 显示当前日历年月日,也可以指定显示某年全年日历及时间. 三.命令选项 -h 关闭今天显示的高亮 -j ...

  5. django的Cookie-9

    设置Cookie 可以通过HttpResponse对象中的set_cookie方法来设置cookie. HttpResponse.set_cookie(cookie名字, value=cookie值, ...

  6. 使用cursor递归遍历sqlserver的相应表

    use rc GO )DECLARE cur1 cursor for select [name] from sys.tables where name LIKE 'index_%' drop tabl ...

  7. Android Sdk Manager更新

    现在Android Sdk Manager无法更新了,什么原因大家都知道,即使使用Goagent效果也不理想. 目前Goagent使用的3.2.2 修改C:\Windows\System32\driv ...

  8. Mysql:存储过程游标不进循环的原因详解

    ------------吾亦无他,唯手熟尔,谦卑若愚,好学若饥------------- 本篇博客给刚接触存储过程的朋友做个引导作用,目的是解决游标不走循环 很多人发现他的游标,无论是嵌套循环还是单层 ...

  9. 使用Python的BeautifulSoup 类库采集网页内容

    BeautifulSoup 一个分析.处理DOM树的类库.可以做网络爬虫.模块简称bs4. 安装类库 easy_install beautifulsoup4 pip install beautiful ...

  10. C#实现仪器的自动化控制

    1.概述 生产测试当中,测试仪器不可或缺,如果是小规模生产,手动测试可以对付:但是要想到达大批量生产的目的,为了简化测试,节约时间,就需要进行自动化测试.出于这样的需求,对仪器的自动化程控就有了需求. ...