Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

spfa判负权环47ms。贴pascal代码

Posted by zyd20001025 at 2015-08-06 13:56:40 on Problem 3259
procedure main;
  var n,m,t,x,y,z,num,i:longint;
      u,v,w,next,first:array[0..10000] of longint;

  procedure addedge(x,y,z:longint);
    begin
    inc(num);
    u[num]:=x;v[num]:=y;w[num]:=z;
    next[num]:=first[x];
    first[x]:=num;
    end;

  function spfa:boolean;
    var q:array[0..10000] of longint;
        dist:array[0..10000] of longint;
        visited:array[0..10000] of boolean;
        time:array[0..10000] of longint;
        f,r,x,e:longint;
    begin
    fillchar(dist,sizeof(dist),127);
    fillchar(visited,sizeof(visited),false);
    fillchar(time,sizeof(time),0);
    f:=0;r:=1;
    q[r]:=0;visited[0]:=true;
    time[0]:=1;dist[0]:=0;
    while f<>r do
      begin
      f:=(f+1) mod 10000;
      x:=q[f];visited[x]:=false;
      e:=first[x];
      while e<>-1 do
        begin
        if dist[x]+w[e]<dist[v[e]] then
          begin
          dist[v[e]]:=dist[x]+w[e];
          if not visited[v[e]] then
            begin
            r:=(r+1) mod 10000;
            q[r]:=v[e];visited[q[r]]:=true;
            inc(time[q[r]]);
            if time[q[r]]>6 then exit(true);
            end;
          end;
        e:=next[e];
        end;
      end;
    exit(false);
    end;

  begin
  readln(n,m,t);
  num:=0;
  fillchar(first,sizeof(first),255);
  for i:=1 to m do
    begin
    readln(x,y,z);
    addedge(x,y,z);
    addedge(y,x,z);
    end;
  for i:=1 to t do
    begin
    readln(x,y,z);
    addedge(x,y,-z);
    end;
  for i:=1 to n do
    addedge(0,i,0);
  if spfa then writeln('YES') else writeln('NO');
  end;
////////////////////////////////////////////////////////
var t:longint;
begin
readln(t);
while t<>0 do
  begin
  main;
  dec(t);
  end;
end.

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator