1 solutions

  • 0
    @ 2024-12-10 23:09:26

    Pascal :

    const dt=1000000;
    type point=^node;
     node=record
       value:longint;
       link:point;
     end;
    var n,m,i,j,ans,head,now,tail,pere,all:longint;
    cost:array[1..dt] of longint;
    color,vis:array[1..dt] of boolean;
    a:array[1..dt+1000] of node;  //思考+1000的用意
    deq:array[1..dt+1000] of longint;
    s,q,p,t:point;
    function min(a,b:longint):longint;
    begin
      if a>b then exit(b) else exit(a);
    end;
    procedure add(x,y:longint);
    begin
      if a[x].link=nil then
        begin
          new(p);
          a[x].link:=p;
          p^.value:=y;
          p^.link:=nil;
        end
    else
            begin
              s:=a[x].link;
              while s <>nil do
                begin
                  q:=s;
                  s:=s^.link;
                end;
               new(p);
               p^.value:=y;
               p^.link:=nil;
               q^.link:=p;
            end;
    end;
    procedure init;
    var i,x,y:longint;
    begin
      all:=0;
      readln(n,m);
      for i:=1 to n do
        begin
          new(q);
          a[i].value:=i;
          a[i].link:=nil;
        end;
      for i:=1 to n do
        begin
        read(cost[i]);
        inc(all,cost[i]);
        end;
      for i:=1 to m do
        begin
          read(x,y);
          add(x,y);
          add(y,x);
        end;
    end;
    function bfs(state:boolean):boolean;
    var i:longint; r:point;
    begin
      fillchar(color,sizeof(color),false);
      fillchar(vis,sizeof(vis),false);
      bfs:=true;
      ans:=0;
      head:=1;
      tail:=1;
      deq[head]:=1;
      color[1]:=state;
      if state then inc(ans,cost[1]);
      vis[1]:=true;
        while head<=tail do
          begin
            now:=deq[head];
            r:=a[now].link;
            while r<>nil do
              begin
                if not vis[r^.value] then
                  begin
                    case color[now] of
                      true: color[r^.value]:=false;
                      false:
                        begin
                        color[r^.value]:=true;
                        inc(ans,cost[r^.value]);
                        end;
                    end;
                    inc(tail);
                    deq[tail]:=r^.value;
                    vis[r^.value]:=true;
                  end
         else
     if not (color[now] xor color[r^.value]) then exit(false);
                r:=r^.link;
              end;
        inc(head);
        end;
    end;
    procedure print;
    begin
      writeln('NO');
    end;
    begin
    //assign(input,'Ezio.in');reset(input);
    //assign(output,'Ezio.out');rewrite(output);
    init;
     pere:=0;
     ans:=0;
    if not bfs(true) then print
    else writeln(min(ans,all-ans));
    //close(input);close(output);
    end.
    
    
    • 1

    Information

    ID
    574
    Time
    10000ms
    Memory
    128MiB
    Difficulty
    (None)
    Tags
    # Submissions
    0
    Accepted
    0
    Uploaded By