【HDU 3376 Matrix Again】网络流 & 拆点 & 最大费用最大流

Matrix Again

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

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

Source
HDOJ Monthly Contest – 2010.04.04

题意 : 从 (1,1) 到 (n,n)只能往右或往下走,然后再从(n,n)走到(1,1),不能重复走,走过的格子的数字最大和是多少

思路 : 从 (1,1) 到 (n,n)再从(n,n)到(1,1),相当于从(1,1)到(n,n)找到两条不重复的路,把每个格子拆分成左右两个点,一个入点,一个出点

源点s -> (1,1)入点 流为 2 -> (1,1)出点 流为 2 -> (1,2)入点流为 1 -> (1,2)出点 流为 1 -> …….-> (n,n)入点 -> (n,n)出点 流为 2 -> 汇点 e 流为 2

AC代码:

#include<cstdio>
#include<cmath>
#include<deque>
#include<queue>
#include<vector>
#include<cstring>
#include<algorithm>
using namespace std;
const int MAX = 8e5 + 10;
const int INF = (1e9 + 7) / 2;
typedef long long LL;
int head[MAX],nl;
struct node{
    int from,to,cost,flow,next;
}st[MAX * 5];
void add(int x,int y,int z,int a){
    st[nl] = node{x,y,z,a,head[x]},head[x] = nl++;
    st[nl] = node{y,x,-z,0,head[y]},head[y] = nl++;
}
void init(){
    nl = 0;
    memset(head,-1,sizeof head);
}
int vis[MAX],p[MAX],w[MAX];
bool DFS(int s,int t){
    memset(vis,0,sizeof vis);
    memset(w,-1,sizeof w);
    fill(p,p + MAX,-INF);
    p[s] = 0,vis[s] = 1;
    queue<int> q;
    q.push(s);
    while(!q.empty()){
        int o = q.front();
        q.pop();
        vis[o] = 0;
        for(int i = head[o]; i != -1; i = st[i].next){
            int a = st[i].to;
            if(p[a] < p[o] + st[i].cost && st[i].flow){
                p[a] = p[o] + st[i].cost;
                w[a] = i;
               if(!vis[a]){
                    vis[a] = 1;
                    q.push(a);
               }
            }
        }
    }
    return w[t] != -1;
}
int Mincost(int s,int t){
    int Maxflow = 0,cost = 0;
    while(DFS(s,t)){
        int flow = INF;
        for(int i = w[t]; i != -1; i = w[st[i].from]){
            if(flow > st[i].flow)
                flow = st[i].flow;
        }
        for(int i = w[t]; i != -1; i = w[st[i].from]){
            st[i].flow -= flow;
            st[i ^ 1].flow += flow;
            cost += flow * st[i].cost;
        }
        Maxflow += flow;
    }
    return cost;
}
int z[666][666],n;
int main()
{
    while(~scanf("%d",&n)){
        init();
        int t = n * n,s = 0,e = n * n * 2 + 1;
        add(s,1,0,2);
        add(t * 2,e,0,2);
        for(int i = 1; i <= n; i++)
            for(int j = 1; j <= n; j++){
                scanf("%d",&z[i][j]);
                int a = (i - 1) * n + j;
                if(i == j && (i == 1 || i == n))
                    add(a,a + t,z[i][j],2);
                else
                    add(a,a + t,z[i][j],1);
                if(i + 1 <= n)
                    add(a + t,a + n,0,1);
                if(j + 1 <= n)
                    add(a + t,a + 1,0,1);
            }
        printf("%d\n",Mincost(s,e) - z[n][n] - z[1][1]);
    }
    return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值