【luogu P4568 [JLOI2011]飞行路线】 题解
题目链接:https://www.luogu.org/problemnew/show/P4568
卡了一晚上,算是分层图最短路的模板。注意卡SPFA,所以我写了个SLF优化。
同时 AC400祭!~
#include <queue>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define ri register
using namespace std;
const int maxn = 200000 + 10;
const int inf = 2147483647;
inline int read()
{
    int k=0,f=1;
    char c=getchar();
    while(!isdigit(c))
    {
        if(c=='-')f=-1;
        c=getchar();
    }
    while(isdigit(c))
    {
        k=(k<<1)+(k<<3)+c-48;
        c=getchar();
    }
    return k*f;
}
int n, m, k, s, end, dis[maxn][20];
bool vis[maxn][20];
struct edge{
    int from, len, to, next;
}e[maxn<<2];
int head[maxn], cnt = 0;
struct que{
    int a, b;
};
deque<que> q;
inline void add(int u, int v, int w)
{
    e[++cnt].from = u;
    e[cnt].to = v;
    e[cnt].len = w;
    e[cnt].next = head[u];
    head[u] = cnt;
}
inline void SPFA()
{
    memset(dis, 127, sizeof(dis));
    memset(vis, 0, sizeof(vis));
    q.push_back((que){s,0});
    dis[s][0] = 0;
    vis[s][0] = 1;
    while(!q.empty())
    {
        que now = q.front(); q.pop_front();
        vis[now.a][now.b] = 0;
        for(ri int i = head[now.a]; i != -1; i = e[i].next)
        {
            if(dis[e[i].to][now.b] > dis[now.a][now.b] + e[i].len)
            {
                dis[e[i].to][now.b] = dis[now.a][now.b] + e[i].len;
                if(vis[e[i].to][now.b] == 0)
                {
                    vis[e[i].to][now.b] = 1;
                    if(q.empty() || dis[e[i].to][now.b] > dis[q.front().a][now.b])
                          q.push_back((que){e[i].to, now.b});
                    else
                    q.push_front((que){e[i].to, now.b});
                }
            }
            if(now.b + 1 <= k)
            {
                if(dis[e[i].to][now.b + 1] > dis[now.a][now.b])
                {
                    dis[e[i].to][now.b + 1] = dis[now.a][now.b];
                    if(vis[e[i].to][now.b + 1] == 0)
                    {
                    		vis[e[i].to][now.b + 1] = 1;
                    		if(q.empty() || dis[e[i].to][now.b] > dis[q.front().a][now.b + 1])
                        q.push_back((que){e[i].to, now.b + 1});
                        else
                        q.push_front((que){e[i].to, now.b + 1});
                    }
                }
            }
        }
    }
}
int main()
{
    //freopen(".in","r",stdin);
    //freopen(".out","w",stdout);
    memset(head, -1, sizeof(head));
    n = read(); m = read(); k = read(); s = read(); end = read();
    for(ri int i = 1; i <= m; i++)
    {
        int u, v, w;
        u = read(); v = read(); w = read();
        add(u, v, w); add(v, u, w);
    }
    SPFA();
    printf("%d",dis[end][k]);
    return 0;
}
【luogu P4568 [JLOI2011]飞行路线】 题解的更多相关文章
- 洛谷 P4568 [JLOI2011]飞行路线 题解
		P4568 [JLOI2011]飞行路线 题目描述 Alice和Bob现在要乘飞机旅行,他们选择了一家相对便宜的航空公司.该航空公司一共在\(n\)个城市设有业务,设这些城市分别标记为\(0\)到\( ... 
- luogu P4568 [JLOI2011]飞行路线
		传送门 看到免费次数\(k\)最多只有10,可以考虑构建\(k+1\)层的分层图,即每一层正常连边,上下两层对应点连边权为0的单向边,最后对所有层里面的\(di_t\)取\(\max\)救星了 #in ... 
- 洛谷 P4568 [JLOI2011]飞行路线 解题报告
		P4568 [JLOI2011]飞行路线 题目描述 Alice和Bob现在要乘飞机旅行,他们选择了一家相对便宜的航空公司.该航空公司一共在\(n\)个城市设有业务,设这些城市分别标记为0到\(n−1\ ... 
- [JLOI2011]飞行路线 题解
		[JLOI2011]飞行路线 题解 题目TP门 题目描述 Alice和Bob现在要乘飞机旅行,他们选择了一家相对便宜的航空公司.该航空公司一共在n个城市设有业务,设这些城市分别标记为0到n-1,一共有 ... 
- 洛谷 P4568 [JLOI2011]飞行路线
		题目描述 Alice和Bob现在要乘飞机旅行,他们选择了一家相对便宜的航空公司.该航空公司一共在n个城市设有业务,设这些城市分别标记为0到n-1,一共有m种航线,每种航线连接两个城市,并且航线有一定的 ... 
- [洛谷P4568][JLOI2011]飞行路线
		题目大意:最短路,可以有$k$条边无费用 题解:分层图最短路,建成$k$层,层与层之间的边费用为$0$ 卡点:空间计算出错,建边写错 C++ Code: #include <cstdio> ... 
- Luogu 2939 [USACO09FEB]改造路Revamping Trails   &&   Luogu 4568 [JLOI2011]飞行路线
		双倍经验 写这两题之前被大佬剧透了呜呜呜. 分层图+最短路. 因为有$k$次机会能够把路径的费用变为$0$,我们可以建$k + 1$层图,对于每一层图我们把原来的边权和双向边连到上面去,而对于层与层之 ... 
- P4568 [JLOI2011]飞行路线 分层图最短路
		思路:裸的分层图最短路 提交:1次 题解: 如思路 代码: #include<cstdio> #include<iostream> #include<cstring> ... 
- [JLOI2011]飞行路线题解
		题目描述 Alice和Bob现在要乘飞机旅行,他们选择了一家相对便宜的航空公司.该航空公司一共在n个城市设有业务,设这些城市分别标记为0到n-1,一共有m种航线,每种航线连接两个城市,并且航线有一定的 ... 
随机推荐
- Hadoop Ecosytem
			There are a lot of Hadoop related projects which are open sourced and widely used by many componies. ... 
- Kettle集群部署(1台Windows主机和2台Linux服务器)
			不多说,直接上干货! http://blog.csdn.net/jianglushou9763/article/details/70859616 
- 修改OPENSUSE 桌面快速搜索快捷键
- TOJ 3651 确定比赛名次
			描述 有N个比赛队(1<=N<=500),编号依次为1,2,3,....,N进行比赛,比赛结束后,裁判委员会要将所有参赛队伍从前往后依次排 名,但现在裁判委员会不能直接获得每个队的比赛成绩 ... 
- JavaScript 闭包初认识
			1.简单的例子 首先从一个经典错误谈起,页面上有若干个div, 我们想给它们绑定一个onclick方法,于是有了下面的代码 <ul id="divTest"> < ... 
- Ling之select
			select用法: 1.Dictionary<string, string>转json Newtonsoft.Json.JsonConvert.SerializeObject(dicSub ... 
- 插入排序——Java实现
			一.排序思想 从数组第一个元素开始(0下标),该元素可以认为已经被排序: 取出待排序列中第一个元素,然后从“有序”序列中,从后往前扫描: 如果该元素(有序序列)大于待插入元素(待排序列),将该元素后移 ... 
- jqueryUI学习
			01.选项卡 拖动按钮<div id="tabs"> <ul> <li><a href="#tabs-1">第一 ... 
- angular - webpack2 例子
			用一周多的时间做了一个简易的wap站 之前研究过webpack但是一直没用过,这次公司要做一个h5网站,正好拿来练练手,话说angular1x对移动端不是很友好,但主要是angular1x比较熟悉,上 ... 
- ADO.NET Tips
			1. SqlCommand.ExecuteScalar Method Executes the query, and returns the first column of the first row ... 
