[24 PowerOJ1753 & network flow distribution problems] (KM)

Meaning of the questions:

 

 Ideas: Cost Flow do

The best algorithm is KM board

  1 #include<bits/stdc++.h>
  2 using namespace std;
  3 typedef long long ll;
  4 typedef unsigned int uint;
  5 typedef unsigned long long ull;
  6 typedef long double ld;
  7 typedef pair<int,int> PII;
  8 typedef pair<ll,ll> Pll;
  9 typedef vector<int> VI;
 10 typedef vector<PII> VII;
 11 typedef pair<ll,ll>P;
 12 #define N  500
 13 #define M  1000000
 14 #define INF 1e9
 15 #define fi first
 16 #define se second
 17 #define MP make_pair
 18 #define pb push_back
 19 #define pi acos(-1)
 20 #define mem(a,b) memset(a,b,sizeof(a))
 21 #define rep(i,a,b) for(int i=(int)a;i<=(int)b;i++)
 22 #define per(i,a,b) for(int i=(int)a;i>=(int)b;i--)
 23 #define lowbit(x) x&(-x)
 24 #define Rand (rand()*(1<<16)+rand())
 25 #define id(x) ((x)<=B?(x):m-n/(x)+1)
 26 #define ls p<<1
 27 #define rs p<<1|1
 28 
 29 const ll MOD=1e9+7,inv2=(MOD+1)/2;
 30       double eps=1e-6;
 31       int dx[4]={-1,1,0,0};
 32       int dy[4]={0,0,-1,1};
 33 
 34 int lx[N],ly[N],lk[N],slk[N],pre[N],vy[N],w[N][N],
 35     py,n,m,x,y,i,j,d,p;
 36 
 37 int read()
 38 {
 39    int v=0,f=1;
 40    char c=getchar();
 41    while(c<48||57<c) {if(c=='-') f=-1; c=getchar();}
 42    while(48<=c&&c<=57) v=(v<<3)+v+v+c-48,c=getchar();
 43    return v*f;
 44 }
 45 
 46 void solve1()
 47 {
 48     rep(i,1,n)
 49      rep(j,1,n) w[i][j]=-w[i][j];
 50     rep(i,1,n)
 51     {
 52         rep(j,1,n) slk[j]=INF,vy[j]=0;
 53         for(lk[py=0]=i;lk[py];py=p)
 54          {
 55              v [p] = 1 ; d = INF; x = lk [p];
56              rep (and 1 , n)
 57               if (! V [y])
 58               {
 59                   if (lx [x] + L [y] -w [x] [y] <clk [and]) clk [and] lx = [x] + l [y] -w [x] [y], pre [y] = p;
60                   if (clk [and] <d) d = clk [y], p = y;
61               }
 62              rep (and, 0 , n)
 63               if (v [and]) lx [lk [and]] - = d, l [y] + = d;
64                else clk [and] - = d;
65 
 66         }
 67         for(;py;py=pre[py]) lk[py]=lk[pre[py]];
 68     }
 69     ll ans=0;
 70     rep(i,1,n) ans+=lx[i]+ly[i];
 71     printf("%lld\n",-ans);
 72 }
 73 
 74 void solve2()
 75 {
 76     mem(lx,0);
 77     mem(ly,0);
 78     mem(lk,0);
 79     rep(i,1,n)
 80      rep(j,1,n) w[i][j]=-w[i][j];
 81     rep(i,1,n)
 82     {
 83         rep(j,1,n) slk[j]=INF,vy[j]=0;
 84         for(lk[py=0]=i;lk[py];py=p)
 85         {
 86             vy[py]=1; d=INF; x=lk[py];
 87             rep(y,1,n)
 88              if(!vy[y])
 89              {
 90                  if(lx [x] + L [y] -w [x] [y] <clk [and]) clk [y] = lx [x] + L [y] -w [x] [y], pre [and ] = py;
91                   if (clk [and] <d) d = clk [y], p = y;
92               }
 93              rep (and, 0 , n)
 94               if (v [y]) lx [lk [and]] - = d, l [y] + = d;
95                else clk [and] - = d;
96  
97          }
 98          for (; py; py = pre [p]) lk [p] = lk [pre [p]];
99      }
 100      ll ans = 0 ;
101      rep (i, 1 , n) ans = lx + [i] + L [i];
102      printf ("%lld\n",ans);
103 }
104 
105 
106 int main()
107 {
108     //freopen("1.in","r",stdin);
109     n=read();
110     rep(i,1,n)
111      rep(j,1,n) w[i][j]=read();
112     solve1();
113     solve2();
114 }

 

Guess you like

Origin www.cnblogs.com/myx12345/p/11765338.html