HDU 2686

       这是一道费用流的题目,当然也可以用DP,不过我只会用费用流的方法。对我而言,可能这道题不算简单。

       这道题首先要构图,开始想了其他的方法,最终发现不对,最后看了网上其他人的思路才构图成功。不过,在这里我不打算详细讲解费用流,只想讲一下这道题一些体会。

       费用流问题用最短路算法求一条费用最小的路径,但这里需要求最大的值,所以需要一些处理。根据这道题和以前的一点经验,发现要求有最大值权值和的路径的话,如果是有负权的图,则是将每条边的权值乘以-1,如果没有负权,则用一个大数减去每条边的权值,此时,求出的最短路径再原图上具有最大的权值。对于负权图不可以用第二种方法,因为这样的话每条边的权值都是正值,那么如果有负环,这样处理后也能求出最短路,而实际上有负环是不存在最短路的。

       而我就是在这里犯了错误,导致一直WA。

代码(G++):

#include <cstdlib>
#include <iostream>
#include <queue>

#define MAX 1000
#define INF 99999999

//#define LOCAL

using namespace std;

struct Edge{
    int to;
    int c;
    int f;
    int w; 
    int cmp; 
    int next; 
}; 
Edge edge[6*MAX];

int head[2*MAX],pre[2*MAX],pre2[2*MAX],cost[2*MAX],map[30][30],p;
bool inq[2*MAX];

void add_edge(int u,int v,int x,int c)
{
     edge[p].to=v;
     edge[p].next=head[u];
     head[u]=p;
     edge[p].c=c;
     edge[p].w=x; 
     edge[p].f=0;
     edge[p].cmp=-edge[p].w;
     p++;
         
     edge[p].to=u;
     edge[p].next=head[v];
     head[v]=p;
     edge[p].c=0;
     edge[p].w=-x;
     edge[p].f=0; 
     edge[p].cmp=-edge[p].w;    
     p++;
}

int main(int argc, char *argv[])
{
#ifdef LOCAL
   freopen("in.txt","r",stdin);
   freopen("out.txt","w",stdout);
#endif

    int n,i,j,t,ans; 
    queue<int> q;
    while(scanf("%d",&n)!=EOF)
    {      
       memset(head,-1,sizeof(head));                                               
       p=0;                        
       for(i=0;i<n;i++)
       {
          for(j=0;j<n;j++)
          {
              scanf("%d",&map[i][j]);
              
              if(i+j==0||i+j==2*(n-1)) add_edge(i*n+j,i*n+j+n*n,map[i][j],2);
              else add_edge(i*n+j,i*n+j+n*n,map[i][j],1);
              
              if(j<n-1) add_edge(i*n+j+n*n,i*n+j+1,0,1);
              if(i<n-1) add_edge(i*n+j+n*n,(i+1)*n+j,0,1);                                                                                                                        
          }             
       }
       ans=0;
              
       while(1)
       {  
          for(i=0;i<2*n*n;i++)
          {
             inq[i]=false;
             cost[i]=INF;  
             pre[i]=i;            
          }       
          cost[0]=0;
          q.push(0);
          inq[0]=true;
          
          while(!q.empty())
          {
              t=q.front();
              q.pop();          
              inq[t]=false;
              for(i=head[t];i!=-1;i=edge[i].next)
              {
                  if(edge[i].c>edge[i].f&&cost[t]+edge[i].cmp<cost[edge[i].to])
                  {
                      cost[edge[i].to]=cost[t]+edge[i].cmp;
                      pre2[edge[i].to]=i;
                      pre[edge[i].to]=t;                      
                      if(!inq[edge[i].to])
                      {
                         q.push(edge[i].to);
                         inq[edge[i].to]=true;           
                      }
                  }                         
              }              
          }
          
          if(cost[2*n*n-1]==INF) break;
          
          for(i=2*n*n-1;i!=0;i=pre[i])
          {
             edge[pre2[i]].f+=1;
             edge[pre2[i]^1].f-=1;
             ans+=edge[pre2[i]].w;                              
          }       
       } 
       ans-=map[0][0]+map[n-1][n-1];
       printf("%d\n",ans);           
    }
    system("PAUSE");
    return EXIT_SUCCESS;
}


题目(http://acm.hdu.edu.cn/showproblem.php?pid=2686):

Matrix

                                                                              Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)


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

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值