제출 #752266

#제출 시각아이디문제언어결과실행 시간메모리
752266anaduguleanuFactories (JOI14_factories)C++14
15 / 100
8037 ms144656 KiB
#include <iostream> #include <vector> #include <algorithm> #define MAX 500000 #define LOG 20 #define INF 1000000000000000000 using namespace std; ///ifstream cin ("c.in"); ///ofstream cout ("c.out"); vector <pair<int, long long>> graph[MAX + 10], length[MAX + 10]; vector <int> nodes, v; int timer = 1, level[MAX + 10], in[MAX + 10], ancestor[LOG + 10][MAX + 10], company[MAX + 10], visited[MAX + 10]; long long dist[MAX + 10], dp1[MAX + 10], dp2[MAX + 10], ans; void dfs(int node, int parent) { in[node] = timer; timer++; level[node] = level[parent] + 1; ancestor[0][node] = parent; for (auto next : graph[node]) if (next.first != parent) { dist[next.first] = dist[node] + next.second; dfs(next.first, node); } } void Init(int n, int a[], int b[], int d[]) { for (int i = 0; i < n - 1; i++) { graph[a[i] + 1].push_back({b[i] + 1, d[i]}); graph[b[i] + 1].push_back({a[i] + 1, d[i]}); } dfs(1, 0); for (int p = 1; p <= LOG; p++) for (int node = 1; node <= n; node++) ancestor[p][node] = ancestor[p - 1][ancestor[p - 1][node]]; for (int node = 1; node <= n; node++) { dp1[node] = INF; dp2[node] = INF; } } bool cmp(int x, int y) { return (in[x] < in[y]); } int lca(int x, int y) { if (level[x] < level[y]) swap(x, y); for (int p = LOG; p >= 0; p--) if (level[ancestor[p][x]] >= level[y]) x = ancestor[p][x]; if (x == y) return x; for (int p = LOG; p >= 0; p--) if (ancestor[p][x] != ancestor[p][y]) { x = ancestor[p][x]; y = ancestor[p][y]; } return ancestor[0][x]; } void solve(int node) { if (company[node] == 1) dp1[node] = 0; if (company[node] == 2) dp2[node] = 0; for (auto next : length[node]) { solve(next.first); dp1[node] = min(dp1[node], dp1[next.first] + next.second); dp2[node] = min(dp2[node], dp2[next.first] + next.second); } ans = min(ans, dp1[node] + dp2[node]); } long long Query(int s, int x[], int t, int y[]) { for (int i = 0; i < s; i++) { visited[x[i] + 1] = 1; company[x[i] + 1] = 1; nodes.push_back(x[i] + 1); } for (int i = 0; i < t; i++) { visited[y[i] + 1] = 1; company[y[i] + 1] = 2; nodes.push_back(y[i] + 1); } sort(nodes.begin(), nodes.end(), cmp); for (int i = 1; i < nodes.size(); i++) if (visited[lca(nodes[i - 1], nodes[i])] == 0) { visited[lca(nodes[i - 1], nodes[i])] = 1; nodes.push_back(lca(nodes[i - 1], nodes[i])); } sort(nodes.begin(), nodes.end(), cmp); for (auto node : nodes) { if (!v.empty()) { while (!v.empty() && lca(node, v.back()) != v.back()) v.pop_back(); length[v.back()].push_back({node, dist[node] + dist[v.back()] - 2 * dist[lca(node, v.back())]}); } v.push_back(node); } ans = INF; solve(v[0]); for (int i = 0; i < s; i++) company[x[i] + 1] = 0; for (int i = 0; i < t; i++) company[y[i] + 1] = 0; for (auto node : nodes) { dp1[node] = INF; dp2[node] = INF; visited[node] = 0; length[node].clear(); } nodes.clear(); v.clear(); return ans; }

컴파일 시 표준 에러 (stderr) 메시지

factories.cpp: In function 'long long int Query(int, int*, int, int*)':
factories.cpp:94:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   94 |     for (int i = 1; i < nodes.size(); i++)
      |                     ~~^~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...