Submission #1276004

#TimeUsernameProblemLanguageResultExecution timeMemory
1276004kaiboyOlympic Bus (JOI20_ho_t4)C++20
100 / 100
140 ms2288 KiB
#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;

const int   N = 200;
const int   M = 50000;
const int INF = 0x3f3f3f3f;

int ii[M], jj[M], ww[M], cc[M], dd_[M], dd__[M];
vector<int> eh[N], fh[N];
int dds[N], ddt[N], hh[N], *dd;
int pq[N], iq[N + 1], pq_cnt;

bool lt(int i, int j) {
	return dd[i] < dd[j];
}

int p2(int p) {
	return (p <<= 1) > pq_cnt ? 0 : p ^ (p < pq_cnt && lt(iq[p ^ 1], iq[p]));
}

void pq_up(int i) {
	int j, p, q;
	for (p = pq[i]; (q = p >> 1) && lt(i, j = iq[q]); p = q)
		iq[pq[j] = p] = j;
	iq[pq[i] = p] = i;
}

void pq_dn(int i) {
	int j, p, q;
	for (p = pq[i]; (q = p2(p)) && lt(j = iq[q], i); p = q)
		iq[pq[j] = p] = j;
	iq[pq[i] = p] = i;
}

void pq_add_last(int i) {
	iq[pq[i] = ++pq_cnt] = i;
}

int pq_remove_first() {
	int i = iq[1], j = iq[pq_cnt--];
	if (i != j)
		pq[j] = 1, pq_dn(j);
	pq[i] = 0;
	return i;
}

void dijkstra(int n, int m, int s, int *hh) {
	for (int i = 0; i < n; i++)
		eh[i].clear();
	for (int h = 0; h < m; h++)
		eh[ii[h]].push_back(h), eh[jj[h]].push_back(h);
	for (int i = 0; i < n; i++)
		dd[i] = INF;
	dd[s] = 0, pq_add_last(s);
	while (pq_cnt) {
		int i = pq_remove_first(), d = dd[i];
		for (int h : eh[i]) {
			int j = jj[h], e = d + ww[h];
			if (dd[j] > e) {
				if (dd[j] == INF)
					pq_add_last(j);
				dd[j] = e, hh[j] = h, pq_up(j);
			}
		}
	}
}

int main() {
	ios_base::sync_with_stdio(false), cin.tie(NULL);
	int n, m; cin >> n >> m;
	for (int h = 0; h < m; h++)
		cin >> ii[h] >> jj[h] >> ww[h] >> cc[h], ii[h]--, jj[h]--;
	int ans = 0;
	for (int s = 0, t = n - 1, z = 0; z < 2; z++, swap(s, t)) {
		dd = dds, dijkstra(n, m, s, hh);
		for (int h = 0; h < m; h++)
			swap(ii[h], jj[h]);
		dd = ddt, dijkstra(n, m, t, hh);
		for (int h = 0; h < m; h++)
			swap(ii[h], jj[h]);
		ans += ddt[s];
		for (int h = 0; h < m; h++)
			dd_[h] = min(ddt[s], dds[jj[h]] + ww[h] + ddt[ii[h]]);
		if (ddt[s] < INF)
			for (int i = s; i != t; ) {
				int h = hh[i]; i ^= ii[h] ^ jj[h];
				swap(ii[h], jj[h]);
				dd = dds, dijkstra(n, m, s, ddt);
				dd_[h] = dds[t];
				swap(ii[h], jj[h]);
			}
		for (int h = 0; h < m; h++)
			dd__[h] += dd_[h];
	}
	for (int h = 0; h < m; h++)
		if (dd__[h] < INF)
			ans = min(ans, dd__[h] + cc[h]);
	cout << (ans < INF ? ans : -1) << '\n';
	return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...