結果

問題 No.3111 Toll Optimization
ユーザー toku4388
提出日時 2025-04-20 01:41:31
言語 C++23
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 433 ms / 5,000 ms
コード長 1,605 bytes
コンパイル時間 3,522 ms
コンパイル使用メモリ 288,708 KB
実行使用メモリ 49,580 KB
最終ジャッジ日時 2025-04-20 01:41:50
合計ジャッジ時間 16,744 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 70
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll LINF = 4321001001001001001;
struct graph {
	struct edge {
		int to;
		ll cost;
	};
	vector<vector<edge>> g;
	vector<ll> dist;
	int n;
	graph(int n_) : g(n_), dist(n_, LINF), n(n_) {}
	void addEdge(int from, int to, ll cost) {
		g[from].push_back(edge{to, cost});
	}
	void addEdgeBoth(int n1, int n2, ll cost) {
		addEdge(n1, n2, cost);
		addEdge(n2, n1, cost);
	}
	void dijkstra(int st) {
		for (int i = 0; i < n; i++) dist[i] = LINF;
		// priority_queue qv
		// first: distance from st
		// second: this node
		using qv = pair<ll, int>;
		priority_queue<qv, vector<qv>, greater<qv>> q;
		dist[st] = 0;
		q.push(qv{dist[st], st});
		while (!q.empty()) {
			qv now = q.top();
			q.pop();
			if (now.first > dist[now.second]) continue;
			for (edge ne : g[now.second]) {
				ll nd = dist[now.second] + ne.cost;
				if (dist[ne.to] > nd) {
					dist[ne.to] = nd;
					q.push(qv{dist[ne.to], ne.to});
				}
			}
		}
		return;
	}
};
int main() {
	int n, m, k;
	cin >> n >> m >> k;
	vector<int> c(m);
	for (int i = 0; i < m; i++) {
		cin >> c[i];
	}
	graph g(n * (k + 1));
	for (int i = 0; i < m; i++) {
		int u, v;
		cin >> u >> v;
		u--;
		v--;
		for (int t = 0; t < (k + 1); t++) {
			g.addEdgeBoth(u + n * t, v + n * t, c[i]);
		}
		for (int t = 0; t < k; t++) {
			g.addEdge(u + n * t, v + n * (t + 1), 0);
			g.addEdge(v + n * t, u + n * (t + 1), 0);
		}
	}
	g.dijkstra(0);
	ll ans = LINF;
	for (int t = 0; t <= k; t++) {
		ans = min(ans, g.dist[n - 1 + t * n]);
	}
	if (ans == LINF) ans = -1;
	cout << ans << endl;
	return 0;
}
0