hdu 2686&&hdu 3376(拆点+构图+最小费用最大流)

Matrix

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


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
 

 

Author
yifenfei
 

 

Source
 
题意:一个人要从(1,1)->(n,n) 然后要从(n,n)->(1,1),每个点最多走一次,每个点都要一个权值,问这样走完之后能够得到的最大权值是多少?
题解:由于每个点只能够走一次,所以我们将除了 (1,1)和(n,n) 之外的点都拆点来限制次数(其实好像(n,n)也可以拆,因为也只会走一次,但是(1,1)是不能够拆的),然后将(i-1)*n+j向(i-1)+j+n*n连一条容量为1,费用为 -graph[i][j] ,然后将每个点都和其右边和下面的点连一条容量为1,费用为 0的边,然后建立超级源点,向 (1,1)连一条容量为2,费用为0的边,建立超级汇点,然后将(n,n)向超级汇点连一条容量为2,费用为0的边,这样就达到了限制次数为2的效果,最后跑一遍最小费用最大流,取反之后加上graph[1][1]和graph[n][n]即最后的结果.
#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm>
using namespace std;
const int INF = 999999999;
const int N = 2005;
const int M = 500005;
struct Edge{
    int u,v,cap,cost,next;
}edge[M];
int head[N],tot,low[N],pre[N];
int total ;
bool vis[N];
int flag[N][N];
void addEdge(int u,int v,int cap,int cost,int &k){
    edge[k].u=u,edge[k].v=v,edge[k].cap = cap,edge[k].cost = cost,edge[k].next = head[u],head[u] = k++;
    edge[k].u=v,edge[k].v=u,edge[k].cap = 0,edge[k].cost = -cost,edge[k].next = head[v],head[v] = k++;
}
void init(){
    memset(head,-1,sizeof(head));
    tot = 0;
}
bool spfa(int s,int t,int n){
    memset(vis,false,sizeof(vis));
    for(int i=0;i<=n;i++){
        low[i] = INF;
        pre[i] = -1;
    }
    queue<int> q;
    low[s] = 0;
    q.push(s);
    while(!q.empty()){
        int u = q.front();
        q.pop();
        vis[u] = false;
        for(int k=head[u];k!=-1;k=edge[k].next){
            int v = edge[k].v;
            if(edge[k].cap>0&&low[v]>low[u]+edge[k].cost){
                low[v] = low[u] + edge[k].cost;
                pre[v] = k; ///v为终点对应的边
                if(!vis[v]){
                    vis[v] = true;
                    q.push(v);
                }
            }
        }
    }
    if(pre[t]==-1) return false;
    return true;
}
int MCMF(int s,int t,int n){
    int mincost = 0,minflow,flow=0;
     while(spfa(s,t,n))
    {
        minflow=INF+1;
        for(int i=pre[t];i!=-1;i=pre[edge[i].u])
            minflow=min(minflow,edge[i].cap);
        flow+=minflow;
        for(int i=pre[t];i!=-1;i=pre[edge[i].u])
        {
            edge[i].cap-=minflow;
            edge[i^1].cap+=minflow;
        }
        mincost+=low[t]*minflow;
    }
    total=flow;
    return mincost;
}
int n;
int graph[35][35];
int P(int x,int y){
    return (x-1)*n+y;
}
int main(){
    while(scanf("%d",&n)!=EOF){
        init();
        for(int i=1;i<=n;i++){
            for(int j=1;j<=n;j++){
                scanf("%d",&graph[i][j]);
            }
        }
        int src = 0,des = 2*n*n+1;
        for(int i=1;i<=n;i++){
            for(int j=1;j<=n;j++){
                if(P(i,j)!=1&&P(i,j)!=n*n){
                    addEdge(P(i,j),P(i,j)+n*n,1,-graph[i][j],tot);
                    if(i!=n) addEdge(P(i,j)+n*n,P(i+1,j),1,0,tot);
                    if(j!=n) addEdge(P(i,j)+n*n,P(i,j)+1,1,0,tot);
                }else{
                    if(P(i,j)==1){
                        addEdge(P(i,j),P(i+1,j),1,0,tot);
                        addEdge(P(i,j),P(i,j)+1,1,0,tot);
                    }
                }
            }
        }
        addEdge(src,1,2,0,tot);
        addEdge(n*n,des,2,0,tot);
        int min_cost = MCMF(src,des,2*n*n+2);
        printf("%d\n",-min_cost+graph[1][1]+graph[n][n]);
    }
}

 hdu 3376开大一点就OK

#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm>
using namespace std;
const int INF = 999999999;
const int N = 720005;
const int M = 3000005;
struct Edge{
    int u,v,cap,cost,next;
}edge[M];
int head[N],tot,low[N],pre[N];
int total ;
bool vis[N];
void addEdge(int u,int v,int cap,int cost,int &k){
    edge[k].u=u,edge[k].v=v,edge[k].cap = cap,edge[k].cost = cost,edge[k].next = head[u],head[u] = k++;
    edge[k].u=v,edge[k].v=u,edge[k].cap = 0,edge[k].cost = -cost,edge[k].next = head[v],head[v] = k++;
}
void init(){
    memset(head,-1,sizeof(head));
    tot = 0;
}
bool spfa(int s,int t,int n){
    memset(vis,false,sizeof(vis));
    for(int i=0;i<=n;i++){
        low[i] = INF;
        pre[i] = -1;
    }
    queue<int> q;
    low[s] = 0;
    q.push(s);
    while(!q.empty()){
        int u = q.front();
        q.pop();
        vis[u] = false;
        for(int k=head[u];k!=-1;k=edge[k].next){
            int v = edge[k].v;
            if(edge[k].cap>0&&low[v]>low[u]+edge[k].cost){
                low[v] = low[u] + edge[k].cost;
                pre[v] = k; ///v为终点对应的边
                if(!vis[v]){
                    vis[v] = true;
                    q.push(v);
                }
            }
        }
    }
    if(pre[t]==-1) return false;
    return true;
}
int MCMF(int s,int t,int n){
    int mincost = 0,minflow,flow=0;
     while(spfa(s,t,n))
    {
        minflow=INF+1;
        for(int i=pre[t];i!=-1;i=pre[edge[i].u])
            minflow=min(minflow,edge[i].cap);
        flow+=minflow;
        for(int i=pre[t];i!=-1;i=pre[edge[i].u])
        {
            edge[i].cap-=minflow;
            edge[i^1].cap+=minflow;
        }
        mincost+=low[t]*minflow;
    }
    total=flow;
    return mincost;
}
int n;
int graph[605][605];
int P(int x,int y){
    return (x-1)*n+y;
}
int main(){
    while(scanf("%d",&n)!=EOF){
        init();
        for(int i=1;i<=n;i++){
            for(int j=1;j<=n;j++){
                scanf("%d",&graph[i][j]);
            }
        }
        int src = 0,des = 2*n*n+1;
        for(int i=1;i<=n;i++){
            for(int j=1;j<=n;j++){
                if(P(i,j)!=1&&P(i,j)!=n*n){
                    addEdge(P(i,j),P(i,j)+n*n,1,-graph[i][j],tot);
                    if(i!=n) addEdge(P(i,j)+n*n,P(i+1,j),1,0,tot);
                    if(j!=n) addEdge(P(i,j)+n*n,P(i,j)+1,1,0,tot);
                }else{
                    if(P(i,j)==1){
                        addEdge(P(i,j),P(i+1,j),1,0,tot);
                        addEdge(P(i,j),P(i,j)+1,1,0,tot);
                    }
                }
            }
        }
        addEdge(src,1,2,0,tot);
        addEdge(n*n,des,2,0,tot);
        int min_cost = MCMF(src,des,2*n*n+2);
        printf("%d\n",-min_cost+graph[1][1]+graph[n][n]);
    }
}

 

posted @ 2016-08-02 21:03  樱花庄的龙之介大人  阅读(196)  评论(0编辑  收藏  举报