Submission #524424

# Submission time Handle Problem Language Result Execution time Memory
524424 2022-02-09T08:00:44 Z Sohsoh84 Wild Boar (JOI18_wild_boar) C++17
0 / 100
115 ms 284932 KB
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef pair<ll, ll> pll;
typedef pair<ll, pll> plll;

#define all(x)			(x).begin(),(x).end()
#define X			first
#define Y			second
#define sep			' '
#define endl			'\n'
#define debug(x)		cerr << #x << ": " <<  x << endl;

const ll MAXN = 2e3 + 10; // 1e5 ?
const ll MAXL = 1e5 + 10;
const ll INF = 4e18;

vector<int> adj[MAXN];
int from[MAXN], to[MAXN], n, m, t, l, sup_list[MAXL];
ll dist[MAXN][3][MAXN][3], w[MAXN], ans[2][MAXN << 2];

inline int f(int ind, int v) {
	return from[ind] + to[ind] - v;
}

inline int edge_side(int ind, int v) {
	return from[ind] == v ? 0 : 1;
}

inline void dijkstra(int tv, int tstate) {
	dist[tv][tstate][tv][tstate] = 0;
	priority_queue<plll, vector<plll>, greater<plll>> pq;
	pq.push({0, {tv, tstate}});

	while (!pq.empty()) {
		int fv = pq.top().Y.X, fstate = pq.top().Y.Y;
		ll d_v = pq.top().X;
		pq.pop();
		if (d_v != dist[tv][tstate][fv][fstate]) continue; 
	
		int lst = fv, v = 0;
		if (fstate == 0) v = from[lst];
		else if (fstate == 1) v = to[lst];
		else v = lst, lst = -1;

		for (int ind : adj[v]) {
			if (ind == lst) continue;
			int u = f(ind, v), state = edge_side(ind, u);
			ll d_u = d_v + w[ind];

			if (d_u < dist[tv][tstate][ind][state]) {
				dist[tv][tstate][ind][state] = d_u;
				pq.push({d_u, {ind, state}});
			}
		}
	}
}

inline vector<pll> v_states(int v) {
	vector<pll> ans;
	for (int ind : adj[v])
		ans.push_back({ind, edge_side(ind, v)});
	return ans;
}

int main() {
	ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
	cin >> n >> m >> t >> l;
	for (int i = 1; i <= m; i++) {
		cin >> from[i] >> to[i] >> w[i];
		adj[from[i]].push_back(i);
		adj[to[i]].push_back(i);
	}

	for (int i = 0; i < MAXN; i++)
		for (int j = 0; j < 3; j++)
			for (int a = 0; a < MAXN; a++)
				for (int b = 0; b < 3; b++)
					dist[i][j][a][b] = INF;

	for (int v = 1; v <= n; v++)
		for (int state = 0; state < 3; state++)
			dijkstra(v, state);

	for (int i = 1; i <= l; i++) cin >> sup_list[i];
	while (t--) {
		int p, c;
		cin >> p >> c;
		sup_list[p] = c;
	}

	ans[1][0] = 0; // !!!
	ll fans = INF;
	for (int i = 2; i <= l; i++) {
		vector<pll> A = v_states(sup_list[i - 1]), B = v_states(sup_list[i]);
		int ind = i & 1;
		if (i == 2) A = {{sup_list[i - 1], 2}};
		fill(ans[ind], ans[ind] + int(B.size()), INF);

		fans = INF;
		for (int i = 0; i < int(B.size()); i++) {
			for (int j = 0; j < int(A.size()); j++)
				ans[ind][i] = min(ans[ind][i], ans[ind ^ 1][j] + dist[A[j].X][A[j].Y][B[i].X][B[i].Y]);
			fans = min(fans, ans[ind][i]);
		}
	}

	cout << (fans == INF ? -1 : fans) << endl;
	return 0;	
}
# Verdict Execution time Memory Grader output
1 Correct 114 ms 284932 KB Output is correct
2 Incorrect 115 ms 284892 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 114 ms 284932 KB Output is correct
2 Incorrect 115 ms 284892 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 114 ms 284932 KB Output is correct
2 Incorrect 115 ms 284892 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 114 ms 284932 KB Output is correct
2 Incorrect 115 ms 284892 KB Output isn't correct
3 Halted 0 ms 0 KB -