Gym - 101147H H. Commandos DAG

H - 记忆里在微甜

  Gym - 101147H 


commandos.in / standard output
Statements

A commando is a soldier of an elite light infantry often specializing in amphibious landings, abseiling or parachuting. This time our Commando unit wants to free as many hostages as it could from a hotel in Byteland, This hotel contains 10 identical floors numbered from 1 to 10 each one is a suite of 10 by 10 symmetric square rooms, our unit can move from a room (F, Y, X) to the room right next to it (F, Y, X + 1) or front next to it (F, Y + 1, X) and it can also use the cooling system to move to the room underneath it (F - 1, Y, X).

Knowing that our unit parachuted successfully in room 1-1 in floor 10 with a map of hostages locations try to calculate the maximum possible hostages they could save.

Input

Your program will be tested on one or more test cases. The first line of the input will be a single integer T. Followed by the test cases, each test case contains a number N (1 ≤ N ≤ 1, 000) representing the number of lines that follows. Each line contains 4 space separated integers (1 ≤ F, Y, X, H ≤ 10) means in the floor number F room Y-X there are H hostages.

Output

For each test case, print on a single line, a single number representing the maximum possible hostages that they could save.

Example
Input
2
3
10 5 5 1
10 5 9 5
10 9 5 9
3
1 5 5 1
5 5 9 5
5 9 5 8
Output
10
8


Source Gym - 101147H
My Solution 题意:即普通的DAG加了一维。 DAG上的dp 三维的有向无环图上dp, dp[k][i][j] = max(dp[k][i][j], dp[k][i+1][j] + mp[k][i][j]);
dp[k][i][j] = max(dp[k][i][j], dp[k][i][j+1] + mp[k][i][j]);
dp[k][i][j] = max(dp[k][i][j], dp[k-1][i][j] + mp[k][i][j]);
复杂度 O(n^3)
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
typedef long long LL;
const int maxn = 1e6 + 8;

int dp[12][12][12], mp[12][12][12];

int main()
{
    freopen("commandos.in", "r", stdin);
    #ifdef LOCAL
    freopen("h.txt", "r", stdin);
    //freopen("h.out", "w", stdout);

    #endif // LOCAL
    ios::sync_with_stdio(false); cin.tie(0);
    int T, n, f, y, x, i, j, k;
    cin >> T;
    while(T--){
        memset(dp, 0, sizeof dp);
        memset(mp, 0, sizeof mp);
        cin >> n;
        for(i = 1; i <= n; i++){
            cin >> f >> y >> x;
            cin >> mp[f][y][x];
        }
        for(k = 1; k <= 10; k++){
            for(i = 10; i >= 1; i--){
                for(j = 10; j >= 1; j--){
                    dp[k][i][j] = max(dp[k][i][j], dp[k][i+1][j] + mp[k][i][j]);
                    dp[k][i][j] = max(dp[k][i][j], dp[k][i][j+1] + mp[k][i][j]);
                    dp[k][i][j] = max(dp[k][i][j], dp[k-1][i][j] + mp[k][i][j]);
                }
            }

        }
        cout << dp[10][1][1] << "\n";

    }
    return 0;
}

Thank you!                                                                                                                                              ------from  ProLights

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值