CODEVS 1033 Earthworm Game Problem

Keywords: Programming less

Topic Description

On a trapezoidal field, a group of earthworms are playing a game of gathering food. Earthworms collect food in trapezoidal fields as follows:
a(1,1) a(1,2)…a(1,m)
a(2,1) a(2,2) a(2,3)…a(2,m) a(2,m+1)
a(3,1) a (3,2) a(3,3)…a(3,m+1) a(3,m+2)
……
a(n,1) a(n,2) a(n,3)… a(n,m+n-1)
They divide the food into n rows. The first row contains M piles of food. The amount of food in each pile is a(1,1),a(1,2), respectively. a(1,m);
In the second line, there are m+1 piles of food. The amount of food in each pile is a(2,1),a(2,2), and ___________ a(2,m+1); the following are m+2 heap, m+3 heap,... m+n-1 pile of food.
Now the earthworms choose k earthworms to test their cooperation ability (1 < K < m). The test method is as follows: Article 1 Earthworms select a pile of food from the first row, then climb left or right down, and collect a pile of food, such as from a (1,2) can only climb to a(2,2) or a(2,3), but can not climb to other places. Next, climb down to collect a pile of food until line n collects a pile of food. Article 1 The amount of food collected by earthworms is the sum of the amount of food they collect i n each row; Article 2 also crawls from the first row to the n row, collecting a pile of food i n each row. The method of crawling is similar to that of Article 1 earthworms, but can not encounter the trajectory of Article 1 earthworms; Generally, Article I earthworms crawl from the first row to the n row, collecting the first pile of food i n each row, and the method of crawling is similar to that of Article 1 earthworms. Similarly, we can't touch the track of the first I-1 earthworm. How should these K earthworms cooperate to maximize the total amount of food they collect? The total amount of food collected represents the level of cooperation of these K earthworms.
Bah programming tasks:
Given the above trapezoidal values of m, N and k (1 < k < m < 30; 1 < n < 30) and the amount of food in each stack of trapezoidal (non-integer less than 10), the program calculates the maximum amount of food that the k earthworms can collect.
Input Description
The input data is provided by a text file named INPUT1. * with a total of n+1 rows. The data in each row is separated by a space.
The first line is the values of n, m and k.
The next n rows are the food quantity a(i,1), a(i,2),... a(i,m+i-1), i=1,2,... N.

Output Description

At the end of the program, the maximum amount of food that can be collected by k-worms is output on the screen.

Sample Input

3 2 2
1 2
5 0 2
1 10 0 6

Sample Output

26

Analysis

The cost flow is actually a higher-level square number.

Code

#include <bits/stdc++.h>
using namespace std;
const int inf=0x3f3f3f3f,maxn=40,maxN=5000;
struct edge{int from,v,flow,cost;}e[150000];
int n,m,k,tot=1,first[maxN],p[maxn][maxn*2],q[1010],d[maxN],N,S,T,S_;
long long ans;
bool vis[maxN];
void insert(int u,int v,int flow,int cost)
{
    tot++;e[tot].v=v;e[tot].flow=flow;e[tot].cost=cost;e[tot].from=first[u];first[u]=tot;
    tot++;e[tot].v=u;e[tot].flow=0;e[tot].cost=-cost;e[tot].from=first[v];first[v]=tot;
}
bool spfa()
{
    memset(d,0x3f,(N+N+3)*4);
    memset(vis,0,N+N+3);
    int head=0,tail=1;q[0]=T;
    vis[T]=1;d[T]=0;
    while(head!=tail)
     {
         int x=q[head++];if(head>=1001)head=0;
         for(int i=first[x];i;i=e[i].from)
          if(e[i^1].flow&&e[i^1].cost+d[x]<d[e[i].v])
           {
               d[e[i].v]=d[x]+e[i^1].cost;
               if(!vis[e[i].v])
                {
                    vis[e[i].v]=1;q[tail++]=e[i].v;
                    if(tail>=1001)tail=0;
                }
           }
         vis[x]=0;
     }
    return d[S]<inf;
}
int dfs(int x,int a)
{
    vis[x]=1;
    if(x==T||a==0)return a;
    int flow=0,f;
    for(int i=first[x];i;i=e[i].from)
     if(!vis[e[i].v]&&d[e[i].v]+e[i].cost==d[x]&&(f=dfs(e[i].v,min(a,e[i].flow)))>0)
      {
          e[i].flow-=f;
          e[i^1].flow+=f;
          ans+=e[i].cost*f;
          a-=f;
          flow+=f;
          if(a==0)break;
      }
    return flow;
}
int main()
{
    scanf("%d%d%d",&n,&m,&k);
    N=0;
    for(int i=1;i<=n;i++)
     {
         for(int j=1;j<=m+i-1;j++)
          {
              N++;
              p[i][j]=N;
          }
     }
    S=0,S_=N+N+1,T=N+N+2;
    for(int i=1;i<=n;i++)
     {
         for(int j=1;j<=m+i-1;j++)
          {
              int x;
              scanf("%d",&x);
              insert(p[i][j],p[i][j]+N,1,-x);
              if(i<n)
               {
                   insert(p[i][j]+N,p[i+1][j],1,0);
                   insert(p[i][j]+N,p[i+1][j+1],1,0);
               }
          }
     }
    insert(S,S_,k,0);
    for(int i=1;i<=m;i++)insert(S_,i,1,0);
    for(int i=1;i<=m+n-1;i++)insert(p[n][i]+N,T,1,0);
    ans=0;
    while(spfa())
     {
         memset(vis,0,N+N+3);
        dfs(S,inf); 
     }
    printf("%lld",-ans);
    return 0;
}

Posted by nareshrevoori on Mon, 22 Apr 2019 00:18:34 -0700