bzoj2763 [JLOI2011]飞行路线——分层图
题目: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]飞行路线——分层图的更多相关文章
- bzoj2763: [JLOI2011]飞行路线(分层图spfa)
2763: [JLOI2011]飞行路线 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 3234 Solved: 1235[Submit][Stat ...
- BZOJ2763[JLOI2011]飞行路线 [分层图最短路]
2763: [JLOI2011]飞行路线 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 2523 Solved: 946[Submit][Statu ...
- BZOJ2763: [JLOI2011]飞行路线(分层图 最短路)
题意 题目链接 Sol 分层图+最短路 建\(k+1\)层图,对于边\((u, v, w)\),首先在本层内连边权为\(w\)的无向边,再各向下一层对应的节点连边权为\(0\)的有向边 如果是取最大最 ...
- bzoj2763: [JLOI2011]飞行路线 分层图+dij+heap
分析:d[i][j]代表从起点到点j,用了i次免费机会,那就可以最短路求解 #include <stdio.h> #include <iostream> #include &l ...
- [bzoj2763][JLOI2011]飞行路线——分层图最短路
水题.不多说什么. #include <bits/stdc++.h> using namespace std; const int maxn = 10010; const int maxk ...
- bzoj2763 [JLOI]飞行路线 分层图最短路
问题描述 Alice和Bob现在要乘飞机旅行,他们选择了一家相对便宜的航空公司.该航空公司一共在n个城市设有业务,设这些城市分别标记为0到n-1,一共有m种航线,每种航线连接两个城市,并且航线有一定的 ...
- 【bzoj2763】[JLOI2011]飞行路线 分层图最短路
题目描述 Alice和Bob现在要乘飞机旅行,他们选择了一家相对便宜的航空公司.该航空公司一共在n个城市设有业务,设这些城市分别标记为0到n-1,一共有m种航线,每种航线连接两个城市,并且航线有一定的 ...
- bzoj 2763: [JLOI2011]飞行路线 -- 分层图最短路
2763: [JLOI2011]飞行路线 Time Limit: 10 Sec Memory Limit: 128 MB Description Alice和Bob现在要乘飞机旅行,他们选择了一家相 ...
- [BZOJ2963][JLOI2011]飞行路线 分层图+spfa
Description Alice和Bob现在要乘飞机旅行,他们选择了一家相对便宜的航空公司.该航空公司一共在n个城市设有业务,设这些城市分别标记为0到n-1,一共有m种航线,每种航线连接两个城市,并 ...
随机推荐
- scrapy爬取简书整站文章
在这里我们使用CrawlSpider爬虫模板, 通过其过滤规则进行抓取, 并将抓取后的结果存入mysql中,下面直接上代码: jianshu_spider.py # -*- coding: utf-8 ...
- rbac组件之菜单操作(三)
菜单包括菜单列表,菜单列表不仅将菜单列出来,而且将每个菜单下的权限也列出来.菜单的添加.删除.修改. urls.py ... re_path(r'^menus/list/$', MenuView.as ...
- STM32串口程序的一般配置方法
#include "stm32f10x.h" /************************************************ 该程序讲解串口程序的一般配置方法: ...
- bzoj3262陌上花开 cdq分治入门题
Description 有n朵花,每朵花有三个属性:花形(s).颜色(c).气味(m),又三个整数表示.现要对每朵花评级,一朵花的级别是它拥有的美丽能超过的花的数量.定义一朵花A比另一朵花B要美丽,当 ...
- hadoop_exporter+prometheus
1.准备工作 安装go.glibe(需要连google服务器,咋连的,我就不写了,因为尝试了各种办法,都失败了,很伤心) 2.下载hadoop_exporter cd /usr/local/prom/ ...
- jQuery入门--- 非常好
jQuery入门------https://blog.csdn.net/dkh_321/article/details/78093788
- HDU-1083Courses,二分图模板题!
Courses ...
- HDU 4945 (dp+组合数学)
2048 Problem Description Teacher Mai is addicted to game 2048. But finally he finds it's too hard to ...
- js重新讲解继承,es5的一些继承,es6继承的改变 ----------由浅入深
es5 利用原型公有私有继承 function Parent(name) { this.name = name } Parent.prototype.home = '北京'; function Chi ...
- 289. Game of Live
According to the Wikipedia's article: "The Game of Life, also known simply as Life, is a cellul ...