题目链接: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]飞行路线(最短路分层图)的更多相关文章

  1. Bzoj 2763: [JLOI2011]飞行路线 拆点,分层图,最短路,SPFA

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

  2. BZOJ 2763: [JLOI2011]飞行路线 最短路

    2763: [JLOI2011]飞行路线 Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://www.lydsy.com/JudgeOnline/pr ...

  3. 分层图+最短路算法 BZOJ 2763: [JLOI2011]飞行路线

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

  4. Bzoj 2763: [JLOI2011]飞行路线 dijkstra,堆,最短路,分层图

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

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

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

  6. BZOJ 2763: [JLOI2011]飞行路线 【分层图模板】

    任意门:https://www.lydsy.com/JudgeOnline/problem.php?id=2763 2763: [JLOI2011]飞行路线 Time Limit: 10 Sec  M ...

  7. bzoj 2763 [JLOI2011]飞行路线——分层图

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2763 分层图两种方法的练习. 1.把图分成k+1层,本层去上面一层的边免费.但空间时间都不算 ...

  8. bzoj 2763: [JLOI2011]飞行路线【分层图+spfa】

    为什么早年的题总是从0开始标号啊--又zz了一次WA 分层图的题只有这一个套路吧,建分层图,然后优化时间是分层跑spfa然后层与层之间单独跑即可 #include<iostream> #i ...

  9. bzoj 2763: [JLOI2011]飞行路线

    #include<cstdio> #include<cstring> #include<iostream> #include<queue> #defin ...

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

    题目链接 n个点m条路, 每条路有权值,  给出起点和终点, 求一条路使得权值最小.可以使路过的路中, k条路的权值忽略. 其实就是多一维, 具体看代码 #include<bits/stdc++ ...

随机推荐

  1. Linux账号管理和ACL

    by zjmyster version :1.2 Linux账号管理和ACL权限设置: 主要相关配置文件:/etc/passwd /etc/shadow /etc/group /etc/gshadow ...

  2. 合并两个数组 以KEY 作为键

    <?php     $a= array(         array(             'ID'=> 2         ) );   $b= array(         arr ...

  3. 简单封装的ajax请求

    简单封装了一个ajax请求,做一下统一处理,少写重复代码,只是一个初步的代码,没有经过优化. $.extend({ myAjax: function (option, rollBack) { var ...

  4. SpringMVC 如何定义类型转换器

    举例说明, 将一个字符串转换成的 User 类型. 例如将字符串 1-zcd-1234-zcd@163.com-1999/12/12  转换成User 类型. 一.实体类 public class U ...

  5. MVVM的核心:双向绑定

    MVVM 模式将 Presenter 改名为 ViewModel,基本上与 MVP 模式完全一致. 唯一的区别是,它采用双向绑定(data-binding):View的变动,自动反映在 ViewMod ...

  6. The transaction log for database 'XXX' is full due to 'ACTIVE_TRANSACTION'.

    Msg 9002, Level 17, State 4, Line 4The transaction log for database 'Test' is full due to 'ACTIVE_TR ...

  7. 微信公众号支付IOS系统能够唤起,安卓系统不能唤起的问题解决

    前言 之前系统内做过要给微信支付程序,只不过鉴于业务应用场景,大部分都是使用业务内的金币兑换产品,没有实际用到支付功能.后来运营小哥哥说他的手机不能唤起支付.于是乎我查询了一下资料,发现了这么个问题. ...

  8. csv文件已经python内置csv模块

    csv(Comma Separated Value,即逗号分隔值),文件以纯文本形式存储表格数据(数字和文本).可以用excel打开,并自动将每个逗号隔开的数据作为一列在excel中显示. pytho ...

  9. ICompare 可比较接口

    执行

  10. mysql忘记密码重置密码方法

    https://blog.csdn.net/weidong_y/article/details/80493743