답안 #51280

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
51280 2018-06-17T11:02:38 Z Kubalionzzale 관광 (NOI14_sightseeing) C++17
15 / 25
3500 ms 112696 KB
#include <iostream>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <functional>

//std::vector< std::vector< std::pair<int, int> > > g(500010);
int ans[500010] = { 0 }, start[500010] = { 0 };
std::pair<int, std::pair<int, int> > edge[10000010];
int n, m, q;

void bfs()
{
    std::set<std::pair<int, int> , std::greater<std::pair<int, int> > > set;
    set.insert(std::make_pair(2e7, 0));
    bool visited[500010] = { 0 };

    int curNode, curVal, nextVal, nextNode;
    while (set.empty() == false)
    {
        std::pair<int, int> top = *set.begin();
        curNode = top.second;
        curVal = top.first;
        set.erase(set.begin());

  //      std::cout << curNode << " " << curVal << "\n";

        if (visited[curNode])
            continue;
        else
            visited[curNode] = 1;

        for (int i = start[curNode]; i < m; ++i)
        {
            if (edge[i].first != curNode)
                break;
            nextVal = edge[i].second.first;
            nextNode = edge[i].second.second;
            if (std::min(nextVal, curVal) > ans[nextNode] && !visited[nextNode])
            {
                ans[nextNode] = std::max(ans[nextNode], std::min(nextVal, curVal));
                set.insert(std::make_pair(std::min(nextVal, curVal), nextNode));
            }
        }
    }
}

int main()
{
    std::ios_base::sync_with_stdio(0);
    std::cin.tie(0);
    scanf("%d%d%d", &n, &m, &q);
//    std::cin >> n >> m >> q;

    int x, y, quality;
    for (int i = 0; i < m; ++i)
    {
        scanf("%d%d%d", &x, &y, &quality);
//        std::cin >> x >> y >> quality;
        --x,
        --y;


        edge[i] = std::make_pair(x, std::make_pair(quality, y));
        edge[m + i] = std::make_pair(y, std::make_pair(quality, x));
    }
    m *= 2;

    std::sort(edge, edge + m);

    int node[500010];
    for (int i = 0; i < q; ++i)
    {
        scanf("%d", &node[i]);
//        std::cin >> node[i];
    }

    bool here[500010] = { 0 };
    for (int i = 0; i < m; ++i)
    {
        if (!here[edge[i].first])
        {
            start[edge[i].first] = i;
            here[edge[i].first] = true;
        }
    }

    bfs();

    for (int i = 0; i < q; ++i)
    {
        printf("%d\n", ans[node[i] - 1]);
//        std::cout << ans[node[i] - 1] << "\n";
    }
}

Compilation message

sightseeing.cpp: In function 'int main()':
sightseeing.cpp:54:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d%d%d", &n, &m, &q);
     ~~~~~^~~~~~~~~~~~~~~~~~~~~~
sightseeing.cpp:60:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d%d%d", &x, &y, &quality);
         ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
sightseeing.cpp:76:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d", &node[i]);
         ~~~~~^~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 1400 KB Output is correct
2 Correct 3 ms 1400 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 1692 KB Output is correct
2 Correct 4 ms 1692 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 64 ms 3740 KB Output is correct
2 Correct 42 ms 3740 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 3531 ms 112696 KB Time limit exceeded
2 Halted 0 ms 0 KB -