分层图+最短路算法 BZOJ 2763: [JLOI2011]飞行路线
2763: [JLOI2011]飞行路线
Time Limit: 10 Sec Memory Limit: 128 MB
Description
Input
Output
Sample Input
0 4
0 1 5
1 2 5
2 3 5
3 4 5
2 3 3
0 2 100
Sample Output
HINT
对于30%的数据,2<=n<=50,1<=m<=300,k=0;
对于50%的数据,2<=n<=600,1<=m<=6000,0<=k<=1;
对于100%的数据,2<=n<=10000,1<=m<=50000,0<=k<=10.
#include <queue>
#include <cstdio>
#include <cstring>
#include <algorithm>
#define N 11000
#define M 51000
#define inf 0x3f3f3f3f
using namespace std;
struct KSD
{
int v,len,next;
}e[M<<];
int head[N],cnt;
struct Lux
{
int x,y;
Lux(int a,int b):x(a),y(b){}
Lux(){}
};
void add(int u,int v,int len)
{
++cnt;
e[cnt].v=v;
e[cnt].len=len;
e[cnt].next=head[u];
head[u]=cnt;
}
int n,m,p,s,t;
int dist[][N];
bool in[][N];/*spfa的dist,标记在每一层都要有*/
queue<Lux>q;
int spfa()
{
int i,v;
memset(dist,0x3f,sizeof(dist));
q.push(Lux(,s));/*从第0层开始spfa*/
dist[][s]=;
in[][s]=;
while(!q.empty())
{
Lux U=q.front();
q.pop();
in[U.x][U.y]=; for(i=head[U.y];i;i=e[i].next)
{
v=e[i].v;/*先跑完这一层的最短路*/
if(dist[U.x][v]>dist[U.x][U.y]+e[i].len)
{
dist[U.x][v]=dist[U.x][U.y]+e[i].len;
if(!in[U.x][v])q.push(Lux(U.x,v)),in[U.x][v]=;
}
}
if(U.x<p)for(i=head[U.y];i;i=e[i].next)
{/*如果还可以向下一层转移的话,就把这个点出发的每一条边都设为免费下一层转移,因为要记录每个点dist到底用了几个免费的路线,所以用二维数组--分层思想*/
v=e[i].v;
if(dist[U.x+][v]>dist[U.x][U.y])
{
dist[U.x+][v]=dist[U.x][U.y];
if(!in[U.x+][v])q.push(Lux(U.x+,v)),in[U.x+][v]=;
}
}
} int ret=inf;
for(i=;i<=p;i++)ret=min(ret,dist[i][t]);/*在每一层中都找到t的最小值(最多k条免费),为什么要在每一层都找,而不是只在最后一层寻找呢。假设有这么一种情况,由s--t的最少的路上的途径数目少于k条,那么在k之前的某一层上就有dis=0,但是如果必须使用k条路径的话,那么就会找的一条路途数多于k的路来满足这个条件,那么只用第k层的dis自然不是正确结果了。*/
return ret;
} int main()
{
// freopen("test.in","r",stdin);
int i,j,k;
int a,b,c;
scanf("%d%d%d%d%d",&n,&m,&p,&s,&t);
for(i=;i<=m;i++)
{
scanf("%d%d%d",&a,&b,&c);
add(a,b,c);/*无向图建立双向边*/
add(b,a,c);
}
printf("%d\n",spfa());
return ;
}
/*我的代码*/
#define K 11
#include<iostream>
using namespace std;
#include<cstdio>
#include<queue>
#include<cstring>
#define N 10010
#define M 50010
struct QU{
int ce,bh;
};
struct Edge{
int v,w,last;
}edge[M<<];
int head[N],dis[K][N],k,n,m,t=,sta,en;
bool inque[K][N];
inline int read()
{
int ff=,ret=;
char s=getchar();
while(s<''||s>'')
{
if(s=='-') ff=-;
s=getchar();
}
while(s>=''&&s<='')
{
ret=ret*+s-'';
s=getchar();
}
return ret*ff;
}
void add_edge(int u,int v,int w)
{
++t;
edge[t].v=v;
edge[t].w=w;
edge[t].last=head[u];
head[u]=t;
}
void input()
{
n=read();m=read();k=read();
sta=read();en=read();
int a,b,c;
for(int i=;i<=m;++i)
{
a=read();b=read();c=read();
add_edge(a,b,c);
add_edge(b,a,c);
}
}
void SPFA()
{
memset(dis,,sizeof(dis));/*注意赋值的最大值不要超界*/
dis[][sta]=;
inque[][sta]=true;
queue<QU>Q;
QU A;
A.ce=;
A.bh=sta;
Q.push(A);
while(!Q.empty())
{
QU NOw=Q.front();
Q.pop();
inque[NOw.ce][NOw.bh]=false;
for(int l=head[NOw.bh];l;l=edge[l].last)
{
if(dis[NOw.ce][edge[l].v]>edge[l].w+dis[NOw.ce][NOw.bh])
{
dis[NOw.ce][edge[l].v]=edge[l].w+dis[NOw.ce][NOw.bh];
if(!inque[NOw.ce][edge[l].v])
{
inque[NOw.ce][edge[l].v]=true;
QU C;
C.bh=edge[l].v;
C.ce=NOw.ce;
Q.push(C);
}
}
}
if(NOw.ce==k) continue;
for(int l=head[NOw.bh];l;l=edge[l].last)
{
if(dis[NOw.ce+][edge[l].v]>dis[NOw.ce][NOw.bh])
{
dis[NOw.ce+][edge[l].v]=dis[NOw.ce][NOw.bh];
if(!inque[NOw.ce+][edge[l].v])
{
inque[NOw.ce+][edge[l].v]=true;
QU C;
C.bh=edge[l].v;
C.ce=NOw.ce+;
Q.push(C);
}
}
}
}
}
int main()
{
input();
SPFA();
int ans=(<<)-;
for(int i=;i<=k;++i)
ans=min(ans,dis[i][en]);
printf("%d\n",ans);
return ;
}
分层图+最短路算法 BZOJ 2763: [JLOI2011]飞行路线的更多相关文章
- 分层图最短路【bzoj2763】: [JLOI2011]飞行路线
bzoj2763: [JLOI2011]飞行路线 Description Alice和Bob现在要乘飞机旅行,他们选择了一家相对便宜的航空公司.该航空公司一共在n个城市设有业务,设这些城市分别标记为0 ...
- Bzoj 2763: [JLOI2011]飞行路线 dijkstra,堆,最短路,分层图
2763: [JLOI2011]飞行路线 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 1728 Solved: 649[Submit][Statu ...
- 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: 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]飞行路线 最短路
2763: [JLOI2011]飞行路线 Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://www.lydsy.com/JudgeOnline/pr ...
- 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 ...
随机推荐
- sqldbhelper
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Data. ...
- Python Import 详解
http://blog.csdn.net/appleheshuang/article/details/7602499 一 module通常模块为一个文件,直接使用import来导入就好了.可以作为mo ...
- java设计模式--工厂模式
所谓工厂,就是要造产品,比如一个小型砖瓦厂,只有一个窑,既能造砖又能造瓦,这种叫简单工厂模式.规模稍大一点呢,我多个窑,有的窑专门造砖,有的窑专门造瓦,想造别的,比如瓷砖,我再用个专门的窑,这种 ...
- Ajax基础实例
前端代码 <script type="text/javascript"> var xmlhttp; function go(url) { xmlhttp=null; i ...
- linux下的inode记录
我们经常在Linux下可以看到inode,都不知道是什么东东,那么我们现在来慢慢了解下. 一.inode是什么? 理解inode,要从文件储存说起. 文件储存在硬盘上,硬盘的最小存储单位叫做&q ...
- netty Failed to submit an exceptionCaught() event异常
最近测试netty开发的服务端应用在关闭时,出现下列异常: ->###WARN#########nioEventLoopGroup-3-2###io.netty.util.internal.lo ...
- docker 使用
https://www.digitalocean.com/community/tutorials/how-to-use-the-digitalocean-docker-application http ...
- 苹果手机不进post方法
今天遇到一个问题,开发的公众号中的一个界面在安卓和微信开发者工具中可以正常显示,在苹果手机中加载不出数据. 以下是部分代码: var start = 0; var limit = 15; var ca ...
- JDK8 API文档(下载)
DK API文档 java SE 8 API文档: http://www.oracle.com/technetwork/java/javase/documentation/jdk8-doc-downl ...
- Swift基础之闭包
内容纲要: 1.闭包基础 2.关于闭包循环引用 正文: 1.闭包 闭包是自包含的函数代码块,可以在代码中被传递和使用.Swift 中的闭包与 C 和 Objective-C 中的代码块(blocks) ...