P2763: [JLOI2011]飞行路线

时间:2023-11-17 09:24:44

然而WA了呀,这道分层图,也是不明白为什么WA了=-=

 const maxe=; maxn=; points=;
type
node=record
f,t,l:longint;
end;
var n,m,k,i,j,u,v,x,s,t,num:longint;
b:array[..maxe] of node;
ans:int64;
f:array[..] of longint;
p:array[..points] of boolean;
head:array[..points] of longint;
d:array[..points] of int64;
procedure insert(u,v,x:longint);
begin
inc(num);
b[num].f:=head[u];
b[num].t:=v;
b[num].l:=x;
head[u]:=num;
end;
procedure spfa;
var now,nowe,l,r,v:longint;
begin
for l:= to points do d[l]:=maxn;
fillchar(p,sizeof(p),true);
l:=; r:=; f[]:=s; d[s]:=; p[s]:=false; ans:=maxn; //writeln(ans);
while l<=r do
begin
now:=f[l];
nowe:=head[now];
while nowe<> do
begin
v:=b[nowe].t;
if d[now]+b[nowe].l<d[v] then
begin
d[v]:=d[now]+b[nowe].l;
if p[v] then
begin
p[v]:=false;
inc(r);
f[r]:=v;
end;
//if ((v mod k)=t) and (d[v]<ans) then ans:=d[v];
end;
nowe:=b[nowe].f;
end;
inc(l);
p[now]:=true;
end;
for l:= to k do
if ans>d[l*n+t] then ans:=d[l*n+t];
end;
begin
readln(n,m,k);
readln(s,t);
inc(s); inc(t);
for i:= to m do
begin
readln(u,v,x);
inc(u); inc(v);
for j:= to k do
begin
insert(u+j*n,v+j*n,x);
insert(v+j*n,u+j*n,x);
if j<>k then
begin
insert(u+j*n,v+(j+)*n,);
insert(v+j*n,u+(j+)*n,);
end;
end;
end;
spfa;
if ans=maxn then writeln()
else writeln(ans);
end.

暂且先不管为什么WA吧,首先这是个分层图原图既然是一层的。我们把它拆成k+1层。每一条边既能连本层,也能连到下一层。然后直接裸上Dijikstra即可,而spfa大概也是可以的吧。

而这是正解

 #include <queue>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define N 1200100
using namespace std;
int n,m,k;
int st,ed,cnt;
int head[N];
int dis[N];
int v[N];
struct node
{
int from,to,val,next;
}edge[N<<];
struct element
{
int val,no;
};
bool operator < (element a,element b)
{
if(a.val==b.val)return a.no<b.no;
return a.val>b.val;
}
priority_queue<element>q;
void dijikstra(int s,int e)
{
memset(dis,0x3f,sizeof(dis));
element fir;
fir.val=,fir.no=s;
dis[s]=;
q.push(fir);
while(!q.empty())
{
element u=q.top();
q.pop();
if(v[u.no])continue;
v[u.no]=;
for(int i=head[u.no];i!=-;i=edge[i].next)
{
int to=edge[i].to;
if(dis[u.no]+edge[i].val<dis[to])
{
dis[to]=dis[u.no]+edge[i].val;
element pus;
pus.no=to,pus.val=dis[to];
q.push(pus);
}
}
}
int ans=0x3f3f3f3f;
for(int i=;i<=k;i++)
{
ans=min(ans,dis[e+i*n]);
}
printf("%d\n",ans);
}
void init()
{
memset(head,-,sizeof(head));
cnt=;
}
void edgeadd(int from,int to,int val)
{
edge[cnt].from=from;
edge[cnt].to=to;
edge[cnt].val=val;
edge[cnt].next=head[from];
head[from]=cnt++;
} int main()
{
init();
scanf("%d%d%d",&n,&m,&k);
scanf("%d%d",&st,&ed);
st++,ed++;
for(int i=;i<=m;i++)
{
int x,y,z;
scanf("%d%d%d",&x,&y,&z);
x++,y++;
for(int i=;i<=k;i++)
{
edgeadd(x+i*n,y+i*n,z);
edgeadd(y+i*n,x+i*n,z);
if(i!=k)
{
edgeadd(x+i*n,y+(i+)*n,);
edgeadd(y+i*n,x+(i+)*n,);
}
}
}
dijikstra(st,ed);
}