답안 #218213

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
218213 2020-04-01T14:22:10 Z SamAnd Fireworks (APIO16_fireworks) C++17
26 / 100
29 ms 1920 KB
#include <bits/stdc++.h>
using namespace std;
#define m_p make_pair
const int N = 302;
const long long INF = 1000000009000000009;

int n, m;
int p[N];
vector<pair<int, int> > a[N];

long long dp[N][N];
long long dpp[N][N];
void dfs(int x)
{
    if (x > n)
    {
        dp[x][0] = 0;
        return;
    }
    for (int i = 0; i < a[x].size(); ++i)
    {
        int h = a[x][i].first;
        dfs(h);
        for (int j = 0; j < N; ++j)
        {
            for (int k = 0; j + k < N; ++k)
            {
                dpp[h][j + k] = min(dpp[h][j + k], dp[h][j] + abs(a[x][i].second - k));
            }
        }
    }
    for (int j = 0; j < N; ++j)
    {
        dp[x][j] = 0;
        for (int i = 0; i < a[x].size(); ++i)
        {
            int h = a[x][i].first;
            dp[x][j] += dpp[h][j];
            dp[x][j] = min(dp[x][j], INF);
        }
    }
}

int main()
{
    scanf("%d%d", &n, &m);
    for (int i = 2; i <= n; ++i)
    {
        int d;
        scanf("%d%d", &p[i], &d);
        a[p[i]].push_back(m_p(i, d));
    }
    for (int i = n + 1; i <= n + m; ++i)
    {
        int d;
        scanf("%d%d", &p[i], &d);
        a[p[i]].push_back(m_p(i, d));
    }
    if (n == 1)
    {
        long long ans = INF;
        for (int i = 0; i < a[1].size(); ++i)
        {
            long long yans = 0;
            for (int j = 0; j < a[1].size(); ++j)
                yans += abs(a[1][i].second - a[1][j].second);
            ans = min(ans, yans);
        }
        printf("%lld\n", ans);
        return 0;
    }
    for (int i = 1; i <= n + m; ++i)
    {
        for (int j = 0; j < N; ++j)
            dpp[i][j] = dp[i][j] = INF;
    }
    dfs(1);
    long long ans = INF;
    for (int j = 0; j < N; ++j)
        ans = min(ans, dp[1][j]);
    printf("%lld\n", ans);
    return 0;
}

Compilation message

fireworks.cpp: In function 'void dfs(int)':
fireworks.cpp:20:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for (int i = 0; i < a[x].size(); ++i)
                     ~~^~~~~~~~~~~~~
fireworks.cpp:35:27: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         for (int i = 0; i < a[x].size(); ++i)
                         ~~^~~~~~~~~~~~~
fireworks.cpp: In function 'int main()':
fireworks.cpp:62:27: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         for (int i = 0; i < a[1].size(); ++i)
                         ~~^~~~~~~~~~~~~
fireworks.cpp:65:31: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
             for (int j = 0; j < a[1].size(); ++j)
                             ~~^~~~~~~~~~~~~
fireworks.cpp:46:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d%d", &n, &m);
     ~~~~~^~~~~~~~~~~~~~~~
fireworks.cpp:50:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d%d", &p[i], &d);
         ~~~~~^~~~~~~~~~~~~~~~~~~
fireworks.cpp:56:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d%d", &p[i], &d);
         ~~~~~^~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 384 KB Output is correct
2 Correct 4 ms 384 KB Output is correct
3 Correct 5 ms 384 KB Output is correct
4 Correct 4 ms 384 KB Output is correct
5 Correct 5 ms 384 KB Output is correct
6 Correct 5 ms 384 KB Output is correct
7 Correct 4 ms 384 KB Output is correct
8 Correct 5 ms 384 KB Output is correct
9 Correct 5 ms 384 KB Output is correct
10 Correct 4 ms 384 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 384 KB Output is correct
2 Correct 7 ms 512 KB Output is correct
3 Correct 8 ms 640 KB Output is correct
4 Correct 11 ms 768 KB Output is correct
5 Correct 12 ms 896 KB Output is correct
6 Correct 13 ms 896 KB Output is correct
7 Correct 15 ms 1024 KB Output is correct
8 Correct 16 ms 1152 KB Output is correct
9 Correct 18 ms 1280 KB Output is correct
10 Correct 20 ms 1408 KB Output is correct
11 Correct 20 ms 1408 KB Output is correct
12 Correct 22 ms 1536 KB Output is correct
13 Correct 23 ms 1664 KB Output is correct
14 Correct 29 ms 1792 KB Output is correct
15 Correct 24 ms 1792 KB Output is correct
16 Correct 25 ms 1920 KB Output is correct
17 Correct 25 ms 1792 KB Output is correct
18 Correct 25 ms 1792 KB Output is correct
19 Correct 25 ms 1792 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 384 KB Output is correct
2 Correct 4 ms 384 KB Output is correct
3 Correct 5 ms 384 KB Output is correct
4 Correct 4 ms 384 KB Output is correct
5 Correct 5 ms 384 KB Output is correct
6 Correct 5 ms 384 KB Output is correct
7 Correct 4 ms 384 KB Output is correct
8 Correct 5 ms 384 KB Output is correct
9 Correct 5 ms 384 KB Output is correct
10 Correct 4 ms 384 KB Output is correct
11 Correct 5 ms 384 KB Output is correct
12 Correct 7 ms 512 KB Output is correct
13 Correct 8 ms 640 KB Output is correct
14 Correct 11 ms 768 KB Output is correct
15 Correct 12 ms 896 KB Output is correct
16 Correct 13 ms 896 KB Output is correct
17 Correct 15 ms 1024 KB Output is correct
18 Correct 16 ms 1152 KB Output is correct
19 Correct 18 ms 1280 KB Output is correct
20 Correct 20 ms 1408 KB Output is correct
21 Correct 20 ms 1408 KB Output is correct
22 Correct 22 ms 1536 KB Output is correct
23 Correct 23 ms 1664 KB Output is correct
24 Correct 29 ms 1792 KB Output is correct
25 Correct 24 ms 1792 KB Output is correct
26 Correct 25 ms 1920 KB Output is correct
27 Correct 25 ms 1792 KB Output is correct
28 Correct 25 ms 1792 KB Output is correct
29 Correct 25 ms 1792 KB Output is correct
30 Incorrect 8 ms 1152 KB Output isn't correct
31 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 384 KB Output is correct
2 Correct 4 ms 384 KB Output is correct
3 Correct 5 ms 384 KB Output is correct
4 Correct 4 ms 384 KB Output is correct
5 Correct 5 ms 384 KB Output is correct
6 Correct 5 ms 384 KB Output is correct
7 Correct 4 ms 384 KB Output is correct
8 Correct 5 ms 384 KB Output is correct
9 Correct 5 ms 384 KB Output is correct
10 Correct 4 ms 384 KB Output is correct
11 Correct 5 ms 384 KB Output is correct
12 Correct 7 ms 512 KB Output is correct
13 Correct 8 ms 640 KB Output is correct
14 Correct 11 ms 768 KB Output is correct
15 Correct 12 ms 896 KB Output is correct
16 Correct 13 ms 896 KB Output is correct
17 Correct 15 ms 1024 KB Output is correct
18 Correct 16 ms 1152 KB Output is correct
19 Correct 18 ms 1280 KB Output is correct
20 Correct 20 ms 1408 KB Output is correct
21 Correct 20 ms 1408 KB Output is correct
22 Correct 22 ms 1536 KB Output is correct
23 Correct 23 ms 1664 KB Output is correct
24 Correct 29 ms 1792 KB Output is correct
25 Correct 24 ms 1792 KB Output is correct
26 Correct 25 ms 1920 KB Output is correct
27 Correct 25 ms 1792 KB Output is correct
28 Correct 25 ms 1792 KB Output is correct
29 Correct 25 ms 1792 KB Output is correct
30 Incorrect 8 ms 1152 KB Output isn't correct
31 Halted 0 ms 0 KB -