Codeforces 762D. Maximum path 题目详解+错点记录

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Goseqh/article/details/56059693

Codeforces 762D. Maximum path


首先,这是一道DP题,虽然说有简便的做法,但我还是采用了插头DP的方法(不知道是不是插头DP,可能是轮廓线DP)

Problem

You are given a rectangular table 3 × n. Each cell contains an integer. You can move from one cell to another if they share a side.

Find such path from the upper left cell to the bottom right cell of the table that doesn’t visit any of the cells twice, and the sum of numbers written in the cells of this path is maximum possible.

Input

The first line contains an integer n (1 ≤ n ≤ 105) — the number of columns in the table.

Next three lines contain n integers each — the description of the table. The j-th number in the i-th line corresponds to the cell aij ( - 109 ≤ aij ≤ 109) of the table.

Output

Output the maximum sum of numbers on a path from the upper left cell to the bottom right cell of the table, that doesn’t visit any of the cells twice.

Examples

Input

3
1 1 1
1 -1 1
1 1 1

Output

7

Input

5
10 10 10 -1 -1
-1 10 10 10 10
-1 10 10 10 10

Output

110

Note

The path for the first example:

这里写图片描述

The path for the second example:

这里写图片描述

好了,那么这道题其实就是要求不重复经过某个方格,求从左上角到右下角的路径上权值和最大值。注意本题方格中的权值可以为负

思路

我曾经参加过这次比赛,然而时间太短写不完。我知道本题应该有简便的DP方法,但我觉得正常的思路还是先使用插头DP(还是轮廓线DP)的方法。因为本题是一个窄棋盘(3*n)所以我们可以枚举一个竖列的所有情况,作为参数,进行状态转移

代码

不多说,贴代码(使用这种方法写的代码十分丑。。。)

#include<cstdio>
#include<cstdlib>
#define maxn 100005
#define maxn2 18
#define LL long long int
#define k1 ans=max(ans,DP(pos+1,1)+geo[pos+1][0])    
#define k2 ans=max(ans,DP(pos+1,2)+geo[pos+1][1])    
#define k3 ans=max(ans,DP(pos+1,3)+geo[pos+1][2])    
//#define k4 ans=max(ans,DP(pos+1,4)+geo[pos+1][0]+geo[pos+1][1])    
//#define k5 ans=max(ans,DP(pos+1,5)+geo[pos+1][0]+geo[pos+1][2])    
//#define k6 ans=max(ans,DP(pos+1,6)+geo[pos+1][2]+geo[pos+1][1])    
#define k7 ans=max(ans,DP(pos+1,7)+geo[pos+1][0]+geo[pos+1][1]+geo[pos+1][2])    
#define k8 ans=max(ans,DP(pos+1,8)+geo[pos+1][0]+geo[pos+1][1]+geo[pos+1][2])    
#define k10 ans=max(ans,DP(pos+1,10)+geo[pos+1][0]+geo[pos+1][1]+geo[pos+1][2])    
#define k11 ans=max(ans,DP(pos+1,11)+geo[pos+1][0]+geo[pos+1][1]+geo[pos+1][2])    
#define k12 ans=max(ans,DP(pos+1,12)+geo[pos+1][0]+geo[pos+1][1])    
#define k13 ans=max(ans,DP(pos+1,13)+geo[pos+1][0]+geo[pos+1][1]+geo[pos+1][2])    
#define k14 ans=max(ans,DP(pos+1,14)+geo[pos+1][2]+geo[pos+1][1])    
#define k15 ans=max(ans,DP(pos+1,15)+geo[pos+1][0]+geo[pos+1][1])    
#define k16 ans=max(ans,DP(pos+1,16)+geo[pos+1][0]+geo[pos+1][1]+geo[pos+1][2])    
#define k17 ans=max(ans,DP(pos+1,17)+geo[pos+1][2]+geo[pos+1][1])    
#define k18 ans=max(ans,DP(pos+1,18)+geo[pos+1][0]+geo[pos+1][1]+geo[pos+1][2])
using namespace std;
LL max(LL a,LL b){
    if(a>b)return a;
    return b;
}
/*
Stable......
    1      2      3      4      5      6      7      8    NULL    10     11     12     13     14     15     16     17     18            
-------------------------------------------------------------------------------------------------------------------------------
|      |      |      |      |      |      |      |      |      |      |      |      |      |      |      |      |      |      |
|******|      |      |******|******|      |******|****  |***   |******|   ***|****  |****  |      |   ***|   ***|      |******|
|      |      |      |      |      |      |      |   *  |  *   |      |   *  |   *  |   *  |      |   *  |   *  |      |      |
-----------------------------------------------------*-----*--------------*------*------*-------------*------*-----------------
|      |      |      |      |      |      |      |   *  |  *   |      |   *  |   *  |   *  |      |   *  |   *  |      |      |
|      |******|      |******|      |******|****  |****  |  *   |   ***|   ***|   ***|   *  |****  |****  |   *  |   ***|******|
|      |      |      |      |      |      |   *  |      |  *   |   *  |      |      |   *  |   *  |      |   *  |   *  |      |   
----------------------------------------------*------------*-------*--------------------*------*-------------*------*-----------
|      |      |      |      |      |      |   *  |      |  *   |   *  |      |      |   *  |   *  |      |   *  |   *  |      |  
|      |      |******|      |******|******|****  |******|***   |   ***|******|      |   ***|   ***|      |****  |****  |******|
|      |      |      |      |      |      |      |      |      |      |      |      |      |      |      |      |      |      |
-------------------------------------------------------------------------------------------------------------------------------------------
*/
LL INF;
LL dp[maxn][maxn2];
LL geo[maxn][3];
int n;
LL DP(int pos,int op){
    if(dp[pos][op])return dp[pos][op];
    LL& ans=dp[pos][op]=-INF;
    if(pos==n-1){
        if(op==3||op==8||op==13||op==14)return ans=0;
        return ans;
    }
    else{
        switch(op){
            case 1:{
                k1;k10;k12;k13;
                return ans;
                break;
            }
            case 2:{
                k2;k14;k15;
                return ans;
                break;
            }case 3:{
                k3;k11;k16;k17;
                return ans;
                break;
            }case 7:{
                k1;k10;k12;k13;
                return ans;
                break;
            }case 8:{
                k3;k11;k16;k17;
                return ans;
                break;
            }case 10:{
                k18;k8;
                return ans;
                break;
            }case 11:{
                k18;k7;
                return ans;   
                break;
            }case 12:{
                k2;k14;k15;
                return ans;
                break;
            }case 13:{
                k3;k11;k16;k17;
                return ans;
                break;
            }case 14:{
                k3;k11;k16;k17;
                return ans;
                break;
            }case 15:{
                k1;k10;k12;k13;
                return ans;
                break;
            }case 16:{
                k1;k10;k12;k13;
                return ans;
                break;
            }case 17:{
                k2;k14;k15;
                return ans;
                break;
            }case 18:{
                k7;k8;k18;
                return ans;
                break;
            }
        }   
    return ans;
    }
}
int main(){
    /*freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);*/
    scanf("%d",&n);
    for(int i=0;i<3;i++){
        for(int j=0;j<n;j++){
            scanf("%I64d",&geo[j][i]);
        }
    }
    INF=2;
    for(int i=0;i<17;i++)INF*=10;
    LL ans=-INF;
    int pos=-1;
    k1;k10;k12;k13;
    printf("%I64d",ans);
    return 0;
}

上面的那堆乱乱的东西,其实是一个状态表(而且很好看),只是博客宽度太短,装不下

错点记录

  1. 对于define语句中的常量,不能过大,也不能作为返回值直接使用,因为这样会被强转为int型,要计算INF

别的。。。也没什么了

Delay Constrained Maximum Capacity Path

07-11

Problem DescriptionnConsider an undirected graph with N vertices, numbered from 1 to N, and M edges. The vertex numbered with 1 corresponds to a mine from where some precious minerals are extracted. The vertex numbered with N corresponds to a minerals processing factory. Each edge has an associated travel time (in time units) and capacity (in units of minerals). It has been decided that the minerals which are extracted from the mine will be delivered to the factory using a single path. This path should have the highest capacity possible, in order to be able to transport simultaneously as many units of minerals as possible. The capacity of a path is equal to the smallest capacity of any of its edges. However, the minerals are very sensitive and, once extracted from the mine, they will start decomposing after T time units, unless they reach the factory within this time interval. Therefore, the total travel time of the chosen path (the sum of the travel times of its edges) should be less or equal to T.n nnInputnThe first line of input contains an integer number X, representing the number of test cases to follow. The first line of each test case contains 3 integer numbers, separated by blanks: N (2 <= N <= 10.000), M (1 <= M <= 50.000) and T (1 <= T <= 500.000). Each of the next M lines will contain four integer numbers each, separated by blanks: A, B, C and D, meaning that there is an edge between vertices A and B, having capacity C (1 <= C <= 2.000.000.000) and the travel time D (1 <= D <= 50.000). A and B are different integers between 1 and N. There will exist at most one edge between any two vertices.n nnOutputnFor each of the X test cases, in the order given in the input, print one line containing the highest capacity of a path from the mine to the factory, considering the travel time constraint. There will always exist at least one path between the mine and the factory obbeying the travel time constraint.n nnSample Inputn2n2 1 10n1 2 13 10n4 4 20n1 2 1000 15n2 4 999 6n1 3 100 15n3 4 99 4n nnSample Outputn13n99

没有更多推荐了,返回首页

私密
私密原因:
请选择设置私密原因
  • 广告
  • 抄袭
  • 版权
  • 政治
  • 色情
  • 无意义
  • 其他
其他原因:
120
出错啦
系统繁忙,请稍后再试