[hdu 2686]Matrix

网上说这道题的题解是费用流

我粗粗看了一下数据范围,觉得出题者似乎是让我们用 “大(d)屁(p)” 的样子,为了尊重出题人,我还是谢谢吧喵~

首先,一条回路可以看做是两条路齐头并进,这是 大屁 和 费永柳 的共同思想

故我们可用一条条次对角线(即 x+y=k  的对角线)来划分状态

考虑枚举对角线上两个点 x1,y1,x2,y2
很大力地用 f[x1][y1][x2][y2] 表示两条路分别走到 点(x1, y1)和 点(x2, y2)时的最佳方案

这样 f[x1][y1][x2][y2] 就可以由 f[x1-1][y1][x2-1][y2], f[x1-1][y1][x2][y2-1], f[x1][y1-1][x2-1][y2], f[x1][y1-1][x2][y2-1] 来得到(丧心病狂)

还有当 点(x1, y1)和 点(x2, y2)在对角线上相邻时情况特殊,因为 点(x1-1, y1)和 点(x2, y2-1)重合了,需要特别考虑(真是丧心病狂)

虽然看上去很恶心,但我觉得再怎么说短也是一大优势,你的费用流有这么短喵?

#include <cstdio>
#include <cstring>
#define max(x, y) ((x)>(y) ? (x):(y))
const int size=32;

int n;
int a[size][size];
int f[size][size][size][size];
inline int getint();
inline void putint(int);

int main()
{
	while (scanf("%d", &n)!=EOF)
	{
		for (int i=1;i<=n;i++)
			for (int j=1;j<=n;j++)
				a[i][j]=getint();
		memset(f, 0, sizeof f);
		f[2][1][1][2]=a[1][1]+a[1][2]+a[2][1];
		for (int i=4;i<2*n;i++)
			for (int y1=i<=n+1?1:i-n;y1<i-1 && y1<n;y1++)
				for (int y2=y1+1;y2<i && y2<=n;y2++)
				{
					int x1=i-y1, x2=i-y2;
					if (y2-y1==1) f[x1][y1][x2][y2]=max(max(f[x1][y1-1][x2][y1], f[x2][y1][x2-1][y2]), f[x1][y1-1][x2-1][y2]);
					else f[x1][y1][x2][y2]=max(max(f[x1-1][y1][x2-1][y2], f[x1-1][y1][x2][y2-1]), max(f[x1][y1-1][x2-1][y2], f[x1][y1-1][x2][y2-1]));
					f[x1][y1][x2][y2]+=a[x1][y1]+a[x2][y2];
				}
		putint(a[n][n]+f[n][n-1][n-1][n]);
	}

	return 0;
}
inline int getint()
{
	register int num=0;
	register char ch;
	do ch=getchar(); while (ch<‘0‘ || ch>‘9‘);
	do num=num*10+ch-‘0‘, ch=getchar(); while (ch>=‘0‘ && ch<=‘9‘);
	return num;
}
inline void putint(int num)
{
	char stack[15];
	register int top=0;
	for ( ;num;num/=10) stack[++top]=num%10+‘0‘;
	for ( ;top;top--) putchar(stack[top]);
	putchar(‘\n‘);
}
时间: 2024-08-08 09:25:37

[hdu 2686]Matrix的相关文章

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(最大费用最大流+拆点)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2686 和POJ3422一样 删掉K把汇点与源点的容量改为2(因为有两个方向的选择)即可 #include <iostream> #include <cstdlib> #include <cstdio> #include <cstring> #include <queue> #include <algorithm> const int ma

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 2686 Matrix &amp;&amp; hdu 3367 Matrix Again (最大费用最大流)

Matrix Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1394    Accepted Submission(s): 758 Problem Description Yifenfei very like play a number game in the n*n Matrix. A positive integer number

hdu 2686 Matrix 最小费用最大流

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2686 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

HDU 2686 Matrix 多线程dp

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2686 思路:多线程dp,参考51Nod 1084:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1084 注:这道题用滚动数组优化反而WA,压到三维即可 代码: 1 #include <bits/stdc++.h> 2 using namespace std; 3 int ans[30][30],dp[60][30][3

HDU 2686 Matrix(最大费用流)

Matrix Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1890    Accepted Submission(s): 1005 Problem Description Yifenfei very like play a number game in the n*n Matrix. A positive integer numbe

hdu 2686 Matrix【最大费用流】

Matrix Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1792    Accepted Submission(s): 958 Problem Description Yifenfei very like play a number game in the n*n Matrix. A positive integer number

HDU 4902 Matrix multiplication

点击打开链接 Matrix multiplication Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 2113    Accepted Submission(s): 956 Problem Description Given two matrices A and B of size n×n, find the product o