题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2763

构建分层图。

代码如下:

写法1(空间略大)(时间很慢):

#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
priority_queue<pair<int,int> >q;
int const maxn=2e5+,maxm=;
int n,m,k,s,t,head[maxn],ct,dis[maxn];
bool vis[maxn];
struct N{
int to,next,w;
N(int t=,int n=,int w=):to(t),next(n),w(w) {}
}edge[maxm<<];
void add(int x,int y,int z)
{
edge[++ct]=N(y,head[x],z); head[x]=ct;
edge[++ct]=N(x,head[y],z); head[y]=ct;
for(int i=;i<=k-;i++)
{
int u=x+i*n,v=y+(i+)*n;
edge[++ct]=N(v,head[u],); head[u]=ct;
u=y+i*n,v=x+(i+)*n;
edge[++ct]=N(v,head[u],); head[u]=ct;
u=x+(i+)*n,v=y+(i+)*n;
edge[++ct]=N(v,head[u],z); head[u]=ct;
edge[++ct]=N(u,head[v],z); head[v]=ct;
}
}
void dijkstra()
{
memset(dis,0x3f,sizeof dis);
dis[s]=; q.push(make_pair(,s));
while(q.size())
{
int x=q.top().second; q.pop();
while(vis[x]&&q.size())x=q.top().second,q.pop();
if(vis[x])break; vis[x]=;
for(int i=head[x];i;i=edge[i].next)
{
int u=edge[i].to;
if(dis[u]>dis[x]+edge[i].w)
{
dis[u]=dis[x]+edge[i].w;
q.push(make_pair(-dis[u],u));
}
}
}
}
int main()
{
scanf("%d%d%d%d%d",&n,&m,&k,&s,&t);
t+=k*n;
for(int i=,x,y,z;i<=m;i++)
{
scanf("%d%d%d",&x,&y,&z);
add(x,y,z);
}
dijkstra();
printf("%d",dis[t]);
return ;
}

写法2(空间小)(时间飞快)(bfs)(dijkstra?):

#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
int const maxn=,maxm=;
struct P{
int bh,d,f;
P(int b=,int d=,int f=):bh(b),d(d),f(f) {}
bool operator < (const P &y) const
{
return d>y.d;//priority_queue 是从大到小排序
}
};
priority_queue<P>q;
int n,m,K,head[maxn],ct,s,t,dis[maxn][];
bool vis[maxn][];
struct N{
int to,next,w;
N(int t=,int n=,int w=):to(t),next(n),w(w) {}
}edge[maxm<<];
void add(int x,int y,int z){edge[++ct]=N(y,head[x],z); head[x]=ct;}
void bfs()
{
memset(dis,0x3f,sizeof dis);
dis[s][]=; q.push(P(s,,));
while(q.size())
{
int x=q.top().bh, d=q.top().d, f=q.top().f; q.pop();
if(vis[x][f])continue;
vis[x][f]=;
if(x==t)
{
printf("%d",d); return;
}
for(int i=head[x];i;i=edge[i].next)
{
int u=edge[i].to;
if(f<K&&dis[u][f+]>d&&!vis[u][f+])
{
dis[u][f+]=d;
q.push(P(u,d,f+));
}
if(dis[u][f]>d+edge[i].w&&!vis[u][f])
{
dis[u][f]=d+edge[i].w;
q.push(P(u,dis[u][f],f));
}
}
}
}
int main()
{
scanf("%d%d%d%d%d",&n,&m,&K,&s,&t);
for(int i=,x,y,z;i<=m;i++)
{
scanf("%d%d%d",&x,&y,&z);
add(x,y,z); add(y,x,z);
}
bfs();
return ;
}

bzoj2763 [JLOI2011]飞行路线——分层图的更多相关文章

  1. bzoj2763: [JLOI2011]飞行路线(分层图spfa)

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

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

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

  3. BZOJ2763: [JLOI2011]飞行路线(分层图 最短路)

    题意 题目链接 Sol 分层图+最短路 建\(k+1\)层图,对于边\((u, v, w)\),首先在本层内连边权为\(w\)的无向边,再各向下一层对应的节点连边权为\(0\)的有向边 如果是取最大最 ...

  4. bzoj2763: [JLOI2011]飞行路线 分层图+dij+heap

    分析:d[i][j]代表从起点到点j,用了i次免费机会,那就可以最短路求解 #include <stdio.h> #include <iostream> #include &l ...

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

    水题.不多说什么. #include <bits/stdc++.h> using namespace std; const int maxn = 10010; const int maxk ...

  6. bzoj2763 [JLOI]飞行路线 分层图最短路

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

  7. 【bzoj2763】[JLOI2011]飞行路线 分层图最短路

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

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

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

  9. [BZOJ2963][JLOI2011]飞行路线 分层图+spfa

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

随机推荐

  1. history.go history.back()

    转http://www.mikebai.com/Article/2009-11/757.html <input type=button value=刷新 onclick="window ...

  2. LeetCode(30) Substring with Concatenation of All Words

    题目 You are given a string, s, and a list of words, words, that are all of the same length. Find all ...

  3. 集训第六周 M题

    Description   During the early stages of the Manhattan Project, the dangers of the new radioctive ma ...

  4. STM32F407 开发环境搭建 程序下载 个人笔记

    详细资料: http://www.openedv.com/thread-13912-1-1.html 需要安装的软件: 1.keil(MDK,必选),用keygen破解 2.CH340驱动,(usb串 ...

  5. 添物不花钱学JavaEE(基础篇) --HTML

    HTML是什么? HTML – Hyper Text Markup Language HTML官方网址 http://www.w3.org/TR/2014/REC-html5-20141028/ 其实 ...

  6. rsync远程文件传输

    [root@rhel5 ~]# rsync -a log.tar.gz root@192.168.124.129:/tmp root@192.168.124.129's password: Permi ...

  7. VMware配置从U盘启动

    很遗憾,VMware的BIOS不能识别USB启动设备,即使已经把USB设备连接上去. 解决这一问题的做法是直接添加硬盘,硬盘指向物理硬盘,即USB设置. 注意:Ubuntu下要设置这一功能需要使用su ...

  8. Ionic3错误记录:navigation stack needs at least one root page

    BUG场景:在 ActionSheetController 使用modalCtrl.create 创建模态框时报如下错误 原代码片段 解决方式: 重新设置root page

  9. C# .NET如何定义图片按钮

    先添加一个按钮,然后修改Image或者BackgroundImage显然不好,我想要按钮透明,就不要放在按钮的方框里,而且鼠标滑过和鼠标点击最好都是我自定义.   我们拉一个PictureBox,然后 ...

  10. Linux下C编程的学习_1

    0x0:为什么写这个系列的文章 博客原本的定位是安卓游戏的破解,可是为什么写这系列的文章呢? 由于在破解过程中,我们是无法避免来敲代码的,恢复算法,模拟算法,游戏中对数据的解密.游戏中对保存在clie ...