【洛谷 P4568】 [JLOI2011]飞行路线 (分层最短路)
分层图最短路。
把每个点拆成\(k+1\)个点,表示总共有\(k+1\)层。
然后每层正常连边,
若\((u,v)\)有边,则把每一层的\(u\)和下一层的\(v\)、每一层的\(v\)和下一层的\(u\)连边。
然后跑最短路就行了,终点是最后一层的\(t\)。
#include <cstdio>
#include <queue>
#include <algorithm>
using namespace std;
int xjc; char ch;
inline int read(){
xjc = 0; ch = getchar();
while(ch < '0' || ch > '9') ch = getchar();
while(ch >= '0' && ch <= '9'){ xjc = xjc * 10 + ch - '0'; ch = getchar(); }
return xjc;
}
const int MAXN = 100010;
const int MAXM = 500010;
struct Edge{
int next, to, dis;
}e[MAXM << 2];
int head[MAXN], num, dis[MAXN];
inline void Add(int from, int to, int dis){
e[++num] = (Edge){ head[from], to, dis }; head[from] = num;
}
typedef pair<int, int> point;
priority_queue <point, vector<point>, greater<point> > q;
point now;
int n, m, k, s, t, a, b, c;
int main(){
n = read(); m = read(); k = read(); s = read(); t = read();
for(int i = 1; i <= m; ++i){
a = read(); b = read(); c = read();
Add(a, b, c); Add(b, a, c);
for(int j = 1; j <= k; ++j){ //分层
Add(a + (j - 1) * n, b + j * n, 0);
Add(b + (j - 1) * n, a + j * n, 0);
Add(a + j * n, b + j * n, c);
Add(b + j * n, a + j * n, c);
}
}
q.push(point(0, s));
memset(dis, 127, sizeof dis);
dis[s] = 0;
while(q.size()){
now = q.top(); q.pop();
if(dis[now.second] < now.first) continue;
for(int i = head[now.second]; i; i = e[i].next)
if(dis[e[i].to] > dis[now.second] + e[i].dis){
dis[e[i].to] = dis[now.second] + e[i].dis;
q.push(point(dis[e[i].to], e[i].to));
}
}
printf("%d\n", dis[t + k * n]);
return 0;
}
【洛谷 P4568】 [JLOI2011]飞行路线 (分层最短路)的更多相关文章
- 洛谷 P4568 [JLOI2011]飞行路线 解题报告
P4568 [JLOI2011]飞行路线 题目描述 Alice和Bob现在要乘飞机旅行,他们选择了一家相对便宜的航空公司.该航空公司一共在\(n\)个城市设有业务,设这些城市分别标记为0到\(n−1\ ...
- 洛谷 P4568 [JLOI2011]飞行路线 题解
P4568 [JLOI2011]飞行路线 题目描述 Alice和Bob现在要乘飞机旅行,他们选择了一家相对便宜的航空公司.该航空公司一共在\(n\)个城市设有业务,设这些城市分别标记为\(0\)到\( ...
- [洛谷P4568][JLOI2011]飞行路线
题目大意:最短路,可以有$k$条边无费用 题解:分层图最短路,建成$k$层,层与层之间的边费用为$0$ 卡点:空间计算出错,建边写错 C++ Code: #include <cstdio> ...
- 洛谷 P4568 [JLOI2011]飞行路线
题目描述 Alice和Bob现在要乘飞机旅行,他们选择了一家相对便宜的航空公司.该航空公司一共在n个城市设有业务,设这些城市分别标记为0到n-1,一共有m种航线,每种航线连接两个城市,并且航线有一定的 ...
- P4568 [JLOI2011]飞行路线 分层图最短路
思路:裸的分层图最短路 提交:1次 题解: 如思路 代码: #include<cstdio> #include<iostream> #include<cstring> ...
- 洛谷 4568 [JLOI2011] 飞行路线
题目戳这里 一句话题意: 有n个点,m条边的有向图,最多可以把k条边变为0,求从起点到终点最短距离. Solution 首先看到这题目,感觉贼难,看起来像DP,貌似也有大佬这么做,但鉴于本蒟蒻思维能力 ...
- P4568 [JLOI2011]飞行路线 分层图
题目描述 Alice和Bob现在要乘飞机旅行,他们选择了一家相对便宜的航空公司.该航空公司一共在nn个城市设有业务,设这些城市分别标记为00到n-1n−1,一共有mm种航线,每种航线连接两个城市,并且 ...
- 【BZOJ2763/洛谷p4563】【分层图最短路】飞行路线
2763: [JLOI2011]飞行路线 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 4630 Solved: 1797[Submit][Stat ...
- BZOJ2763[JLOI2011]飞行路线 [分层图最短路]
2763: [JLOI2011]飞行路线 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 2523 Solved: 946[Submit][Statu ...
- bzoj 2763: [JLOI2011]飞行路线 -- 分层图最短路
2763: [JLOI2011]飞行路线 Time Limit: 10 Sec Memory Limit: 128 MB Description Alice和Bob现在要乘飞机旅行,他们选择了一家相 ...
随机推荐
- 201621044079 week13 网络
作业13-网络 1. 本周学习总结 以你喜欢的方式(思维导图.OneNote或其他)归纳总结多网络相关内容. 2. 为你的系统增加网络功能(购物车.图书馆管理.斗地主等)-分组完成 为了让你的系统可以 ...
- ZOJ 1457 E-Prime Ring Problem
https://vjudge.net/contest/67836#problem/E A ring is compose of n circles as shown in diagram. Put n ...
- java 基础 --匿名内部类-008
不全代码 interface Inter(){void show();} class Outer{补全代码} class OuterDemo{ public static void main(Stri ...
- 请问:在delphi中怎样判断DBgrid中数据是否被修改,以便在退出窗口时加以提示
若DBGrid.DataSource.DateSet为ADOQuery1,这样试一下:if ADOQuery1.Modified then ... procedure TForm1.FormClose ...
- openstack之glance部署及操作
由于时间关系简单的架构图就先不展示了.后续的更新会贴上... 部署glance 安装memcache服务 yum install memcached python-memcached systemct ...
- BZOJ3139/BZOJ1306 HNOI2013比赛/CQOI2009循环赛(搜索)
搜索好难啊. 1.对于每个分数集合记忆化. 2.某人得分超过总分,剪枝. 3.某人之后全赢也无法达到总分,剪枝. 4.每有一场比赛分出胜负总分会多三分,而平局则会多两分.某人的分出胜负场次或平局场次超 ...
- NetScaler ‘Counters’ Grab-Bag!
NetScaler ‘Counters’ Grab-Bag! https://www.citrix.com/blogs/author/andrewre/ https://www.citrix.com/ ...
- BZOJ3653 & 洛谷3899:谈笑风生——题解
https://www.lydsy.com/JudgeOnline/problem.php?id=3653 https://www.luogu.org/problemnew/show/P3899 设 ...
- BZOJ2002:[HNOI2010]弹飞绵羊——题解
http://www.lydsy.com/JudgeOnline/problem.php?id=2002 https://www.luogu.org/problemnew/show/P3203 某天, ...
- fhq_treap 学习笔记
前言:昨天写NOIp2017队列,写+调辗转了3h+,不知道怎么的,就点进了一个神仙的链接,便在今日学习了神仙的fhq_treap. 简介:fhq_treap功能强大,支持splay支持的所有操作,代 ...