P4013 digital keystone minimum cost maximum flow problem

  

Title Description

Given one of  n number of digits n lines trapezoidal as shown in FIG.

The first line has a trapezoidal  m m digits. From the top of the trapezoid  m starts m digits, each digit of the can at the mobile, a path is formed from the top to the bottom of the trapezoid along the lower left or lower right.

They are subject to the following rules:

  1. From top to bottom trapezoid  m path disjoint m pieces;

  2. From top to bottom trapezoid  m intersecting paths m only at a digital node;

  3. From the top to the bottom of the trapezoid  m m paths allows digital node or intersection edges intersect.

Input and output formats

Input formats:

 

The first  1 row 1 has  2 two positive integers  m m and  n- n-, respectively a first row of numbers with a trapezoid  m m numbers, total  n- n-row. The next  n- n-digit number is a row for each row in the trapezoid.

First  a row with a 1  m m digits, the  2 line 2 has  m + 1 m + 1 numbers, and so on.

 

Output formats:

 

According to the rules  . 1 1, Rule  2 2 and Rule  3 maximum digital sum calculated and output 3, a maximum sum of each row.

 

Sample input and output

Input Sample # 1:  Copy
2 5
2 3
3 4 5
9 10 9 1
1 1 10 1 1
1 1 10 12 1 1
Output Sample # 1:  Copy
66
75
77

Explanation

 

 

Attention to detail is quite easy to build map

Re-built three times out of three points QAQ

#include<bits/stdc++.h>
using namespace std;
//input by bxd
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define repp(i,a,b) for(int i=(a);i>=(b);--i)
#define RI(n) scanf("%d",&(n))
#define RII(n,m) scanf("%d%d",&n,&m)
#define RIII(n,m,k) scanf("%d%d%d",&n,&m,&k)
#define RS(s) scanf("%s",s);
#define ll long long
#define pb push_back
#define inf 0x3f3f3f3f
#define CLR(A,v)  memset(A,v,sizeof A)
//////////////////////////////////
const int N=10000001;

int n,m,S,T,maxflow,mincost,last[N],pre[N],dis[N],flow[N];
bool vis[N];
struct Edge{
    int next,to,flow,dis;
}edge[N<<1];
int pos=1,head[N];
void init()
{
    pos=1;
    CLR(head,0);
    mincost=maxflow=0;
}
queue <int> q;

void add(int from,int to,int flow,int dis)
{
    edge[++pos].next=head[from];
    edge[pos].flow=flow;
    edge[pos].dis=dis;
    edge[pos].to=to;
    head[from]=pos;
    edge[++pos].next=head[to];
    edge[pos].flow=0;
    edge[pos].dis=-dis;
    edge[pos].to=from;
    head[to]=pos;
}
bool spfa(int s,int t)
{
    CLR(dis,0x3f);
    CLR(flow,0x3f);
    CLR(vis,0);
    while (!q.empty()) q.pop();
    dis[s]=0; pre[t]=-1; q.push(s); vis[s]=1;
    int tot=0;
    while (!q.empty())
    {
        int now=q.front(); q.pop(); vis[now]=0;
        for (int i=head[now]; i; i=edge[i].next)
        {
            int to=edge[i].to;
            if  (edge[i].flow>0 && dis[to]>dis[now]+edge[i].dis)
            {
                dis[to]=edge[i].dis+dis[now];
                flow[to]=min(edge[i].flow,flow[now]);
                last[to]=i;
                pre[to]=now;
                if (!vis[to])
                {
                    q.push(to); vis[to]=1;
                }
            }
        }
    }
    return pre[t]!=-1;
}
void MCMF(int s,int t)
{
    while (spfa(s,t))
    {
        int now=t;
        maxflow+=flow[t];
        mincost+=flow[t]*dis[t];
        while (now!=s)
        {
            edge[last[now]].flow-=flow[t];
            edge[last[now]^1].flow+=flow[t];
            now=pre[now];
        }
    }
}
int s,t,mp[20][200],id[20][200],cnt=3;

int main()
{
    s=0,t=1;
    RII(n,m);


    rep(i,1,m)
    rep(j,1,n+i-1)
    {
        RI(mp[i][j]);id[i][j]=++cnt;
    }

    int T=cnt;

    rep(i,1,m)
    rep(j,1,n+i-1)
    {
        add(id[i][j],id[i][j]+T,1,-mp[i][j]);
        if(i==m)continue;
        add(id[i][j]+T,id[i+1][j],1,0);
        add(id[i][j]+T,id[i+1][j+1],1,0);
    }
    rep(i,1,n)add(s,id[1][i],1,0);
    rep(i,1,n+m-1)add(id[m][i]+T,t,1,0);
    MCMF(s,t);
    cout<<-mincost<<endl;

    init();
    rep(i,1,m)
    rep(j,1,n+i-1)
    {
        add(id[i][j],id[i][j]+T,n,-mp[i][j]);
        if(i==m)continue;
        add(id[i][j]+T,id[i+1][j],1,0);
        add(id[i][j]+T,id[i+1][j+1],1,0);
    }
    rep(i,1,n)add(s,id[1][i],1,0);
    rep(i,1,n+m-1)add(id[m][i]+T,t,n,0);
    MCMF(s,t);
    cout<<-mincost<<endl;

    init();
    rep(i,1,m)
    rep(j,1,n+i-1)
    {
        add(id[i][j],id[i][j]+T,n,-mp[i][j]);
        if(i==m)continue;
        add(id[i][j]+T,id[i+1][j],n,0);
        add(id[i][j]+T,id[i+1][j+1],n,0);
    }
    rep(i,1,n)add(s,id[1][i],1,0);
    rep(i,1,n+m-1)add(id[m][i]+T,t,n,0);
    MCMF(s,t);
    cout<<-mincost<<endl;

}
View Code

 

Guess you like

Origin www.cnblogs.com/bxd123/p/10944524.html