Submission #711262

#TimeUsernameProblemLanguageResultExecution timeMemory
711262therealpainRobot (JOI21_ho_t4)C++17
100 / 100
178 ms20284 KiB
#include <bits/stdc++.h> 
#define fi first 
#define se second
#define mp make_pair
#define getbit(x, i) (((x) >> (i)) & 1)
#define all(x) x.begin(), x.end()
#define TASK ""
 
using namespace std;
 
template <typename T1, typename T2> bool mini(T1 &a, T2 b) {
	if (a > b) {a = b; return true;} return false;
}
template <typename T1, typename T2> bool maxi(T1 &a, T2 b) {
	if (a < b) {a = b; return true;} return false;
}
 
const long long inf = 1e18;
const int mod = 1e9 + 7;
const int N = 1e5 + 7;

struct Edge {
	int v, c, p;

	Edge(int v, int c, int p) {
		this -> v = v;
		this -> c = c;
		this -> p = p;
	}
};

vector <Edge> adj[N];
long long dist[N], mn[2 * N], sum[2 * N];
int n, m;

int main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cin >> n >> m;
	for (int i = 1; i <= m; ++i) {
		int u, v, c, p; cin >> u >> v >> c >> p;
		adj[u].emplace_back(v, c, p);
		adj[v].emplace_back(u, c, p);
	}

	memset(dist, 0x3f, sizeof dist);
	memset(mn, 0x3f, sizeof mn);
	priority_queue <pair <long long, int>> heap;
	dist[1] = 0;
	heap.emplace(0, 1);

	while (heap.size()) {
		auto [du, u] = heap.top();
		heap.pop();

		du = -du;
		if (du != dist[u]) continue;
		for (auto [v, c, p] : adj[u]) {
			sum[c] += p;
			mini(mn[c], dist[v]);
		}

		for (auto [v, c, p] : adj[u]) {
			if (mini(dist[v], du + min(1LL * p, sum[c] - p)))
				heap.emplace(-dist[v], v);
			if (mini(dist[v], mn[c] + sum[c] - p)) 
				heap.emplace(-dist[v], v);
		}

		for (auto [v, c, p] : adj[u]) {
			sum[c] = 0;
			maxi(mn[c], inf);
		}
	}

	cout << (dist[n] != dist[0] ? dist[n] : -1);
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...