Submission #77829

# Submission time Handle Problem Language Result Execution time Memory
77829 2018-09-30T16:12:02 Z tmwilliamlin168 Wild Boar (JOI18_wild_boar) C++14
12 / 100
4 ms 880 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);
	while(pq.size())
		pq.pop();
//	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;
		}
	}
	while(pq.size()) {
		a<ll, 2> u=pq.top();
		pq.pop();
		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; i<2; ++i) {
				int v=nx[(e^1)*2+i];
				if(v==-1||d[v]<=u[0]+ew[e/2])
					continue;
				d[v]=u[0]+ew[e/2];
				pq.push({d[v], v});
				pv[v]=u[1];
			}
		} else {
			int v=u[1];
			for(; pv[v]!=-1; v=pv[v]);
//			assert(ia==-1||v/4!=ia/2);
			return {u[0]+ew[e/2], v/4, e/2};
		}
		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]];
		}
	}
	return {INF, -1, -1};
}

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";
	}
}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 380 KB Output is correct
3 Correct 2 ms 440 KB Output is correct
4 Correct 2 ms 516 KB Output is correct
5 Correct 2 ms 644 KB Output is correct
6 Correct 2 ms 644 KB Output is correct
7 Correct 2 ms 644 KB Output is correct
8 Correct 2 ms 644 KB Output is correct
9 Correct 2 ms 644 KB Output is correct
10 Correct 2 ms 644 KB Output is correct
11 Correct 2 ms 644 KB Output is correct
12 Correct 2 ms 648 KB Output is correct
13 Correct 2 ms 776 KB Output is correct
14 Correct 3 ms 776 KB Output is correct
15 Correct 2 ms 776 KB Output is correct
16 Correct 2 ms 776 KB Output is correct
17 Correct 2 ms 776 KB Output is correct
18 Correct 2 ms 776 KB Output is correct
19 Correct 2 ms 776 KB Output is correct
20 Correct 2 ms 880 KB Output is correct
21 Correct 2 ms 880 KB Output is correct
22 Correct 2 ms 880 KB Output is correct
23 Correct 2 ms 880 KB Output is correct
24 Correct 2 ms 880 KB Output is correct
25 Correct 2 ms 880 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 380 KB Output is correct
3 Correct 2 ms 440 KB Output is correct
4 Correct 2 ms 516 KB Output is correct
5 Correct 2 ms 644 KB Output is correct
6 Correct 2 ms 644 KB Output is correct
7 Correct 2 ms 644 KB Output is correct
8 Correct 2 ms 644 KB Output is correct
9 Correct 2 ms 644 KB Output is correct
10 Correct 2 ms 644 KB Output is correct
11 Correct 2 ms 644 KB Output is correct
12 Correct 2 ms 648 KB Output is correct
13 Correct 2 ms 776 KB Output is correct
14 Correct 3 ms 776 KB Output is correct
15 Correct 2 ms 776 KB Output is correct
16 Correct 2 ms 776 KB Output is correct
17 Correct 2 ms 776 KB Output is correct
18 Correct 2 ms 776 KB Output is correct
19 Correct 2 ms 776 KB Output is correct
20 Correct 2 ms 880 KB Output is correct
21 Correct 2 ms 880 KB Output is correct
22 Correct 2 ms 880 KB Output is correct
23 Correct 2 ms 880 KB Output is correct
24 Correct 2 ms 880 KB Output is correct
25 Correct 2 ms 880 KB Output is correct
26 Incorrect 4 ms 880 KB Output isn't correct
27 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 380 KB Output is correct
3 Correct 2 ms 440 KB Output is correct
4 Correct 2 ms 516 KB Output is correct
5 Correct 2 ms 644 KB Output is correct
6 Correct 2 ms 644 KB Output is correct
7 Correct 2 ms 644 KB Output is correct
8 Correct 2 ms 644 KB Output is correct
9 Correct 2 ms 644 KB Output is correct
10 Correct 2 ms 644 KB Output is correct
11 Correct 2 ms 644 KB Output is correct
12 Correct 2 ms 648 KB Output is correct
13 Correct 2 ms 776 KB Output is correct
14 Correct 3 ms 776 KB Output is correct
15 Correct 2 ms 776 KB Output is correct
16 Correct 2 ms 776 KB Output is correct
17 Correct 2 ms 776 KB Output is correct
18 Correct 2 ms 776 KB Output is correct
19 Correct 2 ms 776 KB Output is correct
20 Correct 2 ms 880 KB Output is correct
21 Correct 2 ms 880 KB Output is correct
22 Correct 2 ms 880 KB Output is correct
23 Correct 2 ms 880 KB Output is correct
24 Correct 2 ms 880 KB Output is correct
25 Correct 2 ms 880 KB Output is correct
26 Incorrect 4 ms 880 KB Output isn't correct
27 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 380 KB Output is correct
3 Correct 2 ms 440 KB Output is correct
4 Correct 2 ms 516 KB Output is correct
5 Correct 2 ms 644 KB Output is correct
6 Correct 2 ms 644 KB Output is correct
7 Correct 2 ms 644 KB Output is correct
8 Correct 2 ms 644 KB Output is correct
9 Correct 2 ms 644 KB Output is correct
10 Correct 2 ms 644 KB Output is correct
11 Correct 2 ms 644 KB Output is correct
12 Correct 2 ms 648 KB Output is correct
13 Correct 2 ms 776 KB Output is correct
14 Correct 3 ms 776 KB Output is correct
15 Correct 2 ms 776 KB Output is correct
16 Correct 2 ms 776 KB Output is correct
17 Correct 2 ms 776 KB Output is correct
18 Correct 2 ms 776 KB Output is correct
19 Correct 2 ms 776 KB Output is correct
20 Correct 2 ms 880 KB Output is correct
21 Correct 2 ms 880 KB Output is correct
22 Correct 2 ms 880 KB Output is correct
23 Correct 2 ms 880 KB Output is correct
24 Correct 2 ms 880 KB Output is correct
25 Correct 2 ms 880 KB Output is correct
26 Incorrect 4 ms 880 KB Output isn't correct
27 Halted 0 ms 0 KB -