内容目录
P2079 - 防御机制From tangjz Normal (OI) |
|||||||||
---|---|---|---|---|---|---|---|---|---|
|
这题就是Spfa.
Program defence; const maxn=90000; maxm=500000; inf=2139062143; type dis_arr=record s:longint; d:array[1..maxn] of longint; end; var n,m,i,j:longint; w:array[1..maxn] of longint; head,edge,next,weight:array[1..maxm] of longint; size:longint; d1,d2:dis_arr; queue:array[1..6000000] of longint; Procedure addedge(u,v,w:longint); begin inc(size); edge[size]:=v; weight[size]:=w; next[size]:=head[u]; head[u]:=size; end; Procedure addedge_main; var u,v,w:longint; begin read(u,v,w); addedge(u,v,w);addedge(v,u,w); end; Procedure spfa(var d:dis_arr); var i,j,now,p:longint; begin i:=1;j:=1;queue[1]:=d.s; fillchar(d.d,sizeof(d.d),127);d.d[d.s]:=0; while (i<=j) do begin now:=queue[i]; p:=head[now]; while (p<>0) do begin if (d.d[now]+weight[p]<d.d[edge[p]]) then begin inc(j); queue[j]:=edge[p]; d.d[edge[p]]:=d.d[now]+weight[p]; end; p:=next[p]; end; inc(i); end; end; Procedure print; var i,j:longint; ans:int64; flag:boolean; begin flag:=false; ans:=0; for i:=2 to n-1 do begin if d1.d[i]<>inf then ans:=ans+int64(w[i]+d1.d[i]); if (d1.d[i]>d2.d[i]) then begin if not(flag) then begin flag:=true; writeln('No'); end else write(' '); write(i); end; end; if not(flag) then begin writeln('Yes'); writeln(ans); end else writeln; end; begin // assign(input,'defence.in'); // reset(input); fillchar(head,sizeof(head),0); fillchar(edge,sizeof(edge),0); fillchar(next,sizeof(next),0); size:=0; read(n,m); d1.s:=1;d2.s:=n; for i:=2 to n-1 do read(w[i]); for i:=1 to m do begin addedge_main; end; spfa(d1);spfa(d2); print; end.