# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
741621 | 2023-05-14T13:21:09 Z | vjudge1 | 꿈 (IOI13_dreaming) | C++17 | 0 ms | 0 KB |
#include "dreaming.h" #include<bits/stdc++.h> using namespace std; int travelTime(int N, int M, int L, vector<int> A, vector<int> B, vector<int> T) { priority_queue<pair<int, int> > pq; vector<vector<pair<int, int> > > ng(N); for (int i = 0; i < M; i++) { ng[A[i]].push_back(make_pair(B[i], T[i])); ng[B[i]].push_back(make_pair(A[i], T[i])); } vector<int> zeros(N); vector<int> nc(N); for (int i = 0; i < N; i++) { nc[i] = ng[i].size(); if (nc[i] == 1) { pq.push(make_pair(0, i)); } else if (nc[i] == 0) { zeros[i] = true; } } vector<int> vis(N); vector<int> dis(N); while (!pq.empty()) { int s = pq.top().second; pq.pop(); if (nc[s] == 0)continue; vis[s] = true; for (int i = 0; i < ng[s].size(); i++) { int neg = ng[s][i].first; int ds = ng[s][i].second; if (vis[neg])continue; dis[neg] = max(dis[neg], dis[s] + ds); if (nc[neg] == 2) { pq.push(make_pair(-dis[neg], neg)); } else if (nc[neg] == 1) { zeros[neg] = 1; } nc[neg]--; } } int rez = 0; vector<int> vals; for (int i = 0; i < N; i++) { if (!zeros[i])continue; vals.push_back(dis[i]); } sort(vals.begin(), vals.end()); reverse(vals.begin(), vals.end()); rez = vals[0] + vals[1] + L; if (vals.size() > 2) rez = max(rez, vals[1] + vals[2] + L + L); return rez; }