P2763: [JLOI2011]飞行路线
然而WA了呀,这道分层图,也是不明白为什么WA了=-=
1 const maxe=1400001; maxn=100000000001; points=300001;
2 type
3 node=record
4 f,t,l:longint;
5 end;
6 var n,m,k,i,j,u,v,x,s,t,num:longint;
7 b:array[0..maxe] of node;
8 ans:int64;
9 f:array[0..50000000] of longint;
10 p:array[0..points] of boolean;
11 head:array[0..points] of longint;
12 d:array[0..points] of int64;
13 procedure insert(u,v,x:longint);
14 begin
15 inc(num);
16 b[num].f:=head[u];
17 b[num].t:=v;
18 b[num].l:=x;
19 head[u]:=num;
20 end;
21 procedure spfa;
22 var now,nowe,l,r,v:longint;
23 begin
24 for l:=1 to points do d[l]:=maxn;
25 fillchar(p,sizeof(p),true);
26 l:=1; r:=1; f[1]:=s; d[s]:=0; p[s]:=false; ans:=maxn; //writeln(ans);
27 while l<=r do
28 begin
29 now:=f[l];
30 nowe:=head[now];
31 while nowe<>0 do
32 begin
33 v:=b[nowe].t;
34 if d[now]+b[nowe].l<d[v] then
35 begin
36 d[v]:=d[now]+b[nowe].l;
37 if p[v] then
38 begin
39 p[v]:=false;
40 inc(r);
41 f[r]:=v;
42 end;
43 //if ((v mod k)=t) and (d[v]<ans) then ans:=d[v];
44 end;
45 nowe:=b[nowe].f;
46 end;
47 inc(l);
48 p[now]:=true;
49 end;
50 for l:=0 to k do
51 if ans>d[l*n+t] then ans:=d[l*n+t];
52 end;
53 begin
54 readln(n,m,k);
55 readln(s,t);
56 inc(s); inc(t);
57 for i:=1 to m do
58 begin
59 readln(u,v,x);
60 inc(u); inc(v);
61 for j:=0 to k do
62 begin
63 insert(u+j*n,v+j*n,x);
64 insert(v+j*n,u+j*n,x);
65 if j<>k then
66 begin
67 insert(u+j*n,v+(j+1)*n,0);
68 insert(v+j*n,u+(j+1)*n,0);
69 end;
70 end;
71 end;
72 spfa;
73 if ans=maxn then writeln(0)
74 else writeln(ans);
75 end.
暂且先不管为什么WA吧,首先这是个分层图原图既然是一层的。我们把它拆成k+1层。每一条边既能连本层,也能连到下一层。然后直接裸上Dijikstra即可,而spfa大概也是可以的吧。
而这是正解
1 #include <queue>
2 #include <cstdio>
3 #include <cstring>
4 #include <iostream>
5 #include <algorithm>
6 #define N 1200100
7 using namespace std;
8 int n,m,k;
9 int st,ed,cnt;
10 int head[N];
11 int dis[N];
12 int v[N];
13 struct node
14 {
15 int from,to,val,next;
16 }edge[N<<1];
17 struct element
18 {
19 int val,no;
20 };
21 bool operator < (element a,element b)
22 {
23 if(a.val==b.val)return a.no<b.no;
24 return a.val>b.val;
25 }
26 priority_queue<element>q;
27 void dijikstra(int s,int e)
28 {
29 memset(dis,0x3f,sizeof(dis));
30 element fir;
31 fir.val=0,fir.no=s;
32 dis[s]=0;
33 q.push(fir);
34 while(!q.empty())
35 {
36 element u=q.top();
37 q.pop();
38 if(v[u.no])continue;
39 v[u.no]=1;
40 for(int i=head[u.no];i!=-1;i=edge[i].next)
41 {
42 int to=edge[i].to;
43 if(dis[u.no]+edge[i].val<dis[to])
44 {
45 dis[to]=dis[u.no]+edge[i].val;
46 element pus;
47 pus.no=to,pus.val=dis[to];
48 q.push(pus);
49 }
50 }
51 }
52 int ans=0x3f3f3f3f;
53 for(int i=0;i<=k;i++)
54 {
55 ans=min(ans,dis[e+i*n]);
56 }
57 printf("%d\n",ans);
58 }
59 void init()
60 {
61 memset(head,-1,sizeof(head));
62 cnt=1;
63 }
64 void edgeadd(int from,int to,int val)
65 {
66 edge[cnt].from=from;
67 edge[cnt].to=to;
68 edge[cnt].val=val;
69 edge[cnt].next=head[from];
70 head[from]=cnt++;
71 }
72
73 int main()
74 {
75 init();
76 scanf("%d%d%d",&n,&m,&k);
77 scanf("%d%d",&st,&ed);
78 st++,ed++;
79 for(int i=1;i<=m;i++)
80 {
81 int x,y,z;
82 scanf("%d%d%d",&x,&y,&z);
83 x++,y++;
84 for(int i=0;i<=k;i++)
85 {
86 edgeadd(x+i*n,y+i*n,z);
87 edgeadd(y+i*n,x+i*n,z);
88 if(i!=k)
89 {
90 edgeadd(x+i*n,y+(i+1)*n,0);
91 edgeadd(y+i*n,x+(i+1)*n,0);
92 }
93 }
94 }
95 dijikstra(st,ed);
96 }