hdu 3376 Matrix again

Matrix Again

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

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
  
  
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
Starvae
 

 

Source
 

 

Recommend
lcy
花了一天多的时间做的网络流。。。。感动啊! 现在我来说一下建图过程吧,把矩阵中的每个点拆成两个点,v和v',之间连一条边 容量为1,花费为-matrix[i][j];然后v'在与v+1 和v+n的点连一条边!(v+1,v+n保证在矩阵中是存在的!否则不连)然后在源点1和源点1‘连一条边,容量为2,花费为0;在汇点n*n和n*n‘连一条边,容量为2,花费为0;
因为一开始用全局VECTOR,每次做之前要全部清空!所以导致了我10来次的超时。后来索性不用了。改用覆盖的形式,用一个cnt[N]记录建图时放进来的边数;
然后就纯粹的最小费用最大流!
#include<iostream>
#include<queue>
#include<vector>
#include<memory>
using namespace std;
#define inf 0x7ffffff
const int N=721000;
struct FUCK
{
    int c,w,f;//c 容量 w花费 f流量
    int next;//记录下一节点
}node,map[N][7];//map[N][4]这个就够大了,map[N][3]就错了
//vector<FUCK> map[N];
int dia[605*605];
int n,tot;
int pre[N];
int dis[N];
bool flag[N];
int cnt[N];

int SPFA()
{
   
    queue<int>Q;
    //int temp;
    int i,now;
    pre[1] = 1;
    for(i=2;i<=tot+tot;i++)
        dis[i] = inf;
    dis[1] = 0;
   memset(flag,0,sizeof(flag));
 flag[1] = true;
    Q.push(1);
    while(!Q.empty())
    {
        now = Q.front();
        Q.pop();
        flag[now] = false;
        for(i=0;i<cnt[now];i++)
        {
            node = map[now][i];
            if(node.c - node.f > 0 && dis[node.next] > dis[now] + node.w)
            {
                dis[node.next]  = dis[now] + node.w;
                pre[node.next] = now;
    if(!flag[node.next])
    {
                 Q.push(node.next);
     flag[node.next] = true;
    }
            }
        }
    }
    if(dis[tot+tot] != inf)
        return 1;
    else
        return 0;
}
int mincostflow()
{
    int i,j,min,ans=0;
    while(SPFA())
    {
        //min = 0x7fffffff;
        //min = 0x7fffffff;
        /*for(i=tot+tot;i!=1;i=pre[i])
            for(j=0;j<map[pre[i]].size();j++)
                if(map[pre[i]][j].next == i)
                {
                    min = min < (map[pre[i]][j].c - map[pre[i][j]
                    */
        for(i=tot+tot;i!=1;i=pre[i])
            for(j=0;j<cnt[pre[i]];j++)
                if(map[pre[i]][j].next == i)
                {
                    map[pre[i]][j].f+=1;
                    node.c=0;
                    node.f=-1;
                    node.next=pre[i];
                    node.w = -map[pre[i]][j].w;
                   // if(i!=tot+tot&&i!=tot)
                        map[i][cnt[i]++] = node;
                }
        ans += 1*dis[tot+tot];
    }
    return ans;
}
void makegraph()
{
    int i,j;
    int temp;
    tot = n*n;
    node.c=2;node.f=0;node.w=0;node.next=tot+1;
    map[1][cnt[1]++] = node;
    node.c=2;node.f=0;node.w=0;node.next=tot+tot;
    map[tot][cnt[tot]++] = node;
    for(i=1;i<=n*n;i++)
    {
        node.c = 1;
        node.w=-dia[i];
        node.f=0;
        node.next=tot+i;
        map[i][cnt[i]++] = node;
    }
    for(i=tot+1;i<=tot+tot;i++)
    {
        node.w = 0;
        node.f = 0;
        node.c = 1;
        temp = i-tot;
        if(temp%n!=0)
        {
            node.next=temp+1;
            map[i][cnt[i]++] = node;
        }
        if(temp+n<=tot)
        {
            node.next=temp+n;
            map[i][cnt[i]++] = node;
        }
    }
}
int main()
{
    int i;
    while(scanf("%d",&n)!=EOF)
    {
       // for(i=1;i<=n*n*2;i++)
          //  map[i].clear();
  memset(cnt,0,sizeof(cnt));
        for(i=1;i<=n*n;i++)
            scanf("%d",&dia[i]);
        makegraph();
        cout<<-mincostflow()<<endl;
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值