hdu 3376 Matrix Again

Matrix Again

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


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

以左上角为源点,右下角为汇点。对于每个点拆成两点,两点间连一条容量为1花费为cost的边,注意起点和终点都能走两次,因此对于起点和终点,拆成的两点间再连一条容量为1且话费为0的边,对于每个cost都取反,则转化为最小费用最大流。

#include<stdio.h>
#include<algorithm>
#include<string.h>
#include<queue>
using namespace std;
#define INF 0x3f3f3f3f
const int maxm = 1000005;
struct node
{
	int u, v, cap, cost, next;
}edge[maxm*2];
int vis[maxm], pre[maxm], head[maxm], dis[maxm];
int n, m, s, t, cnt;
void init()
{
	cnt = 0, s = 0, t = ((n - 1)*n + n - 1) * 2 + 1;
	memset(head, -1, sizeof(head));
}
void add(int u, int v, int cap, int cost)
{
	edge[cnt].u = u, edge[cnt].v = v;
	edge[cnt].cap = cap, edge[cnt].cost = cost;
	edge[cnt].next = head[u], head[u] = cnt++;
	edge[cnt].u = v, edge[cnt].v = u;
	edge[cnt].cap = 0, edge[cnt].cost = -cost;
	edge[cnt].next = head[v], head[v] = cnt++;
}
int bfs()
{
	memset(pre, -1, sizeof(pre));
	queue<int>q;
	for (int i = 0;i <= t;i++) dis[i] = INF;
	dis[s] = 0;q.push(s);
	while (!q.empty())
	{
		int u = q.front();q.pop();
		for (int i = head[u];i != -1;i = edge[i].next)
		{
			int v = edge[i].v;
			if (dis[v] > dis[u] + edge[i].cost&&edge[i].cap)
			{
				dis[v] = dis[u] + edge[i].cost;
				pre[v] = i;
				q.push(v);
			}
		}
	}
	if (dis[t] == INF) return 0;
	return 1;
}
int MCMF()
{
	int ans = 0, minflow;
	while (bfs())
	{
		minflow = INF;
		for (int i = pre[t];i != -1;i = pre[edge[i].u])
			minflow = min(minflow, edge[i].cap);
		for (int i = pre[t];i != -1;i = pre[edge[i].u])
		{
			edge[i].cap -= minflow;
			edge[i ^ 1].cap += minflow;
		}
		ans += dis[t] * minflow;
	}
	return ans;
}
int main()
{
	int i, j, k, a;
	while (scanf("%d", &n) != EOF)
	{
		init();
		for (i = 0;i < n;i++)
		{
			for (j = 0;j < n;j++)
			{
				scanf("%d", &a);
				add((i*n + j) * 2, (i*n + j) * 2 + 1, 1, -a);
				if (i != n - 1) add((i*n + j) * 2 + 1, ((i + 1)*n + j) * 2, 1, 0);
				if (j != n - 1) add((i*n + j) * 2 + 1, (i*n + j + 1) * 2, 1, 0);
			}
		}
		add(s, s + 1, 1, 0);
		add(t - 1, t, 1, 0);
		printf("%d\n", -MCMF());
	}
	return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值