码迷,mamicode.com
首页 > 其他好文 > 详细

poj3422--Kaka's Matrix Travels(拆点,最大费用)

时间:2014-08-21 21:15:18      阅读:300      评论:0      收藏:0      [点我收藏+]

标签:des   blog   使用   io   strong   for   ar   2014   

Kaka‘s Matrix Travels
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 7802   Accepted: 3138

Description

On an N × N chessboard with a non-negative number in each grid, Kaka starts his matrix travels with SUM = 0. For each travel, Kaka moves one rook from the left-upper grid to the right-bottom one, taking care that the rook moves only to the right or down. Kaka adds the number to SUM in each grid the rook visited, and replaces it with zero. It is not difficult to know the maximum SUM Kaka can obtain for his first travel. Now Kaka is wondering what is the maximum SUM he can obtain after his Kth travel. Note the SUM is accumulative during the K travels.

Input

The first line contains two integers N and K (1 ≤ N ≤ 50, 0 ≤ K ≤ 10) described above. The following N lines represents the matrix. You can assume the numbers in the matrix are no more than 1000.

Output

The maximum SUM Kaka can obtain after his Kth travel.

Sample Input

3 2
1 2 3
0 2 1
1 4 2

Sample Output

15

 

要求从左上走到右下,每个权值只能加一次,所以使用拆点,使得每个权值只会使用一次,同样是求最大的权值,所以要是权值为负值。要走k次,所以可以将源点到1和n*n*2到汇点的容量设为k。

 

#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm>
using namespace std;
#define INF 0x3f3f3f3f
#define maxn 6000
struct node
{
    int v , w , s ;
    int next ;
} p[1000000];
int a[60][60] , cnt ;
int head[maxn] , vis[maxn] , dis[maxn] , pre[maxn] ;
queue <int> q ;
void add(int u,int v,int w,int s)
{
    p[cnt].v = v ;p[cnt].w = w ; p[cnt].s = s ;
    p[cnt].next = head[u] ; head[u] = cnt++;
    p[cnt].v = u ;p[cnt].w = 0 ; p[cnt].s = -s ;
    p[cnt].next = head[v] ; head[v] = cnt++;
}
int spfa(int s,int t)
{
    int u , v , i ;
    memset(dis,INF,sizeof(dis));
    pre[s] = pre[t] = -1 ;
    vis[s] = 1 ;
    dis[s] = 0 ;
    while( !q.empty() )
        q.pop();
    q.push(s) ;
    while( !q.empty() )
    {
        u = q.front();
        q.pop();
        vis[u] = 0 ;
        for(i = head[u] ; i != -1 ; i = p[i].next)
        {
            v=  p[i].v ;
            if( p[i].w && dis[v] > dis[u] + p[i].s)
            {
                dis[v] = dis[u] + p[i].s ;
                pre[v] = i ;
                if( !vis[v] )
                {
                    vis[v] = 1 ;
                    q.push(v);
                }
            }
        }
    }
    if( pre[t] == -1 )
        return 0;
    return 1;
}
void f(int s,int t)
{
    int i , min1 , ans = 0 ;
    memset(pre,-1,sizeof(pre));
    memset(vis,00,sizeof(vis));
    while( spfa(s,t) )
    {
        min1 = INF ;
        for(i = pre[t] ; i != -1 ; i = pre[ p[i^1].v ])
            if( p[i].w < min1 )
                min1 = p[i].w ;
        for(i = pre[t] ; i != -1 ; i = pre[ p[i^1].v ])
        {
            p[i].w -= min1 ;
            p[i^1].w += min1 ;
            ans += p[i].s ;
        }
    }
    printf("%d\n", -ans);
}
int main()
{
    int i , j , n , k , temp , b ;
    while(scanf("%d %d", &n, &k) != EOF)
    {
        /*每个点的值只会被访问一次,所以需要拆点操作,要求的是最大费用,所以可以将费用做成负值,仍可以使用最小费用的做法*/
        cnt = 0 ; temp = n*n ;
        memset(head,-1,sizeof(head));
        for(i = 1 ; i <= n ; i++)
            for(j = 1 ; j <= n ; j++)
            {
                scanf("%d", &a[i][j]);
                b = (i-1)*n + j ;
                add(b,b+temp,1,-a[i][j]);//对于第i个点,由i到i+n*n建边,一条是带有花费的,但容量是1,一条是没花费,容量是k-1,保证一个值只会被访问一次
                add(b,b+temp,k-1,0);
            }
        for(i = 1 ; i <= n ; i++)
        {
            for(j = 1 ; j <= n ; j++)
            {
                b = (i-1)*n+j ;
                if(i >1)
                    add(b-n+temp,b,k,0);
                if(j > 1)
                    add(b-1+temp,b,k,0);
            }
        }
        add(0,1,k,0);//源点是0,汇点是n*n+1
        add(2*temp,2*temp+1,k,0);
        f(0,2*temp+1);
    }
    return 0;
}


 

poj3422--Kaka's Matrix Travels(拆点,最大费用),布布扣,bubuko.com

poj3422--Kaka's Matrix Travels(拆点,最大费用)

标签:des   blog   使用   io   strong   for   ar   2014   

原文地址:http://blog.csdn.net/winddreams/article/details/38735897

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!