HDOJ 3376 Matrix Again

和HDOJ 2686 一样,只是范围不同

最大费用最大流。。。。。

与最小费用最大流的区别用////////////标出来了

对于detour,在源点和汇点处的边的流量为2

对于每个点只能经过一次,拆点,两个点直接建一条流量为1,费用为mp【i】【j】的边

对于每个点可以走到他的左边和下边:连一个费用为0流量大于1的边

Matrix Again

Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 102400/102400 K (Java/Others)

Total Submission(s): 2880    Accepted Submission(s): 846

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

HDOJ Monthly Contest – 2010.04.04

Recommend

lcy   |   We have carefully selected several similar problems for you:  3416 3081 3572 1853 1565

Statistic | Submit | Discuss | Note

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>

using namespace std;

const int maxn=800000;
const int INF=0x3f3f3f3f;
const int inf=9999;

struct Edge
{
	int to,next,cap,flow,cost;
}edge[4000000];

int Adj[maxn],Size,N,n;

void init()
{
	Size=0; memset(Adj,-1,sizeof(Adj));
}

void addedge(int u,int v,int cap,int cost)
{
	edge[Size].to=v;
	edge[Size].next=Adj[u];
	edge[Size].cost=cost;
	edge[Size].cap=cap;
	edge[Size].flow=0;
	Adj[u]=Size++;
}

void Add_Edge(int u,int v,int cap,int cost)
{
	//cout<<u<<" "<<v<<" "<<cap<<" "<<cost<<endl;
	addedge(u,v,cap,cost);
	addedge(v,u,0,-cost);
}

int dist[maxn],vis[maxn],pre[maxn];

bool spfa(int s,int t)
{
	queue<int> q;
	for(int i=0;i<n;i++)
	{
		dist[i]=-INF;vis[i]=false; pre[i]=-1;//////////////////////
	}
	dist[s]=0; vis[s]=true; q.push(s);
	while(!q.empty())
	{
		int u=q.front();
		q.pop();
		vis[u]=false;
		for(int i=Adj[u];~i;i=edge[i].next)
		{
			int v=edge[i].to;
			if(edge[i].cap>edge[i].flow&&
				dist[v]<dist[u]+edge[i].cost)/////////////////////
			{
				dist[v]=dist[u]+edge[i].cost;
				pre[v]=i;
				if(!vis[v])
				{
					vis[v]=true;
					q.push(v);
				}
			}
		}
	}
	if(pre[t]==-1) return false;
	return true;
}

int MinCostMaxFlow(int s,int t,int& cost)
{
	int flow=0;
	cost=0;
	while(spfa(s,t))
	{
		int Min=INF;
		for(int i=pre[t];~i;i=pre[edge[i^1].to])
		{
			if(Min>edge[i].cap-edge[i].flow)
				Min=edge[i].cap-edge[i].flow;
		}
		if(Min==0) break;
		for(int i=pre[t];~i;i=pre[edge[i^1].to])
		{
			edge[i].flow+=Min;
			edge[i^1].flow-=Min;
			cost+=edge[i].cost*Min;
		}
		flow+=Min;
	}
	return flow;
}

int mp[1000][1000];

int main()
{
	while(scanf("%d",&N)!=EOF)
	{
		for(int i=0;i<N;i++)
			for(int j=0;j<N;j++)
				scanf("%d",&mp[i][j]);
		init();
		for(int i=0;i<N;i++)
		{
			for(int j=0;j<N;j++)
			{
				int a=i*N+j;
				int b=a+N*N;
				Add_Edge(a,b,1,mp[i][j]);
				if((i==0&&j==0)||(i==N-1&&j==N-1))
				{
					Add_Edge(a,b,1,mp[i][j]);/////////////
				}
				if(j!=N-1)
				{
					Add_Edge(b,a+1,inf,0);
				}
				if(i!=N-1)
				{
					Add_Edge(b,a+N,inf,0);
				}
			}
		}
		int S=0,T=N*N*2-1;
		n=T+1;

		int FLOW,COST;
		FLOW=MinCostMaxFlow(S,T,COST);
		//cout<<"FLOW: "<<FLOW<<" COST: "<<COST<<endl;
		printf("%d\n",COST-mp[0][0]-mp[N-1][N-1]);///////////////////
	}
	return 0;
}
时间: 2024-08-25 08:58:41

HDOJ 3376 Matrix Again的相关文章

hdoj 3376 Matrix Again and hdoj 2686 Matrix 【最大费用最大流】

Matrix Again Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 102400/102400 K (Java/Others) Total Submission(s): 3453    Accepted Submission(s): 1017 Problem Description Starvae very like play a number game in the n*n Matrix. A positive intege

hdoj 3376,2686 Matrix Again 【最小费用最大流】

题目:hdoj 3376 Matrix Again 题意:给出一个m*n的矩阵,然后从左上角到右下角走两次,每次只能向右或者向下,出了末尾点其他只能走一次,不能交叉,每次走到一个格子拿走这个格子中的数字,求价值最大? 分析:很明显费用流,开始想的到一种建图方案,但是那样的话流量全为负值的话会成一个环,所以果断换了. 建图方案是: 首先拆点,每个点拆成两个i 和 ii ,建边,费用为当前格子的值,流量为1,初始点和末尾点为2 然后每个点向它的右边和下边分别建边,容量为1,费用为0 s 连接 左上角

POJ 2135 Farm Tour &amp;&amp; HDU 2686 Matrix &amp;&amp; HDU 3376 Matrix Again 费用流求来回最短路

累了就要写题解,最近总是被虐到没脾气. 来回最短路问题貌似也可以用DP来搞,不过拿费用流还是很方便的. 可以转化成求满流为2 的最小花费.一般做法为拆点,对于 i 拆为2*i 和 2*i+1,然后连一条流量为1(花费根据题意来定) 的边来控制每个点只能通过一次. 额外添加source和sink来控制满流为2. 代码都雷同,以HDU3376为例. #include <algorithm> #include <iostream> #include <cstring> #in

HDU 2686 Matrix 3376 Matrix Again(费用流)

HDU 2686 Matrix 题目链接 3376 Matrix Again 题目链接 题意:这两题是一样的,只是数据范围不一样,都是一个矩阵,从左上角走到右下角在从右下角走到左上角能得到最大价值 思路:拆点,建图,然后跑费用流即可,不过HDU3376这题,极限情况是300W条边,然后卡时间过了2333 代码: #include <cstdio> #include <cstring> #include <vector> #include <queue> #i

HDU 3376 Matrix Again

Matrix Again Time Limit: 2000ms Memory Limit: 102400KB This problem will be judged on HDU. Original ID: 337664-bit integer IO format: %I64d      Java class name: Main Starvae very like play a number game in the n*n Matrix. A positive integer number i

HDU 3376 Matrix Again(最大费用最大流)HDU2686加强题

Matrix Again Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 102400/102400 K (Java/Others) Total Submission(s): 3206    Accepted Submission(s): 937 Problem Description Starvae very like play a number game in the n*n Matrix. A positive integer

hdu 3376 Matrix Again【最大费用流】

Matrix Again Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 102400/102400 K (Java/Others) Total Submission(s): 2947    Accepted Submission(s): 860 Problem Description Starvae very like play a number game in the n*n Matrix. A positive integer

HDOJ 233 Matrix 5015【矩阵快速幂】

233 Matrix Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 1355    Accepted Submission(s): 806 Problem Description In our daily life we often use 233 to express our feelings. Actually, we may

HDOJ 5671 Matrix

Matrix Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 780    Accepted Submission(s): 330 Problem Description There is a matrix M that has n rows and m columns (1≤n≤1000,1≤m≤1000).Then we per