답안 #77842

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
77842 2018-09-30T17:29:37 Z tmwilliamlin168 Wild Boar (JOI18_wild_boar) C++14
12 / 100
18000 ms 26676 KB
#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define a array

const int mxN=2e3, mxL=1e5;
const ll INF=1e18;
int n, m, t, l, x[mxL], eu, ev[2*mxN], ew[mxN], nx[4*mxN], le[mxN], pv[4*mxN];
ll d[4*mxN];
a<ll, 3> st[1<<18][4];
priority_queue<a<ll, 2>, vector<a<ll, 2>>, greater<a<ll, 2>>> pq;

a<ll, 3> dij(int s, int t, int ia, int ib) {
	memset(d, 0x3f, 8*4*m);
//	cout << "iab " << ia << " " << ib << endl;
	if(ia==-1) {
		s=2*le[s];
		d[s]=0;
		pq.push({0, s});
		pv[s]=-1;
	} else {
		ia=2*ia+(ev[2*ia]==s);
		for(int i=0, j; i<2; ++i) {
			if((j=nx[2*ia+i])==-1)
				continue;
			d[j]=0;
			pq.push({0, j});
			pv[j]=-1;
//			cout << "ie " << j << " " << 2*ia+i << endl;
		}
	}
	a<ll, 3> r={INF, -4, -4};
	while(pq.size()) {
		a<ll, 2> u=pq.top();
		pq.pop();
		if(u[0]>=r[0])
			break;
		if(u[0]>d[u[1]])
			continue;
		int e=u[1]/2;
//		if(s==12&&t==3)
//			cout << u[1] << " " << u[0] << " " << ev[e^1] << " " << ev[e] << endl;
		if(ev[e]!=t||e/2==ib) {
			for(int i=0, j; i<2; ++i) {
				if((j=nx[(e^1)*2+i])==-1||d[j]<=u[0]+ew[e/2])
					continue;
				d[j]=u[0]+ew[e/2];
				pq.push({d[j], j});
				pv[j]=u[1];
			}
		} else
			r=min(array<ll, 3>{u[0]+ew[e/2], u[1], u[1]}, r);
		if(nx[u[1]]!=-1&&d[nx[u[1]]]>u[0]) {
			d[nx[u[1]]]=u[0];
			pq.push({u[0], nx[u[1]]});
			pv[nx[u[1]]]=pv[u[1]];
		}
	}
	while(pq.size())
		pq.pop();
	for(; r[1]!=-4&&pv[r[1]]!=-1; r[1]=pv[r[1]]);
	return {r[0], r[1]/4, r[2]/4};
}

void upd(int l1, int i=1, int l2=0, int r2=l-2) {
	if(l2==r2) {
		st[i][0]=dij(x[l1], x[l1+1], -1, -1);
		st[i][1]=dij(x[l1], x[l1+1], st[i][0][1], st[i][0][2]);
		st[i][2]=dij(x[l1], x[l1+1], st[i][0][1], st[i][1][2]);
		st[i][3]=dij(x[l1], x[l1+1], st[i][1][1], st[i][0][2]);
//		for(int j=0; j<4; ++j) {
//			cout << "o " << i << " " << j << endl;
//			for(int k=0; k<3; ++k)
//				cout << st[i][j][k] << " ";
//			cout << endl;
//		}
		return;
	}
	int m2=(l2+r2)/2;
	if(l1<=m2)
		upd(l1, 2*i, l2, m2);
	else
		upd(l1, 2*i+1, m2+1, r2);
	st[i][0]={0, -1, -1};
	auto b=[&](const int &j) {
		st[i][j]={INF, -1, -1};
		for(int k=0; k<4; ++k)
			for(int l=0; l<4; ++l)
				if(st[2*i][k][1]!=st[i][j&1?0:3][1]&&st[2*i+1][l][2]!=st[i][j&2?0:3][2]&&st[2*i][k][2]!=st[2*i+1][l][1])
					st[i][j]=min(a<ll, 3>{st[2*i][k][0]+st[2*i+1][l][0], st[2*i][k][1], st[2*i+1][l][2]}, st[i][j]);
	};
	b(3);
	for(int j=0; j<3; ++j)
		b(j);
//		for(int j=0; j<4; ++j) {
//			cout << "I " << i << " " << j << endl;
//			for(int k=0; k<3; ++k)
//				cout << st[i][j][k] << " ";
//			cout << endl;
//		}
}

int main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	
	cin >> n >> m >> t >> l;
	memset(le, -1, 4*n);
	memset(nx, -1, 4*4*m);
	for(int i=0; i<2*m; ++i) {
		if(i&1) {
			ev[i]=eu;
			eu=ev[i^1];
		} else
			cin >> eu >> ev[i] >> ew[i/2], --eu, --ev[i];
		if(le[eu]!=-1) {
			nx[2*i]=2*le[eu];
			nx[2*le[eu]+1]=2*i+1;
		} else
			nx[2*i]=-1;
		le[eu]=i;
//		cout << "e " << i << " " << eu << " " << ev[i] << endl;
	}
	for(int i=0; i<l; ++i) {
		cin >> x[i], --x[i];
		if(i)
			upd(i-1);
	}
	while(t--) {
		int pk, qk;
		cin >> pk >> qk, --pk, --qk;
		x[pk]=qk;
		if(pk)
			upd(pk-1);
		if(pk<l-1)
			upd(pk);
		ll ans=min({st[1][0][0], st[1][1][0], st[1][2][0], st[1][3][0]});
		cout << (ans>=INF?-1:ans) << "\n";
	}
}
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 420 KB Output is correct
2 Correct 2 ms 504 KB Output is correct
3 Correct 2 ms 504 KB Output is correct
4 Correct 2 ms 504 KB Output is correct
5 Correct 2 ms 516 KB Output is correct
6 Correct 2 ms 648 KB Output is correct
7 Correct 2 ms 648 KB Output is correct
8 Correct 2 ms 648 KB Output is correct
9 Correct 2 ms 648 KB Output is correct
10 Correct 2 ms 648 KB Output is correct
11 Correct 2 ms 648 KB Output is correct
12 Correct 2 ms 648 KB Output is correct
13 Correct 2 ms 648 KB Output is correct
14 Correct 2 ms 648 KB Output is correct
15 Correct 2 ms 648 KB Output is correct
16 Correct 2 ms 648 KB Output is correct
17 Correct 2 ms 648 KB Output is correct
18 Correct 2 ms 668 KB Output is correct
19 Correct 2 ms 672 KB Output is correct
20 Correct 2 ms 676 KB Output is correct
21 Correct 2 ms 680 KB Output is correct
22 Correct 2 ms 684 KB Output is correct
23 Correct 2 ms 688 KB Output is correct
24 Correct 2 ms 692 KB Output is correct
25 Correct 2 ms 696 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 420 KB Output is correct
2 Correct 2 ms 504 KB Output is correct
3 Correct 2 ms 504 KB Output is correct
4 Correct 2 ms 504 KB Output is correct
5 Correct 2 ms 516 KB Output is correct
6 Correct 2 ms 648 KB Output is correct
7 Correct 2 ms 648 KB Output is correct
8 Correct 2 ms 648 KB Output is correct
9 Correct 2 ms 648 KB Output is correct
10 Correct 2 ms 648 KB Output is correct
11 Correct 2 ms 648 KB Output is correct
12 Correct 2 ms 648 KB Output is correct
13 Correct 2 ms 648 KB Output is correct
14 Correct 2 ms 648 KB Output is correct
15 Correct 2 ms 648 KB Output is correct
16 Correct 2 ms 648 KB Output is correct
17 Correct 2 ms 648 KB Output is correct
18 Correct 2 ms 668 KB Output is correct
19 Correct 2 ms 672 KB Output is correct
20 Correct 2 ms 676 KB Output is correct
21 Correct 2 ms 680 KB Output is correct
22 Correct 2 ms 684 KB Output is correct
23 Correct 2 ms 688 KB Output is correct
24 Correct 2 ms 692 KB Output is correct
25 Correct 2 ms 696 KB Output is correct
26 Correct 6 ms 696 KB Output is correct
27 Correct 1543 ms 26192 KB Output is correct
28 Correct 1408 ms 26676 KB Output is correct
29 Execution timed out 18059 ms 26676 KB Time limit exceeded
30 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 420 KB Output is correct
2 Correct 2 ms 504 KB Output is correct
3 Correct 2 ms 504 KB Output is correct
4 Correct 2 ms 504 KB Output is correct
5 Correct 2 ms 516 KB Output is correct
6 Correct 2 ms 648 KB Output is correct
7 Correct 2 ms 648 KB Output is correct
8 Correct 2 ms 648 KB Output is correct
9 Correct 2 ms 648 KB Output is correct
10 Correct 2 ms 648 KB Output is correct
11 Correct 2 ms 648 KB Output is correct
12 Correct 2 ms 648 KB Output is correct
13 Correct 2 ms 648 KB Output is correct
14 Correct 2 ms 648 KB Output is correct
15 Correct 2 ms 648 KB Output is correct
16 Correct 2 ms 648 KB Output is correct
17 Correct 2 ms 648 KB Output is correct
18 Correct 2 ms 668 KB Output is correct
19 Correct 2 ms 672 KB Output is correct
20 Correct 2 ms 676 KB Output is correct
21 Correct 2 ms 680 KB Output is correct
22 Correct 2 ms 684 KB Output is correct
23 Correct 2 ms 688 KB Output is correct
24 Correct 2 ms 692 KB Output is correct
25 Correct 2 ms 696 KB Output is correct
26 Correct 6 ms 696 KB Output is correct
27 Correct 1543 ms 26192 KB Output is correct
28 Correct 1408 ms 26676 KB Output is correct
29 Execution timed out 18059 ms 26676 KB Time limit exceeded
30 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 420 KB Output is correct
2 Correct 2 ms 504 KB Output is correct
3 Correct 2 ms 504 KB Output is correct
4 Correct 2 ms 504 KB Output is correct
5 Correct 2 ms 516 KB Output is correct
6 Correct 2 ms 648 KB Output is correct
7 Correct 2 ms 648 KB Output is correct
8 Correct 2 ms 648 KB Output is correct
9 Correct 2 ms 648 KB Output is correct
10 Correct 2 ms 648 KB Output is correct
11 Correct 2 ms 648 KB Output is correct
12 Correct 2 ms 648 KB Output is correct
13 Correct 2 ms 648 KB Output is correct
14 Correct 2 ms 648 KB Output is correct
15 Correct 2 ms 648 KB Output is correct
16 Correct 2 ms 648 KB Output is correct
17 Correct 2 ms 648 KB Output is correct
18 Correct 2 ms 668 KB Output is correct
19 Correct 2 ms 672 KB Output is correct
20 Correct 2 ms 676 KB Output is correct
21 Correct 2 ms 680 KB Output is correct
22 Correct 2 ms 684 KB Output is correct
23 Correct 2 ms 688 KB Output is correct
24 Correct 2 ms 692 KB Output is correct
25 Correct 2 ms 696 KB Output is correct
26 Correct 6 ms 696 KB Output is correct
27 Correct 1543 ms 26192 KB Output is correct
28 Correct 1408 ms 26676 KB Output is correct
29 Execution timed out 18059 ms 26676 KB Time limit exceeded
30 Halted 0 ms 0 KB -