hdu2686 费用流

Matrix

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1008    Accepted Submission(s): 560


Problem Description
Yifenfei very like play a number game in the n*n Matrix. A positive integer number is put in each area of the Matrix.
Every time yifenfei should to do is that choose a detour which frome the top left point to the bottom right point and than back to the top left point with the maximal values of sum integers that area of Matrix yifenfei choose. But from the top to the bottom can only choose right and down, from the bottom to the top can only choose left and up. And yifenfei can not pass the same area of the Matrix except the start and end. 
 

Input
The input contains multiple test cases.
Each case first line given the integer n (2<n<30) 
Than n lines,each line include n positive integers.(<100)
 

Output
For each test case output the maximal values yifenfei can get.
 

Sample Input
   
   
   
   
2 10 3 5 10 3 10 3 3 2 5 3 6 7 10 5 1 2 3 4 5 2 3 4 5 6 3 4 5 6 7 4 5 6 7 8 5 6 7 8 9
 

Sample Output
   
   
   
   
28 46 80
 

Author
yifenfei
 

Source
ZJFC 2009-3 Programming Contest
 

Recommend
yifenfei
 
每个点访问一次可以拆点
求最长路,可以把边取负数,运行最短路,结果去负即可
#include <iostream>
#include <cstring>
#include <cstdio>
#include <queue>
#define inf 99999999
using namespace std;
struct node
{
    int u,v,f,c;
};
node e[80000];
int first[2000],next[80000],d[2000],ans_cost,ans_flow;
int map[50][50],cc,p[2000],preedge[2000],inq[2000];
inline void add_edge(int u,int v,int f,int c)
{
    e[cc].u=u;
    e[cc].v=v;
    e[cc].f=f;
    e[cc].c=c;
    next[cc]=first[u];
    first[u]=cc;
    cc++;

    e[cc].v=u;
    e[cc].u=v;
    e[cc].f=0;
    e[cc].c=-c;
    next[cc]=first[v];
    first[v]=cc;
    cc++;
}
bool spfa(int s,int t)
{
    int i;
    for(i=0;i<=t;i++)
        d[i]=inf;
    memset(inq,0,sizeof(inq));
    d[s]=0;
    memset(p,-1,sizeof(p));
    memset(preedge,-1,sizeof(preedge));
    queue<int> q;
    q.push(s);
    while(!q.empty())
    {
        int u=q.front();
        q.pop();
        inq[u]=0;
        for(i=first[u];i!=-1;i=next[i])
        {
            if(e[i].f)
            {
                int v=e[i].v;
                if(d[v]>d[u]+e[i].c)
                {
                    d[v]=d[u]+e[i].c;
                    p[v]=u;
                    preedge[v]=i;
                    if(!inq[v])
                    {
                        inq[v]=1;
                        q.push(v);
                    }
                }
            }
        }
    }
    if(d[t]>=inf)
        return false;
    else
        return true;
}
void min_cost_flow(int s,int t)
{
    ans_flow=0,ans_cost=0;
    while(spfa(s,t))
    {
        int u=t;
        int mm=inf;
        while(p[u]!=-1)
        {
            mm=min(mm,e[preedge[u]].f);
            u=p[u];
        }
        u=t;
        while(p[u]!=-1)
        {
            e[preedge[u]].f-=mm;
            e[preedge[u]^1].f+=mm;
            u=p[u];
        }
        ans_flow+=mm;
        ans_cost+=mm*d[t];
    }
}
int main()
{
    int n;
    while(scanf("%d",&n)!=EOF)
    {
        int i,j;
        memset(first,-1,sizeof(first));
        memset(next,-1,sizeof(next));
        cc=0;
        for(i=0;i<n;i++)
        {
            for(j=0;j<n;j++)
            {
                scanf("%d",&map[i][j]);
                if((i==0&&j==0)||(i==n-1&&j==n-1))
                    add_edge(i*n+j,i*n+j+n*n,2,-map[i][j]);
                else
                    add_edge(i*n+j,i*n+j+n*n,1,-map[i][j]);
                if(j<n-1)
                    add_edge(i*n+j+n*n,i*n+j+1,1,0);
                if(i<n-1)
                    add_edge(i*n+j+n*n,(i+1)*n+j,1,0);
            }
        }
        int s=2*n*n+1;
        int t=2*n*n+2;
        add_edge(s,0,2,0);
        add_edge(2*n*n-1,t,2,0);
        min_cost_flow(s,t);
        printf("%d\n",-ans_cost-map[0][0]-map[n-1][n-1]); //因为00,与n-1,n-1 访问了两次,费用计算2次所以-
    }
    return 0;
}


你可能感兴趣的:(hdu2686 费用流)