제출 #535772

#제출 시각아이디문제언어결과실행 시간메모리
535772abc864197532Olympic Bus (JOI20_ho_t4)C++17
5 / 100
1083 ms1524 KiB
#include <bits/stdc++.h>
using namespace std;
#define lli long long int
#define mp make_pair
#define eb emplace_back
#define pb push_back
#define pii pair<int,int>
#define X first
#define Y second
#define all(x) x.begin(), x.end()
void abc() {cout << endl;}
template <typename T, typename ...U> void abc(T i, U ...j) {
	cout << i << ' ', abc(j...);
}
template <typename T> void printv(T l, T r) {
	for (; l != r; ++l)
		cout << *l << " \n"[l + 1 == r];
}
#ifdef Doludu
#define test(x...) abc("[" + string(#x) + "]", x)
#define owo freopen("input.txt", "r", stdin), freopen("output.txt", "w", stdout)
#else
#define test(x...) void(0)
#define owo ios::sync_with_stdio(false), cin.tie(0)
#endif
const int N = 205, M = 50001;

array <int, 4> E[M];

vector <int> adj[N];
int n;

lli build(int s, int t) {
	vector <lli> dis(n, 1ll << 60);
	vector <int> rt(n, -1);
	dis[s] = 0;
	priority_queue <pair <lli, int>, vector <pair <lli, int>>, greater <pair <lli, int>>> pq;	
	pq.emplace(0, s);
	while (!pq.empty()) {
		lli d; int v; tie(d, v) = pq.top(); pq.pop();
		if (d > dis[v])
			continue;
		for (int id : adj[v]) {
			int u = E[id][0] ^ E[id][1] ^ v;
			if (dis[u] > d + E[id][2])
				dis[u] = d + E[id][2], rt[u] = v, pq.emplace(dis[u], u);
		}
	}
	return dis[t];
}

int main () {
	owo;
	int m;
	cin >> n >> m;
	int s = 0, t = n - 1;
	for (int i = 0, u, v, c, d; i < m; ++i) {
		cin >> u >> v >> c >> d, --u, --v;
		E[i] = {u, v, c, d};
		adj[u].pb(i);
	}
	lli ans = build(s, t) + build(t, s);
	for (int i = 0; i < m; ++i) {
		int u = E[i][0], v = E[i][1];
		adj[u].erase(find(all(adj[u]), i)), adj[v].pb(i);
		ans = min(ans, build(s, t) + build(t, s) + E[i][3]);
		adj[u].pb(i), adj[v].erase(find(all(adj[v]), i));
	}
	if (ans >= 1ll << 40)
		ans = -1;
	cout << ans << '\n';
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...