HDU-2686 Matrix(多线程DP?)2017寒假集训

题意:给一个矩阵,求(1,1)到(n,n)的两条路径(不能相交),求能取到的最大值

数据范围:2 <= n <= 30,矩阵上的数 < 100

思路:记得在kuangbin最短路专题里有个要求差不多的题,当时写不出来,查题解是最大流????

然后问了大佬,说这题也可以网络流做,不过呢。。。然后我就了解到了这个叫多线程DP的东西

多线程。。????这不是C语言的那啥玩意。。。吗?课设好像还要用呢orz

其实这里是指DP数组存分别在两个点的状态能走出多少的最大值,走的时候不走到一个点上就行了,用记忆化搜索写很方便很好理解就跟爆搜的差不多

用递推还可以省出一维的空间,设当前是第k步,每次从k-1转移过来,然后恒有x+y==k就可以只设三维的状态了

自己写的是记忆化,毕竟n只有30

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<algorithm>
 4 using namespace std;
 5
 6 const int mx = 35;
 7 int a[mx][mx], dp[mx][mx][mx][mx], n;
 8 int dr[] = {1, 0, 1, 0};
 9 int dc[] = {0, 1, 0, 1};
10 int du[] = {1, 1, 0, 0};
11 int dv[] = {0, 0, 1, 1};
12
13 int dfs(int x, int y, int u, int v){
14     if (a[x][y] == -1 || a[u][v] == -1) return 0;
15     if (x+y == 2*n) return a[n][n];
16     if (dp[x][y][u][v] != -1) return dp[x][y][u][v];
17     if (x == u && y == v && x+y > 2) return 0;
18     int ans = 0;
19     for (int i = 0; i < 4; i++){
20         int dx = x + dr[i];
21         int dy = y + dc[i];
22         int r = u + du[i];
23         int c = v + dv[i];
24         ans = max(ans, dfs(dx, dy, r, c));
25     }
26     return dp[x][y][u][v] = ans+a[x][y]+a[u][v];
27 }
28
29 int main(){
30     while (scanf("%d", &n) == 1){
31         memset(dp, -1, sizeof dp);
32         memset(a, -1, sizeof a);
33         for (int i = 1; i <= n; i++)
34             for (int j = 1; j <= n; j++)
35                 scanf("%d", &a[i][j]);
36         printf("%d\n", dfs(1, 1, 1, 1)-a[1][1]);
37     }
38     return 0;
39 }

原文地址:https://www.cnblogs.com/QAQorz/p/9035479.html

时间: 2024-10-28 14:19:08

HDU-2686 Matrix(多线程DP?)2017寒假集训的相关文章

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

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 最小费用最大流

题目链接: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 5569 matrix(简单dp)

Problem Description Given a matrix with n rows and m columns ( n+m is an odd number ), at first , you begin with the number at top-left corner (1,1) and you want to go to the number at bottom-right corner (n,m). And you must go right or go down every

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