hdu 2686 Matrix(最大费用流)

46 篇文章 0 订阅

Matrix

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


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
题意:给出一个矩阵,每个点都有一个权值。找出一条路径:从最左上角的点到最右下角的点,再从最右下角的点到最左上角的点,使途中经过的点的权值总和最大。并且除了最左上角和最右下角的点,每个点只能经过一次,最左上角的点和最右下角的点的权值只算一次。
思路:其实就是找出最左上角点到最右下角点的两条不相交的路径,路径上的权值总和要最大。可以用dp做,这里用了最大费用流。
因为每个点只能经过一次,所以将每个点拆点为i和i'。每个i连一条边到i',费用为该点的权值,容量为1。
每个点的i'向右面和下面的点j各连一条边,费用为0,容量为1。
因为最左上角的点和最右下角的点要经过两次,所以源点s不能连边到1,应连到1',费用为0,容量为2(因为要求两条路径)。同理,点n*n连一条边到汇点t,费用为0,容量为2。
最后求最大费用流即可,最大费用加上 最左上角的点和最右下角的点的权值即为答案。
AC代码:
#include <iostream>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <queue>
#include <ctime>
#include <algorithm>
#define ll __int64

using namespace std;

const int INF = 1000000000;
const int maxn = 2000;

struct Edge{
    int u, v, cost, cap, flow, next;
}et[maxn * maxn];
int pre[maxn], dis[maxn], eh[maxn], low[maxn], G[35][35];
bool vis[maxn];
int s, t, num, anscost, n;
bool ok(int x, int y){
    if(x >= 1 && x <= n && y >= 1 && y <= n) return true;
    return false;
}
void init(){
    memset(eh, -1, sizeof(eh));
    num = 0;
}
void add(int u, int v, int cost, int cap, int flow){
    Edge e = {u, v, cost, cap, flow, eh[u]};
    et[num] = e;
    eh[u] = num++;
}
void addedge(int u, int v, int cost, int cap){
    add(u, v, cost, cap, 0);
    add(v, u, -cost, 0, 0);
}
bool spfa(){
    queue<int> Q;
    memset(vis, false, sizeof(vis));
    memset(pre, -1, sizeof(pre));
    memset(low, 0, sizeof(low));
    fill(&dis[0], &dis[maxn], -INF);
    low[s] = INF, dis[s] = 0, vis[s] = true;
    Q.push(s);
    while(!Q.empty())
    {
        int u = Q.front();
        Q.pop();
        vis[u] = false;
        for(int i = eh[u]; i != -1; i = et[i].next)
        {
            int v = et[i].v, cost = et[i].cost, cap = et[i].cap, flow = et[i].flow;
            if(cap - flow && dis[v] < dis[u] + cost)
            {
                dis[v] = dis[u] + cost;
                pre[v] = i;
                low[v] = min(cap - flow, low[u]);
                if(!vis[v])
                {
                    Q.push(v);
                    vis[v] = true;
                }
            }
        }
    }
    return dis[t] != -INF;
}
void costflow(){
    anscost = 0;
    while(spfa())
    {
        int x = pre[t];
        anscost += low[t] * dis[t];
        while(x != -1)
        {
            et[x].flow += low[t];
            et[x^1].flow -= low[t];
            x = pre[et[x].u];
        }
    }
}
int main()
{
    while(~scanf("%d", &n))
    {
        init();
        s = 0, t = 2 * n * n + 1;
        for(int i = 1; i <= n; i++)
        for(int j = 1; j <= n; j++)
        {
            scanf("%d", &G[i][j]);
            int u = (i - 1) * n + j;
            addedge(u, u + n * n, G[i][j], 1);
        }
        addedge(s, 1 + n * n, 0, 2);
        addedge(n * n, t, 0, 2);
        for(int i = 1; i <= n; i++)
        for(int j = 1; j <= n; j++)
        {
            int u = (i - 1) * n + j;
            if(ok(i + 1, j)) addedge(u + n * n, u + n, 0, 1);
            if(ok(i, j + 1)) addedge(u + n * n, u + 1, 0, 1);
        }
        costflow();
        printf("%d\n", anscost + G[1][1] + G[n][n]);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值