hdu 3376 Matrix Again(费用流,拆点,手动扩栈)

来源:互联网 发布:李小龙身体实际数据 编辑:程序博客网 时间:2024/05/01 23:05

Matrix Again

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


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
210 35 10310 3 32 5 36 7 1051 2 3 4 52 3 4 5 63 4 5 6 74 5 6 7 85 6 7 8 9
 

Sample Output
284680
题意:有一个n*n的矩阵,现在要从左上角走到右下角并且每个点只能走一次(除开起点和终点),再从终点走到起点,求最大的权值,从左上角走到右下角只能走右边和下边,右下角走到左上角只能走左边和上边

思路:很明显的费用流模型,其实就相当于(1,1)出发走到(n,n)走两次求最大权值。不过因为这个每个点只能走一次,所以我们把每个点都拆成两个点,中间连一条容量为1的边即可,注意数组开大点,600*600*2,这道题数组那么大居然能过我也是挺惊讶的= = 不过G++会TLE,原因是栈溢出了,我们可以手动扩栈。在头文件处加一句#pragma comment(linker, "/STACK:102400000,102400000")

或者直接用手动循环队列更方便,都是1000多msAC

代码1:

#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <cmath>#include <queue>#pragma comment(linker, "/STACK:102400000,102400000")using namespace std;#define N 720050#define INF 0x3f3f3f3fstruct Edge{    int from,to,next,cap,cost;///起点,终点,同起点下一条边,残余流量,费用} edge[N*3];int cnt,head[N];int vis[N],d[N],pp[N];int dir[2][2]={1,0,0,1};int ma[602][602];int sumflow;///最大流量总和void init(){    cnt=0;    memset(head,-1,sizeof(head));}void addedge(int from,int to,int cap,int cost){    edge[cnt].from=from;    edge[cnt].to=to;    edge[cnt].cost=cost;    edge[cnt].cap=cap;    edge[cnt].next=head[from];    head[from]=cnt++;    edge[cnt].from=to;    edge[cnt].to=from;    edge[cnt].cost=-cost;    edge[cnt].cap=0;    edge[cnt].next=head[to];    head[to]=cnt++;///存反向边}int spfa(int s,int t,int n){    queue<int>q;    memset(vis,0,sizeof(vis));    memset(pp,-1,sizeof(pp));///pp[i]表示最短路径上以i为终点的边的编号    for(int i=0; i<=n; i++)        d[i]=-INF;    d[s]=0;    vis[s]=1;    q.push(s);    while(!q.empty())    {        int u=q.front();        q.pop();        vis[u]=0;        for(int i=head[u]; i!=-1; i=edge[i].next)        {            int v=edge[i].to;            if(edge[i].cap>0&&d[v]<d[u]+edge[i].cost)            {                d[v]=d[u]+edge[i].cost;                pp[v]=i;                if(!vis[v])                {                    vis[v]=1;                    q.push(v);                }            }        }    }    if(d[t]==-INF) return 0;///找不到一条到终点的路    return 1;}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=pp[t]; i!=-1; i=pp[edge[i].from])            minflow=min(minflow,edge[i].cap);///从路径中找最小的流量        flow+=minflow;///总流量加上最小流量        for(int i=pp[t]; i!=-1; i=pp[edge[i].from])        {            edge[i].cap-=minflow;///当前边减去最小流量            edge[i^1].cap+=minflow;///反向边加上最小流量        }        mincost+=d[t]*minflow;///最小费用等于路径和*每条路径的流量(经过多少次)    }    sumflow=flow;    return mincost;}int main(){    int n;    while(~scanf("%d",&n))    {        init();        for(int i=1;i<=n;i++)            for(int j=1;j<=n;j++)            scanf("%d",&ma[i][j]);        int  s=1,t=2*n*n;        for(int i=1;i<=n;i++)        {            for(int j=1;j<=n;j++)            {                if((i==1&&j==1)||(i==n&&j==n)) addedge((i-1)*n+j,(i-1)*n+j+n*n,2,ma[i][j]);                else addedge((i-1)*n+j,(i-1)*n+j+n*n,1,ma[i][j]);                for(int k=0;k<2;k++)                {                    int x=i+dir[k][0],y=j+dir[k][1];                    if(x<1||x>n||y<1||y>n) continue;                    addedge((i-1)*n+j+n*n,(x-1)*n+y,1,0);                }            }        }        printf("%d\n",MCMF(s,t,t)-ma[1][1]-ma[n][n]);    }    return 0;}
代码二:

#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <cmath>#include <queue>using namespace std;#define N 720050#define INF 0x3f3f3f3fstruct Edge{    int from,to,next,cap,cost;///起点,终点,同起点下一条边,残余流量,费用} edge[N*3];int cnt,head[N];int vis[N],d[N],pp[N];int dir[2][2]={1,0,0,1};int ma[602][602];int que[N];int sumflow;///最大流量总和void init(){    cnt=0;    memset(head,-1,sizeof(head));}void addedge(int from,int to,int cap,int cost){    edge[cnt].from=from;    edge[cnt].to=to;    edge[cnt].cost=cost;    edge[cnt].cap=cap;    edge[cnt].next=head[from];    head[from]=cnt++;    edge[cnt].from=to;    edge[cnt].to=from;    edge[cnt].cost=-cost;    edge[cnt].cap=0;    edge[cnt].next=head[to];    head[to]=cnt++;///存反向边}int spfa(int s,int t,int n){    memset(vis,0,sizeof(vis));    memset(pp,-1,sizeof(pp));///pp[i]表示最短路径上以i为终点的边的编号    for(int i=0; i<=n; i++)        d[i]=-INF;    d[s]=0;    vis[s]=1;    int tot=0,tail=0;    que[tail++]=s;    while(tot!=tail)    {        int u=que[tot];        tot=(tot+1)%N;        vis[u]=0;        for(int i=head[u]; i!=-1; i=edge[i].next)        {            int v=edge[i].to;            if(edge[i].cap>0&&d[v]<d[u]+edge[i].cost)            {                d[v]=d[u]+edge[i].cost;                pp[v]=i;                if(!vis[v])                {                    vis[v]=1;                    que[tail]=v;                    tail=(tail+1)%N;                }            }        }    }    if(d[t]==-INF) return 0;///找不到一条到终点的路    return 1;}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=pp[t]; i!=-1; i=pp[edge[i].from])            minflow=min(minflow,edge[i].cap);///从路径中找最小的流量        flow+=minflow;///总流量加上最小流量        for(int i=pp[t]; i!=-1; i=pp[edge[i].from])        {            edge[i].cap-=minflow;///当前边减去最小流量            edge[i^1].cap+=minflow;///反向边加上最小流量        }        mincost+=d[t]*minflow;///最小费用等于路径和*每条路径的流量(经过多少次)    }    sumflow=flow;    return mincost;}int main(){    int n;    while(~scanf("%d",&n))    {        init();        for(int i=1;i<=n;i++)            for(int j=1;j<=n;j++)            scanf("%d",&ma[i][j]);        int  s=1,t=2*n*n;        for(int i=1;i<=n;i++)        {            for(int j=1;j<=n;j++)            {                if((i==1&&j==1)||(i==n&&j==n)) addedge((i-1)*n+j,(i-1)*n+j+n*n,2,ma[i][j]);                else addedge((i-1)*n+j,(i-1)*n+j+n*n,1,ma[i][j]);                for(int k=0;k<2;k++)                {                    int x=i+dir[k][0],y=j+dir[k][1];                    if(x<1||x>n||y<1||y>n) continue;                    addedge((i-1)*n+j+n*n,(x-1)*n+y,1,0);                }            }        }        printf("%d\n",MCMF(s,t,t)-ma[1][1]-ma[n][n]);    }    return 0;}







0 0
原创粉丝点击