Matrix Again (hdu3376)

Matrix Again

Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 102400/102400 K (Java/Others)
Total Submission(s): 28 Accepted Submission(s): 10
 
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
 
题目中是从起点走到终点,再走回起点,其实我们可以从起点走两次,这样会比较简单。
另外,此题的每个结点的流量限制是1,(起点跟终点是2)---》对于流量限制的结点,可以用拆点的方法。《挑战程序设计》中有讲解。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
#include <cmath>
using namespace std;

const int N = 1010;
const int INF = 0x3f3f3f3f;
int mpa[N][N];
int n;

struct edge
{
    int from, to, cap, cost, nex;
}ss[N*N*3];

int preve[N*N];
int dist[N*N], head[N*N];
int cnt;
bool used[N*N];

void add_edge(int from, int to, int cap, int cost)
{
    ss[cnt].from = from, ss[cnt].to = to, ss[cnt].cap = cap, ss[cnt].cost = cost, ss[cnt].nex = head[from], head[from] = cnt++;
    ss[cnt].from = to, ss[cnt].to = from, ss[cnt].cap = 0, ss[cnt].cost = -cost, ss[cnt].nex = head[to], head[to] = cnt++;
}

int min_cost_flow(int s, int t, int f)
{
    int res = 0;
    while(f > 0)
    {
        memset(used, 0, sizeof used);
        memset(dist, -1, sizeof dist);
        memset(preve, -1, sizeof preve);
        queue <int> que;
        que.push(s);
        dist[s] = 0;
        used[s] = true;

        while(! que.empty())
        {
            int v = que.front(); que.pop();

            for(int i = head[v]; i != -1; i = ss[i].nex)
            {
                edge &e = ss[i];
                if(e.cap > 0 && dist[e.to] < dist[v] + e.cost)
                {
                    dist[e.to] = dist[v] + e.cost;
                    preve[e.to] = i;
                    if(! used[e.to])
                    {
                        que.push(e.to);
                        used[e.to] = true;
                    }
                }
            }
            used[v] = false;
        }

        if(dist[t] == -1) return 0;

        int d = f;
        for(int i = preve[t]; i != -1; i = preve[ss[i].from])
            d = min(d, ss[i].cap);

        f -= d;
        res += d * dist[t];
        for(int i = preve[t]; i != -1; i = preve[ss[i].from])
        {
            ss[i].cap -= d;
            ss[i^1].cap += d;
        }
    }

    return res;
}

int main()
{
    while(~ scanf("%d", &n))
    {
        cnt = 0;
        memset(head, -1, sizeof head);
        for(int i = 0; i < n; i++)
            for(int j = 0; j < n; j++)
                scanf("%d", &mpa[i][j]);

        for(int i = 0; i < n; i++)
            for(int j = 0; j < n; j++)
            {
                if((i == 0 && j == 0) || (i == n - 1 && j == n - 1))
                    add_edge(i * n + j, i * n + j + n * n, 2, 0);
                else
                    add_edge(i * n + j, i * n + j + n * n, 1, 0);

                if(i != n - 1) add_edge(i * n + j + n * n, (i+1) * n + j, 1, mpa[i+1][j]);
                if(j != n - 1) add_edge(i * n + j + n * n, i * n + j + 1, 1, mpa[i][j+1]);
            }
        printf("%d\n", min_cost_flow(0, n * n - 1 + n * n, 2) + mpa[0][0] - mpa[n-1][n-1]);
    }

    return 0;
}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值