BZOJ:2763-[JLOI2011]飞行路线(最短路分层图)
题目链接:https://www.lydsy.com/JudgeOnline/problem.php?id=2763
解题心得:
- 第一次见到分层最短路。其实题中说选择k条路径免费,那怎么选k条路径并没有一个明确的选法,就只能遍历。分层图就是一个图分成k层,每个节点可以走当前层的相邻节点,费用为cost,但是也可以走下一层的相邻节点,费用为0,因为只有k层,所以从第0层的S到达第k层的T也就走了k个免费路径。这个时候K不能太大,不然容易MLE。
#include <stdio.h>
#include <cstring>
#include <algorithm>
#include <queue>
#include <vector>
using namespace std;
typedef long long ll;
const int maxn = 5e5+;
const ll INF = 1e12; ll n, m, k, S, T; struct edge {
ll to, len; edge(ll to1, ll len1):
to(to1), len(len1){};
}; struct NODE {//记录队列中节点的位置,层数,花费
ll len, pos, c; bool friend operator < (NODE a, NODE b) {
return a.len > b.len;
} NODE (ll len1, ll pos1, ll c1):
len(len1), pos(pos1), c(c1){};
}; vector <edge> ve[maxn]; ll dis[][maxn]; void add_edge(ll u, ll v, ll len) {//注意是双向边
ve[u].push_back(edge(v, len));
ve[v].push_back(edge(u, len));
} void init() {
for(int i=;i<;i++)
for(int j=;j<maxn;j++)
dis[i][j] = INF; scanf("%lld%lld%lld",&n,&m,&k);
scanf("%lld%lld",&S, &T);
for(int i=;i<m;i++) {
ll u, v, len;
scanf("%lld%lld%lld",&u, &v, &len);
add_edge(u, v, len);
}
} void dij() {//用dij不容易被卡网格图
priority_queue <NODE> qu;
qu.push(NODE(, S, ));
dis[][S] = ;
while(!qu.empty()) {
NODE now = qu.top(); qu.pop();
ll u = now.pos;
for(int i=;i<ve[u].size();i++) {//走当前层
ll v = ve[u][i].to;
ll len = ve[u][i].len;
ll c = now.c;
if(dis[c][v] > dis[c][u]+len) {
dis[c][v] = dis[c][u] + len;
qu.push(NODE(dis[c][v], v, c));
}
} ll c = now.c;
if(c < k) {//走下一层
for (int i = ; i < ve[u].size(); i++) {
ll v = ve[u][i].to;
if(dis[c+][v] > dis[c][u]) {//花费为0
dis[c+][v] = dis[c][u];
qu.push(NODE(dis[c][u], v, c+));
}
}
}
}
} void min_path() {
dij();
ll Min = INF;
for(int i=;i<=k;i++) {
Min = min(Min, dis[i][T]);
}
printf("%lld\n", Min);
} int main() {
init();
min_path();
}
BZOJ:2763-[JLOI2011]飞行路线(最短路分层图)的更多相关文章
- Bzoj 2763: [JLOI2011]飞行路线 拆点,分层图,最短路,SPFA
2763: [JLOI2011]飞行路线 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 1694 Solved: 635[Submit][Statu ...
- BZOJ 2763: [JLOI2011]飞行路线 最短路
2763: [JLOI2011]飞行路线 Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://www.lydsy.com/JudgeOnline/pr ...
- 分层图+最短路算法 BZOJ 2763: [JLOI2011]飞行路线
2763: [JLOI2011]飞行路线 Time Limit: 10 Sec Memory Limit: 128 MB Description Alice和Bob现在要乘飞机旅行,他们选择了一家相 ...
- Bzoj 2763: [JLOI2011]飞行路线 dijkstra,堆,最短路,分层图
2763: [JLOI2011]飞行路线 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 1728 Solved: 649[Submit][Statu ...
- bzoj 2763: [JLOI2011]飞行路线 -- 分层图最短路
2763: [JLOI2011]飞行路线 Time Limit: 10 Sec Memory Limit: 128 MB Description Alice和Bob现在要乘飞机旅行,他们选择了一家相 ...
- BZOJ 2763: [JLOI2011]飞行路线 【分层图模板】
任意门:https://www.lydsy.com/JudgeOnline/problem.php?id=2763 2763: [JLOI2011]飞行路线 Time Limit: 10 Sec M ...
- bzoj 2763 [JLOI2011]飞行路线——分层图
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2763 分层图两种方法的练习. 1.把图分成k+1层,本层去上面一层的边免费.但空间时间都不算 ...
- bzoj 2763: [JLOI2011]飞行路线【分层图+spfa】
为什么早年的题总是从0开始标号啊--又zz了一次WA 分层图的题只有这一个套路吧,建分层图,然后优化时间是分层跑spfa然后层与层之间单独跑即可 #include<iostream> #i ...
- bzoj 2763: [JLOI2011]飞行路线
#include<cstdio> #include<cstring> #include<iostream> #include<queue> #defin ...
- bzoj 2763: [JLOI2011]飞行路线 分层图
题目链接 n个点m条路, 每条路有权值, 给出起点和终点, 求一条路使得权值最小.可以使路过的路中, k条路的权值忽略. 其实就是多一维, 具体看代码 #include<bits/stdc++ ...
随机推荐
- sqlite 用法整理
转载:http://blog.csdn.net/zhaoweixing1989/article/details/19080593 先纪录到这,以后慢慢整理. 1. 在Android下通过adb she ...
- gtest学习
介绍 学习如下gtest课程,总结主要的部分 1.玩转Google开源C++单元测试框架Google Test系列(gtest)之一 - 初识gtest 2.玩转Google开源C++单元测试框架Go ...
- Lovable eccentric
It took him four years to stage this elaborate joke simply to prove that critics do not always know ...
- [EffectiveC++]item39:明智而审慎地使用private
187页 private继承意味implementedin-terms-of(根据某物实现出).如果你让class D以private形式继承class B,你的用意为了采用class B内已经备妥的 ...
- hdu 6216 A Cubic number and A Cubic Number【数学题】
hdu 6216 A Cubic number and A Cubic Number[数学] 题意:判断一个素数是否是两个立方数之差,就是验差分.. 题解:只有相邻两立方数之差才可能,,因为x^3-y ...
- 打印出类所在的jar包
ackage time; /** * Created by sheting on 10/20/2017 */ public class Test { public static void main(S ...
- bzoj4600 [Sdoi2016]硬币游戏
Description Alice和Bob现在在玩的游戏,主角是依次编号为1到n的n枚硬币.每一枚硬币都有两面,我们分别称之为正面和反面.一开始的时候,有些硬币是正面向上的,有些是反面朝上的.Alic ...
- [HNOI2007]紧急疏散EVACUATE
嘟嘟嘟 看数据范围,第一反应觉得爆搜是不是能骗点分,但发现爆搜太难写了,于是就开始想想正解…… 正解大概猜到了是网络流,但是怎么把时间这个条件加入到图的内容中,却困扰了我好半天,总是感觉把这种不同维度 ...
- Linux学习总结(十)-文件复制及查看, 环境变量
一 文件复制及移动 1.命令 cp --------copy 的意思格式 cp 选项 源文件 目标文件a: 对于文件我们直接cp 文件 目标文件假定我们在普通用户家目录下/home/lv新建两个普通文 ...
- [转]ASP.NET母版页中对控件ID的处理
一.问题提出 由于总体排版和设计的需要,我们往往创建母版页来实现整个网站的统一性,最近我由于统一性的需要,把原来整个项目单独的页面全部套用了母版页.但是出现了一个错误……在我的Blog中记录一下,方便 ...