题意:

有向图,可以把k条路的长度变为0,求1到n的最短路

思路:

将图复制k份,一共k+1层图,对于每一条i→j,都连一条低层的i→高层的j,并且权值为0

即对每一对<i,j,w>,都加边<i,j,w>,<i+n,j+n,w>,<i+2n,j+2n,w>,....,<i+kn,j+kn,w>

同时加“楼梯”<i,j+n,0>,<i+n,j+2n,0>,...,<i+(k-1)n, j+kn>

然后跑一个1~(k+1)n的最短路,用迪杰斯特拉+堆优化

代码:

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<string>
#include<stack>
#include<queue>
#include<deque>
#include<set>
#include<vector>
#include<map>
#include<functional> #define fst first
#define sc second
#define pb push_back
#define mem(a,b) memset(a,b,sizeof(a))
#define lson l,mid,root<<1
#define rson mid+1,r,root<<1|1
#define lc root<<1
#define rc root<<1|1
#define lowbit(x) ((x)&(-x)) using namespace std; typedef double db;
typedef long double ldb;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> PI;
typedef pair<ll,ll> PLL; const db eps = 1e-;
const int mod = 1e9+;
const int maxn = 2e6+;
const int maxm = 2e6+;
const int inf = 0x3f3f3f3f;
const db pi = acos(-1.0); int dist[maxn];
struct node{
int id, d;
node(){}
node(int a,int b) {id = a; d = b;}
bool operator < (const node & a)const{
if(d == a.d) return id > a.id;
else return d > a.d;
}
};
vector<node>e[maxn];
int n; void dijkstra(int s){
for(int i = ; i <= n; i++) dist[i] = inf;
dist[s] = ;
priority_queue<node>q;
q.push(node(s, dist[s]));
while(!q.empty()){
node top = q.top();
q.pop();
if(top.d != dist[top.id]) continue;
for(int i = ; i < (int)e[top.id].size(); i++){
node x = e[top.id][i];
if(dist[x.id] > top.d + x.d){
dist[x.id] = top.d + x.d;
q.push(node(x.id, dist[x.id]));
}
}
}
return;
} int main() {
int T;
scanf("%d", &T);
while(T--){
int m, k;
scanf("%d %d %d", &n, &m, &k);
while(m--){
int x ,y, w;
scanf("%d %d %d", &x, &y, &w);
for(int i = ; i <= k; i++){
e[x + i*n].pb(node(y + i*n, w));
if(i)e[x + (i-)*n].pb(node(y + i*n, )); }
}
n += k*n;
dijkstra();
printf("%d\n", dist[n]);
for(int i = ; i <= n; i++)e[i].clear();
}
return ;
}

2018icpc南京网络赛-L Magical Girl Haze (分层图最短路)的更多相关文章

  1. 2018 ICPC南京网络赛 L Magical Girl Haze 题解

    大致题意: 给定一个n个点m条边的图,在可以把路径上至多k条边的权值变为0的情况下,求S到T的最短路. 数据规模: N≤100000,M≤200000,K≤10 建一个立体的图,有k层,每一层是一份原 ...

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

  3. ICPC 2018 南京网络赛 J Magical Girl Haze(多层图最短路)

    传送门:https://nanti.jisuanke.com/t/A1958 题意:n个点m条边的路,你有k次机会将某条路上的边权变为0,问你最短路径长度 题解:最短路变形,我们需要在常规的最短路上多 ...

  4. ACM-ICPC 2018 南京赛区网络预赛 - L Magical Girl Haze (分层迪杰斯特拉)

    题意:N个点,M条带权有向边,求可以免费K条边权值的情况下,从点1到点N的最短路. 分析:K<=10,用dist[i][j]表示从源点出发到点i,免费j条边的最小花费.在迪杰斯特拉的dfs过程中 ...

  5. 2018ICPC南京网络赛

    2018ICPC南京网络赛 A. An Olympian Math Problem 题目描述:求\(\sum_{i=1}^{n} i\times i! \%n\) solution \[(n-1) \ ...

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

  7. 计蒜客 31001 - Magical Girl Haze - [最短路][2018ICPC南京网络预赛L题]

    题目链接:https://nanti.jisuanke.com/t/31001 题意: 一带权有向图,有 n 个节点编号1~n,m条有向边,现在一人从节点 1 出发,他有最多 k 次机会施展魔法使得某 ...

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

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

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

随机推荐

  1. Spring boot项目搭建及简单实例

    Spring boot项目搭建 Spring Boot 概述 Build Anything with Spring Boot:Spring Boot is the starting point for ...

  2. SpringCloud入门系列0-Nacos的安装与配置

    背景 工作有一些年头了,自从19年初彻底转了java(这又是另一篇心酸的故事),突然感觉自己荒废了好几年(不是说.net不好,而是回顾自己这几年做的很多东西都浮于表面,有时候弄成很忙的样子,回头看看自 ...

  3. 2019 年百度之星 初赛一 1002 Game

    传送门 Problem Description 度度熊在玩一个好玩的游戏.游戏的主人公站在一根数轴上,他可以在数轴上任意移动,对于每次移动,他可以选择往左或往右走一格或两格.现在他要依次完成 n 个任 ...

  4. eclipse maven工程错误总汇

    1.问题: Target runtime Apache Tomcat v7.0 is not defined 解决方法:           right click on your project & ...

  5. JAVA封装、继承、多态

    封装 1.概念: 将类的某些信息隐藏在类的内部,不允许外部程序访问,而是通过该类提供的方法来实现对隐藏信息的操作和访问. 2.好处: a.只能通过规定的方法访问数据 b.隐藏类的实例细节,方便修改和实 ...

  6. css3实现左右div高度自适应且内容居中对齐

    主要运用了css3的弹层布局,直接上代码: 效果:左边盒子宽度固定.内容居中对齐.与右侧盒子高度相等,右侧自动缩放 html: <div class="main"> & ...

  7. Bonny手机APP试用体验

    在上周四(即6月13日)下午,应王建民老师的邀请,我参观了学长学姐们的软件设计评比以及专业交流的活动,看到了形形色色学长学姐设计出的软件我觉得非常有趣,并对学长学姐们设计的软件的种类与功能感到由衷的钦 ...

  8. 在IIS上发布netcore项目

    保证电脑上有.net core sdk或者.net core runtime; 需要安装AspNetCoreModule托管模块:DotNetCore.2.0.5-WindowsHosting.exe ...

  9. 奇葩的Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.

    启动springboot的时候莫名其妙出现这个错误,我properties里面也没配置数据源啥的,但就是出现这个错误 解决方法: 在启动类上加@SpringBootApplication(exclud ...

  10. 【Spring Boot 源码解读】之 【为何引入了 Jedis 依赖最后用的还是 Lettuce 客户端?】

    1.Spring Boot 2.x 的两种 Redis 客户端 首先,我们都知道,从 Spring Boot 2.x 开始 Lettuce 已取代 Jedis 成为首选 Redis 的客户端.当然 S ...