分层图+最短路算法 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 ...
随机推荐
- 行转列一定要sum
--SELECT 姓名 ,-- 课程 ,-- 分数--FROM tb UNPIVOT ( 分数 FOR 课程 IN ( [语文], [数学], [物理] ) ) t --)
- php中设定一个全局异常处理。全局catch。默认catch。默认异常处理
<?php function handleMissedException($e) { echo "Sorry, something is wrong. Please try again ...
- jquery自定义插件——window的实现
本例子实现弹窗的效果: 1.jquery.show.js /* * 开发者:lzugis * 开发时间:2014年6月10日 * 实现功能:点击在鼠标位置显示div * 版本序号:1.0 */ (fu ...
- JAVa中进制之间的转化方法
public class Code { public static void main(String[] args) throws Exception{ // TODO Auto-generated ...
- (旧)子数涵数·C语言——让C帮你做计算
之前,我们学过了我们的第一个C程序--hello World.现在开始进一步学习,想一想如何让C帮你做计算. 我们先来看代码(我没有新建,还是用之前的hello world.cpp): 好,因为之前在 ...
- CentOS修改服务器系统时间
linux安装完毕后,一般都是国外的世界,一点都不方便设置任务,或者导致网站获取本地的时间错乱,所以就需要把服务器的时间改为和本地时间一致,也就是换成中国的时间. 第一条指令:date –s '201 ...
- 自定义View_2_关于自定义组合View
自定义View(2) Android当中给我们提供了丰富的UI控件,当然也许满足不了我们的需求,我们就必须学会自定义自己的View,我们怎么算是自定义自己的view呢! 我们会根据原来有的View对V ...
- 【JavaEE】SSH+Spring Security+Spring oauth2整合及example
现在加最后一样,就是oauth2,现在很多网站都有对应的移动版本,那么移动端访问服务端的服务怎么控制权限,我知道的主要是两种方法,第一是模拟浏览器,访问服务的时候会生成session,之后在移动端缓存 ...
- javascript宿主对象之window.frames
window.frames属性是当前页面所有框架的集合.要注意的事,这里并没有frame和iframe做出区分.而且,无论页面存不存在框架,window.frames属性总是存在的,并总是指向wind ...
- SPJS Upload for SharePoint: Custom upload page for uploading documents to various document libraries in a site collection
http://spjsblog.com/2013/12/08/spjs-upload-for-sharepoint-custom-upload-page-for-uploading-documents ...