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][30];
 4 int main() {
 5     int n;
 6     while(~scanf("%d",&n)) {
 7         for(int i=1;i<=n;++i)
 8             for(int j=1;j<=n;++j)
 9                 scanf("%d",&ans[i][j]);
10         memset(dp,0,sizeof(dp));
11         dp[0][1][1]=ans[1][1];//一步都没走
12         for(int tot=1;tot<=n+n-2;++tot)//走了几步
13             for(int i=1;i<=n&&(i-1<=tot);++i)
14                 for(int j=1;j<=n&&(j-1<=tot);++j) {
15                     dp[tot][i][j]=max(dp[tot][i][j],dp[tot-1][i-1][j-1]);
16                     dp[tot][i][j]=max(dp[tot][i][j],dp[tot-1][i-1][j]);
17                     dp[tot][i][j]=max(dp[tot][i][j],dp[tot-1][i][j-1]);
18                     dp[tot][i][j]=max(dp[tot][i][j],dp[tot-1][i][j])+ans[i][tot+2-i]+ans[j][tot+2-j];
19                     if(i==j) dp[tot][i][j]-=ans[i][tot+2-i];
20                 }
21         printf("%d\n",dp[n+n-2][n][n]);
22     }
23     return 0;
24 }

时间: 2024-11-13 19:28:57

HDU 2686 Matrix 多线程dp的相关文章

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

HDU 4313 Matrix 树形dp

题意: 给定n个点的树,m个黑点 以下n-1行给出边和删除这条边的费用 以下m个黑点的点标[0,n-1] 删除一些边使得随意2个黑点都不连通. 问删除的最小花费. 思路: 树形dp 每一个点有2个状态,成为黑点或白点. 若本身这个点就是黑点那么仅仅有黑点一种状态. 否则能够觉得是子树中某个黑点转移上来. 所以dp[i][0]是i点为黑点的状态. #pragma comment(linker, "/STACK:1024000000,1024000000") #include <st