bzoj2763
首先是稀疏图,不难想到dij+heap
观察题目可以知道,0<=k<=10;
所以比较裸的想法就是,d[i,j]表示已经免费了i条线路后到达j的最短路
容易得到
d[i,j]:=min(d[i,j],d[i-1,k]);
d[i,j]:=min(d[i,j],d[i,k]+w[k,j]);
然后在做dij在选择中间点的时候,我们穷举一下免费的线路数就可以了;
然后就要维护10个堆……
语言表述有点渣,具体还是看程序吧
const inf=;
type point=record
num,dis:longint;
end;
node=record
next,cost,point:longint;
end; var edge:array[..] of node;
heap:array[..,..] of point;
d,where:array[..,..] of longint;
h:array[..] of longint;
p:array[..] of longint;
len,x,y,z,i,j,n,m,u,k,s,t:longint; procedure swap(var a,b:point);
var c:point;
begin
c:=a;
a:=b;
b:=c;
end; function min(a,b:longint):longint;
begin
if a>b then exit(b) else exit(a);
end; procedure add(x,y,z:longint);
begin
inc(len);
edge[len].point:=y;
edge[len].cost:=z;
edge[len].next:=p[x];
p[x]:=len;
end; procedure sift(p,i:longint);
var j,x,y:longint;
begin
j:=i shl ;
while j<=h[p] do
begin
if (j+<=h[p]) and (heap[p,j].dis>heap[p,j+].dis) then inc(j);
if heap[p,i].dis>heap[p,j].dis then
begin
x:=heap[p,i].num;
y:=heap[p,j].num;
where[p,x]:=j;
where[p,y]:=i;
swap(heap[p,i],heap[p,j]);
i:=j;
j:=i shl ;
end
else break;
end;
end; procedure up(p,i:longint);
var j,x,y:longint;
begin
j:=i shr ;
while j> do
begin
if heap[p,i].dis<heap[p,j].dis then
begin
x:=heap[p,i].num;
y:=heap[p,j].num;
where[p,x]:=j;
where[p,y]:=i;
swap(heap[p,i],heap[p,j]);
i:=j;
j:=j shr ;
end
else break;
end;
end; begin
len:=-;
fillchar(p,sizeof(p),);
readln(n,m,k);
readln(s,t);
inc(s); //点序号都+,习惯一点
inc(t);
for i:= to m do
begin
readln(x,y,z);
inc(x);
inc(y);
add(x,y,z);
add(y,x,z);
end;
for i:= to n do
if i<>s then
begin
for j:= to k do
d[j,i]:=inf;
end
else
for j:= to k do
d[j,i]:=;
for i:= to k do
begin
for j:= to n do
begin
heap[i,j].num:=j;
heap[i,j].dis:=d[i,j];
where[i,j]:=j;
end;
h[i]:=n;
end;
for i:= to k do
up(i,s); //起点不一定是0,要初始化堆,一开始这里被坑翻了
for u:= to n do
begin
for j:=k downto do
begin
x:=heap[j,].num;
y:=heap[j,h[j]].num;
if (h[j]=) or (d[j,x]=inf) then continue;
where[j,y]:=; //dij+heap比较重要的细节
swap(heap[j,],heap[j,h[j]]);
dec(h[j]);
sift(j,); //出堆,调整堆
i:=p[x];
while i<>- do
begin
y:=edge[i].point;
if (j<>k) and (d[j+,y]>d[j,x]) then //两种情况
begin
d[j+,y]:=d[j,x];
heap[j+,where[j+,y]].dis:=d[j+,y];
up(j+,where[j+,y]);
end;
if (d[j,y]>d[j,x]+edge[i].cost) then
begin
d[j,y]:=d[j,x]+edge[i].cost;
heap[j,where[j,y]].dis:=d[j,y];
up(j,where[j,y]);
end;
i:=edge[i].next;
end;
end;
end;
writeln(d[k,t]); //肯定尽可能免费好啊
end.
bzoj2763的更多相关文章
- BZOJ2763[JLOI2011]飞行路线 [分层图最短路]
2763: [JLOI2011]飞行路线 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 2523 Solved: 946[Submit][Statu ...
- 【BZOJ2763】飞行路线(最短路)
[BZOJ2763]飞行路线(最短路) 题面 BZOJ Description Alice和Bob现在要乘飞机旅行,他们选择了一家相对便宜的航空公司.该航空公司一共在n个城市设有业务,设这些城市分别标 ...
- BZOJ2763 JLOI2011 飞行路线 【最短路+DP】
BZOJ2763 JLOI2011 飞行路线 Description Alice和Bob现在要乘飞机旅行,他们选择了一家相对便宜的航空公司.该航空公司一共在n个城市设有业务,设这些城市分别标记为0到n ...
- 【bzoj2763】[JLOI2011]飞行路线 (分层图最短路)(优先队列dij)
[bzoj2763][JLOI2011]飞行路线 2014年3月25日1,7260 Description Alice和Bob现在要乘飞机旅行,他们选择了一家相对便宜的航空公司.该航空公司一共在n个城 ...
- 分层图最短路【bzoj2763】: [JLOI2011]飞行路线
bzoj2763: [JLOI2011]飞行路线 Description Alice和Bob现在要乘飞机旅行,他们选择了一家相对便宜的航空公司.该航空公司一共在n个城市设有业务,设这些城市分别标记为0 ...
- BZOJ2763 [JLOI2011]飞行路线(SPFA + DP)
题目 Source http://www.lydsy.com/JudgeOnline/problem.php?id=2763 Description Alice和Bob现在要乘飞机旅行,他们选择了一家 ...
- Bzoj2763 [JLOI2011]飞行路线
Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 2651 Solved: 1004 Description Alice和Bob现在要乘飞机旅行,他们选 ...
- bzoj2763: [JLOI2011]飞行路线 分层图+dij+heap
分析:d[i][j]代表从起点到点j,用了i次免费机会,那就可以最短路求解 #include <stdio.h> #include <iostream> #include &l ...
- [luogu4568][bzoj2763][JLOI2011]飞行路线
题目描述 Alice和Bob现在要乘飞机旅行,他们选择了一家相对便宜的航空公司.该航空公司一共在n个城市设有业务,设这些城市分别标记为00到n-1,一共有m种航线,每种航线连接两个城市,并且航线有一定 ...
随机推荐
- HDU 3943 K-th Nya Number(数位DP)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3943 题目大意:求出区间 (P,Q] 中找到第K个满足条件的数,条件是该数包含X个4和Y个7 Samp ...
- POJ 1661 Help Jimmy -- 动态规划
题目地址:http://poj.org/problem?id=1661 Description "Help Jimmy" 是在下图所示的场景上完成的游戏. 场景中包括多个长度和高度 ...
- busbox编译出错,arm-linux-未找到命令
1.问题:/opt/FriendlyARM/mini6410/linux/busybox-1.17.2/scripts/gcc-version.sh: 行 11: arm-linux-gcc: 未找到 ...
- DZ升级到X3.2后,UCenter用户管理中心进不了了
前天将DZ升级到X3.2后,UCenter用户管理中心进不了了,输入的密码也对,验证码也对,就是点登录后没反应,又回来输入前的状态.如果更换密码后,显示密码错误,证明密码是没错的.但就是进不了.大家看 ...
- win7定时任务
最近某app有个小抽奖,每天点击太麻烦,想做个定时任务访问抽奖链接(带着cookie登录信息,即可抽奖成功) 刚开始做了php,最后发现部署在百度bae中没法做定时任务每天执行,后来换成了python ...
- 前端资源多个产品整站一键打包&包版本管理(一)
来新公司工作的第五个月.整站资源打包管理也提上了日程. 问题: 首先.什么是整站的打包管理呢? 我们公司的几个重要产品都在同一个webapp里面,但是,不同的开发部门独立开发不同的产品,长期以来,我们 ...
- #Leet Code# Gray Code
描述: 要求相邻数2进制差一位 先获得n-1的列表表示小于 2^(n-1) 的符合要求的列表,加上最高位的加成 2^(n-1) 就是大于等于 2^(n-1) 的符合要求的列表,后者翻转一下就能够与前者 ...
- Logback 简单使用
1.Logback为取代log4j而生 Logback是由log4j创始人Ceki Gülcü设计的又一个开源日志组件.logback当前分成三个模块:logback-core,logback- cl ...
- 【intellij】异常信息汇总
Application Server was not connected before run configuration stop, reason: javax.management.Instanc ...
- 创建第一个UI
创建一个2D UI 制作UI时,首先要创建UI的"根".在Unity顶部NGUI菜单中选择Create,然后选择2D UI. 创建完成后,在Scene窗口中,NGUI自动生成了一个 ...