답안 #335692

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
335692 2020-12-13T16:49:41 Z GioChkhaidze 공장들 (JOI14_factories) C++14
100 / 100
6316 ms 181096 KB
#include <bits/stdc++.h> 
#include "factories.h"

#define ll long long
#define pb push_back
#define F first
#define S second

using namespace std;

const int U=5e5+5;

bool f[U];
ll d[U],Dist[U];
int P[U][21];
int timer,in[U],out[U];

vector < int > rec,ver;
vector < pair < int , ll > > g[U]; 
vector < pair < int , int  > > v[U];
priority_queue < pair < ll , int > > pq;

bool is_anc(int a,int b) {
	return (in[a]<=in[b] && out[b]<=out[a]);
}

int LCA(int a,int b) {
	if (is_anc(a,b)) return a;
	for (int i=19; i>=0; i--) 
		if (!is_anc(P[a][i],b)) a=P[a][i];
	return P[a][0];
}

void Dfs(int x,int p,ll dist) {
	in[x]=++timer;
	d[x]=dist;
	P[x][0]=p;
	int to,cost;
	for (int i=0; i<v[x].size(); i++) {
		to=v[x][i].F;
		cost=v[x][i].S;
		if (to!=p) Dfs(to,x,dist+cost);
	}
	out[x]=timer;
}

void Init(int n, int a[], int b[], int c[]) {
	for (int i=0; i<n; i++) {
		v[a[i]].pb({b[i],c[i]});
		v[b[i]].pb({a[i],c[i]});
	}
	
	Dfs(0,0,0);
	for (int j=1; j<=19; j++)
		for (int i=1; i<=n; i++)
			P[i][j]=P[P[i][j-1]][j-1];
}

bool cmp(int a,int b) {
	return in[a]<in[b];
}

long long Query(int S, int X[], int T, int Y[]) {
	for (int i=0; i<S; i++) {
		ver.pb(X[i]);
	}
	
	for (int i=0; i<T; i++) {
		ver.pb(Y[i]);
	}
	
	sort(ver.begin(),ver.end(),cmp);
	
	ll dist;
	int vert;
	for (int i=0; i<S+T; i++) {
		if (!i) continue;
		vert=LCA(ver[i-1],ver[i]);
		ver.pb(vert);
	}

	sort(ver.begin(),ver.end(),cmp);
	
	vert=ver[0];
	for (int i=1; i<ver.size(); i++) {
		if (ver[i]==ver[i-1]) continue;
		if (ver[i]==vert) continue;

		while (!(is_anc(vert,ver[i]))) {
			vert=rec.back();
			rec.pop_back();	
		}
			
		dist=d[ver[i]]-d[vert];
		g[vert].pb({ver[i],dist});
		g[ver[i]].pb({vert,dist});
		rec.pb(vert);
		vert=ver[i];
	}
	
	while (rec.size()) 
		rec.pop_back();
	
	for (int i=0; i<ver.size(); i++) {
		if (i && ver[i]==ver[i-1]) continue;
		Dist[ver[i]]=1e15;
	}
	
	for (int i=0; i<S; i++) {
		Dist[X[i]]=0;
	}
	
	for (int i=0; i<ver.size(); i++) {
		if (i && ver[i]==ver[i-1]) continue;
		pq.push({-Dist[ver[i]],ver[i]});
	}
	
	int to,x;
	ll cost;
	while (!pq.empty()) {
		x=pq.top().S;
		pq.pop();
		if (f[x]) continue;
		f[x]=true;
		for (int i=0; i<g[x].size(); i++) {
			to=g[x][i].F;
			cost=g[x][i].S;
			if (!f[to] && Dist[x]+cost<Dist[to]) {
				Dist[to]=Dist[x]+cost;
				pq.push({-Dist[to],to});
			}
		}
	}
	
	ll ans=1e15;
	for (int i=0; i<T; i++) {
		ans=min(ans,Dist[Y[i]]);
	}
	
	for (int i=0; i<ver.size(); i++) {
		g[ver[i]].clear();
		f[ver[i]]=false;
		Dist[ver[i]]=0;
	}
	
	ver.clear();
	return ans;
}

Compilation message

factories.cpp: In function 'void Dfs(int, int, long long int)':
factories.cpp:39:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   39 |  for (int i=0; i<v[x].size(); i++) {
      |                ~^~~~~~~~~~~~
factories.cpp: In function 'long long int Query(int, int*, int, int*)':
factories.cpp:85:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   85 |  for (int i=1; i<ver.size(); i++) {
      |                ~^~~~~~~~~~~
factories.cpp:104:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  104 |  for (int i=0; i<ver.size(); i++) {
      |                ~^~~~~~~~~~~
factories.cpp:113:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  113 |  for (int i=0; i<ver.size(); i++) {
      |                ~^~~~~~~~~~~
factories.cpp:125:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  125 |   for (int i=0; i<g[x].size(); i++) {
      |                 ~^~~~~~~~~~~~
factories.cpp:140:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  140 |  for (int i=0; i<ver.size(); i++) {
      |                ~^~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 52 ms 24300 KB Output is correct
2 Correct 1850 ms 33140 KB Output is correct
3 Correct 1869 ms 33388 KB Output is correct
4 Correct 1721 ms 33260 KB Output is correct
5 Correct 1441 ms 33440 KB Output is correct
6 Correct 1303 ms 33004 KB Output is correct
7 Correct 1846 ms 33332 KB Output is correct
8 Correct 1790 ms 33388 KB Output is correct
9 Correct 1416 ms 33492 KB Output is correct
10 Correct 1311 ms 33068 KB Output is correct
11 Correct 1818 ms 32936 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 21 ms 24044 KB Output is correct
2 Correct 2214 ms 122784 KB Output is correct
3 Correct 3287 ms 125768 KB Output is correct
4 Correct 1613 ms 141780 KB Output is correct
5 Correct 1905 ms 171908 KB Output is correct
6 Correct 3469 ms 145928 KB Output is correct
7 Correct 3162 ms 67040 KB Output is correct
8 Correct 1698 ms 67156 KB Output is correct
9 Correct 1488 ms 71840 KB Output is correct
10 Correct 3041 ms 68432 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 52 ms 24300 KB Output is correct
2 Correct 1850 ms 33140 KB Output is correct
3 Correct 1869 ms 33388 KB Output is correct
4 Correct 1721 ms 33260 KB Output is correct
5 Correct 1441 ms 33440 KB Output is correct
6 Correct 1303 ms 33004 KB Output is correct
7 Correct 1846 ms 33332 KB Output is correct
8 Correct 1790 ms 33388 KB Output is correct
9 Correct 1416 ms 33492 KB Output is correct
10 Correct 1311 ms 33068 KB Output is correct
11 Correct 1818 ms 32936 KB Output is correct
12 Correct 21 ms 24044 KB Output is correct
13 Correct 2214 ms 122784 KB Output is correct
14 Correct 3287 ms 125768 KB Output is correct
15 Correct 1613 ms 141780 KB Output is correct
16 Correct 1905 ms 171908 KB Output is correct
17 Correct 3469 ms 145928 KB Output is correct
18 Correct 3162 ms 67040 KB Output is correct
19 Correct 1698 ms 67156 KB Output is correct
20 Correct 1488 ms 71840 KB Output is correct
21 Correct 3041 ms 68432 KB Output is correct
22 Correct 5642 ms 159200 KB Output is correct
23 Correct 4658 ms 161240 KB Output is correct
24 Correct 5882 ms 162716 KB Output is correct
25 Correct 5652 ms 165944 KB Output is correct
26 Correct 6316 ms 155744 KB Output is correct
27 Correct 4083 ms 181096 KB Output is correct
28 Correct 3287 ms 157424 KB Output is correct
29 Correct 6096 ms 154500 KB Output is correct
30 Correct 6032 ms 153820 KB Output is correct
31 Correct 5951 ms 154468 KB Output is correct
32 Correct 2444 ms 74832 KB Output is correct
33 Correct 2153 ms 71348 KB Output is correct
34 Correct 3237 ms 66176 KB Output is correct
35 Correct 3157 ms 65944 KB Output is correct
36 Correct 3420 ms 66668 KB Output is correct
37 Correct 3388 ms 66324 KB Output is correct