HDU3376:Matrix Again(最大费用最大流)

Matrix Again

Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 102400/102400 K (Java/Others)
Total Submission(s): 4425    Accepted Submission(s): 1288


Problem Description
Starvae 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 starvae should to do is that choose a detour which from 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 starvae 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 starvae can not pass the same area of the Matrix except the start and end..
Do you know why call this problem as “Matrix Again”? AS it is like the problem 2686 of HDU.
 

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

Output
For each test case output the maximal values starvae 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
Starvae

题意:从左上到右下再回到左上,每个点除左上右下只可以走一次,问最大值,

思路:拆点+费用流即可,左上和右下容量为2就行,事实证明...用数组模拟队列容易出事。

# include <iostream>
# include <cstdio>
# include <queue>
# include <cstring>
# include <algorithm>
using namespace std;
const int INF= 0x3f3f3f3f;
const int maxn = 720010;
int n, cnt=0, Next[maxn], dis[maxn], vis[maxn], pre[maxn];
int a[603][603], source, sink;;
struct node
{
    int u, v, w, c, next;
    node(){}
    node(int u, int v, int w, int c, int next):u(u),v(v),w(w),c(c),next(next){}
}edge[3501000];
inline void scan(int &ret)
{
    char c; ret=0;
    while((c=getchar())<'0'||c>'9');
    while(c>='0'&&c<='9') ret=ret*10+(c-'0'),c=getchar();
}
inline void out(int x)
{
   if(x>9) out(x/10);
   putchar(x%10+'0');
}
void add(int u, int v, int w, int c)
{
    edge[cnt] = node(u,v,w,c,Next[u]);
    Next[u] = cnt++;
    edge[cnt] = node(v,u,0,-c,Next[v]);
    Next[v] = cnt++;
}
bool spfa()
{
    memset(vis, 0, sizeof(vis));
    memset(dis, INF, sizeof(dis));
    int l=0, r=0;
    dis[source] = 0;
    vis[source] = 1;
    queue<int>q;
    q.push(source);
    while(!q.empty())
    {
        int u=q.front();
        q.pop();
        vis[u] = 0;
        for(int i=Next[u]; i!=-1; i=edge[i].next)
        {
            int v=edge[i].v, w=edge[i].w, c=edge[i].c;
            if(w>0 && dis[v] > dis[u]+c)
            {
                dis[v] = dis[u] + c;
                pre[v] = i;
                if(!vis[v])
                {
                    q.push(v);
                    vis[v] = 1;
                }
            }
        }
    }
    return dis[sink] != INF;
}
int full = 0;
int mcmf()
{
    int cost = 0;
    while(spfa())
    {
        int imin = INF;
        for(int i=sink; i!=source; i=edge[pre[i]].u) imin = min(imin, edge[pre[i]].w);
        for(int i=sink; i!=source; i=edge[pre[i]].u)
        {
            edge[pre[i]].w -= imin;
            edge[pre[i]^1].w += imin;
        }
        cost += imin*dis[sink];
        full += imin;
    }
    return cost;
}
int main()
{
    while(~scanf("%d",&n))
    {
        memset(Next, -1, sizeof(Next));
        cnt = 0;
        for(int i=1; i<=n; ++i)
            for(int j=1; j<=n; ++j)
            {
                scan(a[i][j]);
                add((i-1)*n+j, (i+n-1)*n+j, 1, -a[i][j]);
                if(i != n) add((i+n-1)*n+j, i*n+j, 1, 0);
                if(j != n) add((i+n-1)*n+j, (i-1)*n+j+1, 1, 0);
            }
        add(1, n*n+1, 1, 0);
        add((n-1)*n+n, (2*n-1)*n+n, 1, 0);
        source = 1, sink = (2*n-1)*n+n;
        out(-mcmf());
        puts("");
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值