题目链接

分层图最短路。

把每个点拆成\(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]飞行路线 (分层最短路)的更多相关文章

  1. 洛谷 P4568 [JLOI2011]飞行路线 解题报告

    P4568 [JLOI2011]飞行路线 题目描述 Alice和Bob现在要乘飞机旅行,他们选择了一家相对便宜的航空公司.该航空公司一共在\(n\)个城市设有业务,设这些城市分别标记为0到\(n−1\ ...

  2. 洛谷 P4568 [JLOI2011]飞行路线 题解

    P4568 [JLOI2011]飞行路线 题目描述 Alice和Bob现在要乘飞机旅行,他们选择了一家相对便宜的航空公司.该航空公司一共在\(n\)个城市设有业务,设这些城市分别标记为\(0\)到\( ...

  3. [洛谷P4568][JLOI2011]飞行路线

    题目大意:最短路,可以有$k$条边无费用 题解:分层图最短路,建成$k$层,层与层之间的边费用为$0$ 卡点:空间计算出错,建边写错 C++ Code: #include <cstdio> ...

  4. 洛谷 P4568 [JLOI2011]飞行路线

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

  5. P4568 [JLOI2011]飞行路线 分层图最短路

    思路:裸的分层图最短路 提交:1次 题解: 如思路 代码: #include<cstdio> #include<iostream> #include<cstring> ...

  6. 洛谷 4568 [JLOI2011] 飞行路线

    题目戳这里 一句话题意: 有n个点,m条边的有向图,最多可以把k条边变为0,求从起点到终点最短距离. Solution 首先看到这题目,感觉贼难,看起来像DP,貌似也有大佬这么做,但鉴于本蒟蒻思维能力 ...

  7. P4568 [JLOI2011]飞行路线 分层图

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

  8. 【BZOJ2763/洛谷p4563】【分层图最短路】飞行路线

    2763: [JLOI2011]飞行路线 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 4630  Solved: 1797[Submit][Stat ...

  9. BZOJ2763[JLOI2011]飞行路线 [分层图最短路]

    2763: [JLOI2011]飞行路线 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 2523  Solved: 946[Submit][Statu ...

  10. bzoj 2763: [JLOI2011]飞行路线 -- 分层图最短路

    2763: [JLOI2011]飞行路线 Time Limit: 10 Sec  Memory Limit: 128 MB Description Alice和Bob现在要乘飞机旅行,他们选择了一家相 ...

随机推荐

  1. iOS- 网络请求的两种常用方式【GET & POST】的区别

    GET和POST 网络请求的两种常用方式的实现[GET & POST] –GET的语义是获取指定URL上的资源 –将数据按照variable=value的形式,添加到action所指向的URL ...

  2. 分享几个.Net计划任务组件

    Quartz http://www.quartz-scheduler.net/ Hangfire http://hangfire.io/ Install-Package Hangfire 使用OWIN ...

  3. Android出现:Your project path contains non-ASCII characters.

    导入Project的出现: Error:(1, 0) Your project path contains non-ASCII characters. This will most likely ca ...

  4. 求助 delphi ADO组件的 CursorLocation属性设置为 clUseServer 用法 [问题点数:20分]

    我有个管理系统,所有ADOQUERY组件的 CursorLocation属性设置为 clUseClient,一直运行正常,我尝试全部设置为clUseServer, 系统不运行了,请大家帮忙. 我的做法 ...

  5. input 元素 相对父元素错位

    <div class="recommend"> <i class="iconfont icon-user"></i> < ...

  6. init只创建一次 只有父类的init创建servletContext的对象

    init只创建一次 只有父类的init创建servletContext的对象  如果重写父类的方法 但不显示调用父类的init 是不会创建servletContext对象的

  7. 【题解】CF#855 G-Harry Vs Voldemort

    个人感觉挺有意思的,然而被颜神D无聊惹(- ̄▽ ̄)- 这题我们可以首先试图去统计以每一个点作为 w 点所能对答案造成的贡献是多少.不难发现,当且仅当 u 和 v 都在 w 所在边双的一侧的时候不能构成 ...

  8. 【Codeforces Round #405 ( Div 2)】题解

    Bear and Big Brother 签到题,直接模拟就可以了. Bear and Friendship Condition 满足只能是每个朋友圈中每个人和其他人都是朋友,这样的边数的确定的. 然 ...

  9. Android ListView的优化

    最近的项目中有通讯录这个模块,里面的通讯录涉及的联系人数量很大,导致在加载页面的时候有点卡,所以就必须得进行优化,优化的最终实现理论是什么?就是让ListView一次性加载的数据较少,后续根据用户操作 ...

  10. BZOJ2298:[HAOI2011]problem a——题解

    https://www.lydsy.com/JudgeOnline/problem.php?id=2298 https://www.luogu.org/problemnew/show/P2519 一次 ...