HDU 2686 Matrix 最小费用最大流

题意:给一个N*N的矩阵,找一条路从(1,1)到(n,n)然后再从(n,n)回到(1,1),除了(1,1)和(n,n)每个点只能经过一次。
思路:把每个点拆成入点和出点,拆点是为了控制权值。(1,1)和(n,n)的 位置流量为2,把(i,j)的入点和(i-1,j) (i,j-1)分别连一条流量为1的边(控制流量,保证每个点只能经过一次)。网络流求的是最小费用,只要将100-权值即可(权值<100),最后算结果的时候再处理。

http://acm.hdu.edu.cn/showproblem.php?pid=2686

/*********************************************
    Problem : HDU 2686
    Author  : NMfloat
    InkTime (c) NM . All Rights Reserved .
********************************************/

#include <map>
#include <set>
#include <queue>
#include <cmath>
#include <ctime>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>

#define rep(i,a,b)  for(int i = a ; i <= b ; i ++)
#define rrep(i,a,b) for(int i = b ; i >= a ; i --)
#define repE(p,u) for(Edge * p = G[u].first ; p ; p = p -> next)
#define cls(a,x)   memset(a,x,sizeof(a))

using namespace std;

const int MOD = 1e9+7;
//const int INF = 0x3f3f3f3f;
//const int MAXN = 1e5;
//const int MAXE = 2e5;

typedef long long LL;

int T,n,m,k;

const int MAXN = 10000;  
const int MAXM = 100000;  
const int INF = 0x3f3f3f3f;  

struct Edge{  
    int to,next,cap,flow,cost;  
}edge[MAXM];  

int head[MAXN],tol;  
int pre[MAXN],dis[MAXN];  
bool vis[MAXN];  
int N;  
//N means the number of nodes.from 0 to n-1  

void init(int n){  
    N = n;  
    tol = 0;  
    memset(head,-1,sizeof(head));  
}  

void addedge(int u,int v,int cap,int cost){  
    edge[tol].to = v; edge[tol].cap = cap; edge[tol].cost = cost;  
    edge[tol].flow = 0; edge[tol].next = head[u]; head[u] = tol ++;  
    edge[tol].to = u; edge[tol].cap = 0; edge[tol].cost = -cost;  
    edge[tol].flow = 0; edge[tol].next = head[v]; head[v] = tol ++;  
}  

bool spfa(int s,int t){  
    queue<int>q;  
    for(int i = 0;i < N;i++){  
        dis[i] = INF;  
        vis[i] = false;  
        pre[i] = -1;  
    }  
    dis[s] = 0;  
    vis[s] = true;  
    q.push(s);  
    while(!q.empty()){  
        int u = q.front();  
        q.pop();  
        vis[u] = false;  
        for(int i=head[u];i != -1;i=edge[i].next){  
            int v = edge[i].to;  
            if(edge[i].cap > edge[i].flow && dis[v] > dis[u] + edge[i].cost){  
                dis[v] = dis[u] + edge[i].cost;  
                pre[v] = i;  
                if(!vis[v]){  
                    vis[v] = true;  
                    q.push(v);  
                }  
            }  
        }  
    }  
    if(pre[t] == -1) return false;  
    else return true;  
}  

//ruturn maxflow ,cost is the mincost;  
int mincost_maxflow(int s,int t,int &cost){  
    int flow = 0;  
    cost = 0;  
    while(spfa(s,t)){  
        int Min = INF;  
        for(int i = pre[t];i != -1;i = pre[edge[i^1].to]){  
            if(Min > edge[i].cap - edge[i].flow)  
                Min = edge[i].cap - edge[i].flow;  
        }  
        for(int i = pre[t];i != -1;i = pre[edge[i^1].to]){  
            edge[i].flow += Min;  
            edge[i^1].flow -= Min;  
            cost += edge[i].cost * Min;  
        }  
        flow += Min;  
    }  
    return flow;  
}

int Map[35][35];
int Mat[35][35][2];

void input() {
    rep(i,1,n) rep(j,1,n) {
        scanf("%d",&Map[i][j]);
        Map[i][j] = 100 - Map[i][j];
    }
}   

void solve() {
    int tot = 0;
    cls(Mat,0);
    rep(i,1,n) rep(j,1,n) {
        Mat[i][j][0] = tot ++; //入点
        Mat[i][j][1] = tot ++; //出点
    }
    init(Mat[n][n][1]+1);
    rep(i,1,n) rep(j,1,n) {
        if(i == 1 && j == 1) addedge(Mat[i][j][0],Mat[i][j][1],2,Map[i][j]);
        else if(i == 1) {
            addedge(Mat[i][j][0],Mat[i][j][1],1,Map[i][j]);
            addedge(Mat[i][j-1][1],Mat[i][j][0],1,0);
        }
        else if(j == 1) {
            addedge(Mat[i][j][0],Mat[i][j][1],1,Map[i][j]);
            addedge(Mat[i-1][j][1],Mat[i][j][0],1,0);
        }
        else if(i == n && j == n) {
            addedge(Mat[i][j][0],Mat[i][j][1],2,Map[i][j]);
            addedge(Mat[i][j-1][1],Mat[i][j][0],1,0);
            addedge(Mat[i-1][j][1],Mat[i][j][0],1,0);
        }
        else {
            addedge(Mat[i][j][0],Mat[i][j][1],1,Map[i][j]);
            addedge(Mat[i][j-1][1],Mat[i][j][0],1,0);
            addedge(Mat[i-1][j][1],Mat[i][j][0],1,0);

        }
    }
    int cost = 0;
    int flow = mincost_maxflow(Mat[1][1][0],Mat[n][n][1],cost);
    cost = 100*(4*n-2)-cost - (200 - Map[1][1] - Map[n][n]);
    printf("%d\n",cost);
}

int main(void) {
    //freopen("a.in","r",stdin);
    //scanf("%d",&T);
    //while(T--) {
    while(~scanf("%d",&n)) {
        input();
        solve();
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值