답안 #870084

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
870084 2023-11-06T22:42:53 Z StefanL2005 꿈 (IOI13_dreaming) C++14
32 / 100
519 ms 17784 KB
#include <bits/stdc++.h>
#include "dreaming.h"
using namespace std;

int k = 0;
 
void DFS_sum(int node, int p, vector<int> &sum, vector<vector<int>> &vec, vector<vector<int>> &cost, vector<int> &col)
{
    col[node] = k;
    for (int i = 0; i < vec[node].size(); i++)
    {
        if (vec[node][i] == p)
            continue;
 
        DFS_sum(vec[node][i], node, sum, vec, cost, col);
        sum[node] = max(sum[node], sum[vec[node][i]] + cost[node][i]);
    }
}

void DFS_main(int node, int p, int main, int sec, vector<int> &sum, int &ans, vector<vector<int>> &vec, vector<vector<int>> &cost)
{
    ans = min(ans, max(main, sec));
    if (sec > main)
        return;
    int nr = 0;
    for (int i = 0; i < vec[node].size(); i++)
    {
        if (vec[node][i] == p)
            continue;
        int temp = sum[vec[node][i]] + cost[node][i];

        if (temp == main)
        {
            nr++;
            continue;
        }
        if (temp > sec && (temp != main || nr >= 1))
            sec = temp; 
    }

    for (int i = 0; i < vec[node].size(); i++)
    {
        if (vec[node][i] == p)
            continue;
        int temp = sum[vec[node][i]] + cost[node][i];

        if (temp == main)
            DFS_main(vec[node][i], node, main - cost[node][i], sec + cost[node][i], sum, ans, vec, cost);
    }
}
 
pair<int, int> graph_hub(int node, vector<vector<int>> &vec, vector<vector<int>> &cost, vector<int> &col)
{
    vector<int> sum(vec.size(), 0);
    DFS_sum(node, -1, sum, vec, cost, col);
    
    int MAX = 0, sec = 0;
    for (int i = 0; i < vec[node].size(); i++)
    {
        int temp = sum[vec[node][i]] + cost[node][i];
 
        if (temp > MAX)
        {
            sec = MAX;
            MAX = temp;
        }
        else
            sec = max(sec, temp);
    }

    int ans = sum[node];
    DFS_main(node, -1, MAX, sec, sum, ans, vec, cost);

    return make_pair(ans, MAX + sec);
}
int travelTime(int N, int M, int L, int A[], int B[], int C[])
{
    vector<int> col(N, -1);
    vector<vector<int>> vec(N), cost(N);
    for (int i = 0; i < M; i++)
    {
        vec[A[i]].push_back(B[i]);
        vec[B[i]].push_back(A[i]);
        cost[A[i]].push_back(C[i]);
        cost[B[i]].push_back(C[i]);
    }

    if (M == N - 1)
    {
        vector<int> sum(N, 0);
        DFS_sum(0, -1, sum, vec, cost, col);
 
        int unos = 0, dos = 0;
 
        for (int i = 0; i < vec[0].size(); i++)
       {
            int temp = sum[vec[0][i]] + cost[0][i];
            if (temp > unos)
            {
                dos = unos;
                unos = temp;
             }
            else
                dos = max(dos, temp);
        }
        return unos + dos;
    }

    vector<pair<int, int>> hub_l;
    for (int i = 0; i < N; i++)
        if (col[i] == -1)
        {
            hub_l.push_back(graph_hub(i, vec, cost, col));
            k++;
        }
    
    int unos = 0, dos = 0, tres = 0;

    for (int i = 0; i < hub_l.size(); i++)
    {
        int p = hub_l[i].first;

        if (p > unos)
        {
            tres = dos;
            dos = unos;
            unos = p;
        }
        else
            if (p > dos)
            {
                tres = dos;
                dos = p;
            }
            else
                tres = max(tres, p);
    }
    int Max = 0;
    for (int i = 0; i < hub_l.size(); i++)
        Max = max(Max, hub_l[i].second);
    
    if (M == N - 2)
        return max(unos + L + dos, Max);
    return max(unos + L + dos, max(Max, dos + 2 * L + tres));
}

Compilation message

dreaming.cpp: In function 'void DFS_sum(int, int, std::vector<int>&, std::vector<std::vector<int> >&, std::vector<std::vector<int> >&, std::vector<int>&)':
dreaming.cpp:10:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   10 |     for (int i = 0; i < vec[node].size(); i++)
      |                     ~~^~~~~~~~~~~~~~~~~~
dreaming.cpp: In function 'void DFS_main(int, int, int, int, std::vector<int>&, int&, std::vector<std::vector<int> >&, std::vector<std::vector<int> >&)':
dreaming.cpp:26:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   26 |     for (int i = 0; i < vec[node].size(); i++)
      |                     ~~^~~~~~~~~~~~~~~~~~
dreaming.cpp:41:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   41 |     for (int i = 0; i < vec[node].size(); i++)
      |                     ~~^~~~~~~~~~~~~~~~~~
dreaming.cpp: In function 'std::pair<int, int> graph_hub(int, std::vector<std::vector<int> >&, std::vector<std::vector<int> >&, std::vector<int>&)':
dreaming.cpp:58:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   58 |     for (int i = 0; i < vec[node].size(); i++)
      |                     ~~^~~~~~~~~~~~~~~~~~
dreaming.cpp: In function 'int travelTime(int, int, int, int*, int*, int*)':
dreaming.cpp:95:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   95 |         for (int i = 0; i < vec[0].size(); i++)
      |                         ~~^~~~~~~~~~~~~~~
dreaming.cpp:119:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  119 |     for (int i = 0; i < hub_l.size(); i++)
      |                     ~~^~~~~~~~~~~~~~
dreaming.cpp:139:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  139 |     for (int i = 0; i < hub_l.size(); i++)
      |                     ~~^~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 42 ms 16872 KB Output is correct
2 Correct 43 ms 17784 KB Output is correct
3 Correct 29 ms 13908 KB Output is correct
4 Correct 6 ms 2908 KB Output is correct
5 Correct 5 ms 2140 KB Output is correct
6 Correct 10 ms 4296 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 20 ms 7856 KB Output is correct
9 Correct 26 ms 11204 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 37 ms 13600 KB Output is correct
12 Correct 44 ms 15708 KB Output is correct
13 Correct 1 ms 600 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 344 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Incorrect 0 ms 348 KB Output isn't correct
10 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 42 ms 16872 KB Output is correct
2 Correct 43 ms 17784 KB Output is correct
3 Correct 29 ms 13908 KB Output is correct
4 Correct 6 ms 2908 KB Output is correct
5 Correct 5 ms 2140 KB Output is correct
6 Correct 10 ms 4296 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 20 ms 7856 KB Output is correct
9 Correct 26 ms 11204 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 37 ms 13600 KB Output is correct
12 Correct 44 ms 15708 KB Output is correct
13 Correct 1 ms 600 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 ms 344 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Incorrect 0 ms 348 KB Output isn't correct
23 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 286 ms 10460 KB Output is correct
2 Correct 287 ms 10468 KB Output is correct
3 Correct 299 ms 10608 KB Output is correct
4 Correct 291 ms 10468 KB Output is correct
5 Correct 287 ms 10468 KB Output is correct
6 Correct 319 ms 11472 KB Output is correct
7 Correct 317 ms 10932 KB Output is correct
8 Correct 273 ms 10444 KB Output is correct
9 Correct 277 ms 10392 KB Output is correct
10 Correct 304 ms 10824 KB Output is correct
11 Correct 1 ms 344 KB Output is correct
12 Correct 438 ms 7080 KB Output is correct
13 Correct 441 ms 7080 KB Output is correct
14 Correct 466 ms 6984 KB Output is correct
15 Correct 465 ms 6876 KB Output is correct
16 Correct 519 ms 6844 KB Output is correct
17 Correct 451 ms 7052 KB Output is correct
18 Correct 451 ms 7276 KB Output is correct
19 Correct 470 ms 6892 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 1 ms 604 KB Output is correct
23 Correct 451 ms 7000 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 344 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Incorrect 0 ms 348 KB Output isn't correct
10 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 42 ms 16872 KB Output is correct
2 Correct 43 ms 17784 KB Output is correct
3 Correct 29 ms 13908 KB Output is correct
4 Correct 6 ms 2908 KB Output is correct
5 Correct 5 ms 2140 KB Output is correct
6 Correct 10 ms 4296 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 20 ms 7856 KB Output is correct
9 Correct 26 ms 11204 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 37 ms 13600 KB Output is correct
12 Correct 44 ms 15708 KB Output is correct
13 Correct 1 ms 600 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 ms 344 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Incorrect 0 ms 348 KB Output isn't correct
23 Halted 0 ms 0 KB -