zoukankan      html  css  js  c++  java
  • 太空飞行计划

    网络流与线性规划24题第二题。

    题解中算法很明显,但是有一个地方不完美,就是方案,数据中存在这样的情况,对于一个最优方案,再加上一个实验,总收益不变,数据就把这个实验也算在内了。

    所以输出时不是和s相连的点,而是与t不连通的点。

    View Code
      1 program fly(input,output);
      2 const
      3    oo = 19950714;
      4 var
      5    dist    : array[0..500] of longint;
      6    f    : array[0..500,0..500] of longint;
      7    q    : array[0..2000] of longint;
      8    v    : array[0..400] of boolean;
      9    n,m    : longint;
     10    w    : array[0..200] of longint;
     11    cost    : array[0..200] of longint;
     12    s,t    : longint;
     13    flow    : longint;
     14 procedure init;
     15 var
     16    x,i : longint;
     17 begin
     18    readln(m,n);
     19    s:=0;
     20    t:=m+n+2;
     21    for i:=1 to m do
     22    begin
     23       read(w[i]);
     24       f[s,i]:=w[i];
     25       f[i,s]:=0;
     26       while not eoln do
     27       begin
     28      read(x);
     29      f[i,m+x]:=oo;
     30      f[m+x,i]:=0;
     31       end;
     32       readln;
     33    end;
     34    for i:=1 to n do
     35    begin
     36       read(cost[i]);
     37       f[m+i,t]:=cost[i];
     38       f[t,m+i]:=0;
     39    end;
     40    for i:=s to t do
     41       f[i,i]:=0;
     42 end; { init }
     43 procedure swap(var aa,bb :longint );
     44 var
     45    tt : longint;
     46 begin
     47    tt:=aa;
     48    aa:=bb;
     49    bb:=tt;
     50 end; { swap }
     51 function bfs(now: longint ):boolean;
     52 var
     53    head,tail : longint;
     54    i         : longint;
     55 begin
     56    head:=0;
     57    tail:=1;
     58    for i:=s to t do
     59       dist[i]:=-1;
     60    dist[now]:=0;
     61    q[1]:=now;
     62    while head<tail do
     63    begin
     64       inc(head);
     65       for i:=s to t do
     66      if f[q[head],i]>0 then
     67         if dist[i]=-1 then
     68         begin
     69            dist[i]:=dist[q[head]]+1;
     70            inc(tail);
     71            q[tail]:=i;
     72         end;
     73    end;
     74    if dist[t]=-1 then
     75       exit(false);
     76    exit(true);
     77 end; { bfs }
     78 function min(aa,bb :longint ):longint;
     79 begin
     80    if aa<bb then
     81       exit(aa);
     82    exit(bb);
     83 end; { min }
     84 function dfs(now,minflow: longint ):longint;
     85 var
     86    i   : longint;
     87    tmp : longint;
     88 begin
     89    if now=t then
     90       exit(minflow);
     91    for i:=s to t do
     92       if f[now,i]>0 then
     93      if dist[i]=dist[now]+1 then
     94      begin
     95         tmp:=dfs(i,min(minflow,f[now,i]));
     96         if tmp<>0 then
     97         begin
     98            inc(f[i,now],tmp);
     99            dec(f[now,i],tmp);
    100            exit(tmp);
    101         end;
    102      end;
    103    exit(0);
    104 end; { dfs }
    105 procedure main;
    106 var
    107    tmp,i,j : longint;
    108    sum       : longint;
    109 begin
    110    flow:=0;
    111    while bfs(s) do
    112    begin
    113       tmp:=dfs(s,oo);
    114       while tmp<>0 do
    115       begin
    116      inc(flow,tmp);
    117      tmp:=dfs(s,oo);
    118       end;
    119    end;
    120    for i:=s to t-1 do
    121       for j:=i+1 to t do
    122      swap(f[i,j],f[j,i]);
    123    bfs(t);
    124    fillchar(v,sizeof(v),true);
    125    for i:=1 to m do
    126       if dist[i]<>-1 then
    127      v[i]:=false;
    128    for i:=1 to m do
    129       if v[i] then
    130      write(i,' ');
    131    writeln;
    132    fillchar(v,sizeof(v),true);
    133    for i:=1 to n do
    134       if dist[i+m]<>-1 then
    135      v[i]:=false;
    136    for i:=1 to n do
    137       if v[i] then
    138      write(i,' ');
    139    writeln;
    140    sum:=0;
    141    for i:=1 to m do
    142       inc(sum,w[i]);
    143    writeln(sum-flow);
    144 end; { main }
    145 begin
    146    assign(input,'fly.in');reset(input);
    147    assign(output,'fly.out');rewrite(output);
    148    init;
    149    main;
    150    close(input);
    151    close(output);
    152 end.
  • 相关阅读:
    活动安排问题
    喵哈哈村的魔法考试 Round #5 (Div.2) C
    梯度下降,牛顿法 ,高斯牛顿法
    SSD模型解析
    训练较深的卷积神经网络时遇到的问题
    手写体识别
    Fast Patch-based Style Transfer of Arbitrary Style 理解
    多任务学习
    迁移学习(训练数据少的可怜时的办法)
    通过训练得出的结果修改模型
  • 原文地址:https://www.cnblogs.com/neverforget/p/2442794.html
Copyright © 2011-2022 走看看