【洛谷 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现在要乘飞机旅行,他们选择了一家相 ...
随机推荐
- 转 【关于api-ms-win-crt-runtimel1-1-0.dll缺失的解决方案】
关于api-ms-win-crt-runtimel1-1-0.dll缺失的解决方案 目录 关于api-ms-win-crt-runtimel1-1-0dll缺失的解决方案 目录 安装VC redite ...
- 实现一个可以实时提示的textarea组件
该组件输入.换行.变换光标可以实时给出提示 效果: textarea.vue <template> <div> <el-input id="user-input ...
- C# 使用this的形参
示例1: public static RectangleF TransformRect(this Matrix mat, RectangleF rect) 是向Matrix类扩展带有Rectangle ...
- 新建maven工程问题001
这周一直在研究SpringMVC+Mybatis,有些心得,记录一下. Ⅰ:建maven遇到的问题. 1.1 新建maven时选中[Create a simple project]这样,后面[Pack ...
- 【Python】Python之文件操作
1. file=open('xxx.txt', encoding='utf-8'),open()函数是Python内置的用于对文件的读写操作,返回的是文件的流对象(而不是文件本身,所以使用的方法都是流 ...
- 编写高效Lua代码的方法
编写高效Lua代码的方法 翻译自<Lua Programming Gems>Chapter 2:Lua Performance Tips:Basic fact By Roberto Ier ...
- [NOI2006]网络收费
题面在这里 description 一棵\(2^n\)个叶节点的满二叉树,每个节点代表一个用户,有一个预先的收费方案\(A\)或\(B\); 对于任两个用户 \(i,j(1≤i<j≤2^n)i, ...
- 【Codeforces Round #405 ( Div 2)】题解
Bear and Big Brother 签到题,直接模拟就可以了. Bear and Friendship Condition 满足只能是每个朋友圈中每个人和其他人都是朋友,这样的边数的确定的. 然 ...
- HDU.2503 a/b + c/d (分式化简)
a/b + c/d Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Sub ...
- Spring.NET中事务管理【转】
http://www.cnblogs.com/GoodHelper/archive/2009/11/16/springnet_transaction.html 浏览了下写的比较清楚. 在.NET FC ...