Description
在幻想乡,伊吹萃香是能够控制物体密度的鬼王。因为能够控制密度,所以萃香能够制造白洞和黑洞,并可以随时改变它们。某一天萃香闲着无聊,在妖怪之山上设置了一些白洞或黑洞,由于引力的影响,给妖怪们带来了很大的麻烦。于是他们决定找出一条消耗体力最少的路,来方便进出。已知妖怪之山上有N个路口(编号1..N),每个路口都被萃香设置了一定质量白洞或者黑洞。原本在各个路口之间有M条单向路,走过每一条路需要消耗一定量的体力以及1个单位的时间。由于白洞和黑洞的存在,走过每条路需要消耗的体力也就产生了变化,假设一条道路两端路口黑白洞的质量差为delta: 1. 从有白洞的路口走向有黑洞的路口,消耗的体力值减少delta,若该条路径消耗的体力值变为负数的话,取为0。 2. 从有黑洞的路口走向有白洞的路口,消耗的体力值增加delta。 3. 如果路口两端均为白洞或黑洞,消耗的体力值无变化。 由于光是放置黑洞白洞不足以体现萃香的强大,所以她决定每过1个单位时间,就把所有路口的白洞改成黑洞,黑洞改成白洞。当然在走的过程中你可以选择在一个路口上停留1个单位的时间,如果当前路口为白洞,则不消耗体力,否则消耗s[i]的体力。现在请你计算从路口1走到路口N最小的体力消耗。保证一定存在道路从路口1到路口N。
第1行:2个正整数N, M 第2行:N个整数,第i个数为0表示第i个路口开始时为白洞,1表示黑洞 第3行:N个整数,第i个数表示第i个路口设置的白洞或黑洞的质量w[i] 第4行:N个整数,第i个数表示在第i个路口停留消耗的体力s[i] 第5..M+4行:每行3个整数,u, v, k,表示在没有影响的情况下,从路口u走到路口v需要消耗k的体力。
Output
第1行:1个整数,表示消耗的最小体力
4 5
1 0 1 0
10 10 100 10
5 20 15 10
1 2 30
2 3 40
1 3 20
1 4 200
3 4 200
Sample Output
130
【数据范围】
对于30%的数据:1 <= N <= 100, 1 <= M <= 500 对于60%的数据:1 <= N <= 1,000, 1 <= M <= 5,000 对于100%的数据:1 <= N <= 5,000, 1 <= M <= 30,000 其中20%的数据为1 <= N <= 3000的链 1 <= u,v <= N, 1 <= k,w[i],s[i] <= 200
【样例说明】
按照1 -> 3 -> 4的路线。
题解
因为每个点的状态是变化的,所以可以把一个点分成两个点,分别表示到达这个点时时间为奇数的最短路和时间为偶数的最短路。
f[i,j]表示到达i,j时时间为奇数的最短路
f[i+n,j+n]表示到达i,j时时间为奇数的最短路
如果i到达j的时间为奇数,出发的点一定是到达时为偶数,所以i+n连接到j,否则相反i连接到j+n。
但问题来了,本题最坑的“等待”:如果等完后的时间为奇数,开始等时的时间为偶数,所以i+n连接到i,否则相反i连接到i+n。
然后处理出的新图跑一遍spfa,最后判断一下是f[n]短还是f[n+n]短,输出。
代码
type
nyd=
record
x,y,z:longint;
end;
var
m,n,t:longint;
a,p,w,s:
array[
0..
30000]
of longint;
b:
array[
0..
30000]
of nyd;
c:
array[
0..
5000,
0..
1]
of boolean;
d:
array[
0..
5000,
0..
1]
of longint;
q,st:
array[
0..
1000000]
of longint;
function min(a,b:longint):longint;
begin
if a<b
then exit(b)
else exit(b);
end;
function max(a,b:longint):longint;
begin
if a>b
then exit(a)
else exit(b);
end;
procedure spfa;
var
nn,x,y,h,z,l:longint;
begin
h:=
1;t:=
1;
q[
1]:=
1;st[
1]:=p[
1];d[
1,p[
1]]:=
0;c[
1,p[
1]]:=
true;
while h<=t
do
begin
x:=q[h];
if (st[h]=
0)
and(d[x,st[h]
xor 1]>d[x,st[h]])
then
begin
d[x,st[h]
xor 1]:=d[x,st[h]];
if not c[x,st[h]
xor 1]
then
begin
inc(t);
c[x,st[h]
xor 1]:=
true;
q[t]:=x;
st[t]:=st[h]
xor 1;
end;
end;
if(s[h]=
1)
and(d[x,st[h]
xor 1]>d[x,st[h]]+w[x])
then
begin
d[x,st[h]
xor 1]:=d[x,st[h]]+w[x];
if not c[x,st[h]
xor 1]
then
begin
inc(t);
q[t]:=x;
st[t]:=st[h]
xor 1;
c[x,st[h]
xor 1]:=
true;
end;
end;
z:=a[x];
while z<>
0 do
begin
y:=b[z].y;
if st[h]=p[x]
then nn:=p[y]
else nn:=p[y]
xor 1;
if st[h]=nn
then l:=b[z].z
else
if(st[h]=
0)
and(nn=
1)
then l:=max(
0,b[z].z-abs(w[x]-w[y]))
else
if(st[h]=
1)
and(nn=
0)
then l:=b[z].z+abs(w[x]-w[y]);
if d[y,nn
xor 1]>d[x,st[h]]+l
then
begin
d[y,nn
xor 1]:=d[x,st[h]]+l;
if not c[y,nn
xor 1]
then
begin
inc(t);
q[t]:=y;
st[t]:=nn
xor 1;
c[y,nn
xor 1]:=
true;
end;
end;
z:=b[z].x;
end;
c[x,st[h]]:=
false;
inc(h);
end;
writeln(min(d[n,
1],d[n,
0]));
end;
var
i,x,y,z:longint;
begin
readln(n,m);
for i:=
1 to n
do read(p[i]);
for i:=
1 to n
do read(w[i]);
for i:=
1 to n
do read(s[i]);
for i:=
1 to m
do
begin
readln(x,y,z);
inc(t);
b[t].x:=a[x];a[x]:=t;b[t].z:=z;b[t].y:=y;
end;
fillchar(d,sizeof(d),
100);
spfa;
end.
转载请注明原文地址: https://ju.6miu.com/read-1305781.html