結果

問題 No.2569 はじめてのおつかいHard
ユーザー kwm_tkwm_t
提出日時 2023-12-29 23:57:56
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 443 ms / 2,000 ms
コード長 3,103 bytes
コンパイル時間 2,606 ms
コンパイル使用メモリ 217,496 KB
実行使用メモリ 44,696 KB
最終ジャッジ日時 2023-12-29 23:58:03
合計ジャッジ時間 6,516 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 57 ms
14,848 KB
testcase_01 AC 57 ms
14,848 KB
testcase_02 AC 57 ms
14,848 KB
testcase_03 AC 57 ms
14,848 KB
testcase_04 AC 56 ms
14,848 KB
testcase_05 AC 435 ms
43,756 KB
testcase_06 AC 215 ms
23,416 KB
testcase_07 AC 426 ms
44,496 KB
testcase_08 AC 443 ms
44,696 KB
testcase_09 AC 115 ms
19,612 KB
testcase_10 AC 2 ms
6,676 KB
testcase_11 AC 2 ms
6,676 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
//#include <atcoder/all>
using namespace std;
//using namespace atcoder;
//using mint = modint998244353;
//const int mod = 998244353;
//using mint = modint1000000007;
//const int mod = 1000000007;
//const int INF = 1e9;
const long long LINF = 1e18;
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rep2(i,l,r)for(int i=(l);i<(r);++i)
#define rrep(i, n) for (int i = (n-1); i >= 0; --i)
#define rrep2(i,l,r)for(int i=(r-1);i>=(l);--i)
#define all(x) (x).begin(),(x).end()
#define allR(x) (x).rbegin(),(x).rend()
#define P pair<int,int>
template<typename A, typename B> inline bool chmax(A & a, const B & b) { if (a < b) { a = b; return true; } return false; }
template<typename A, typename B> inline bool chmin(A & a, const B & b) { if (a > b) { a = b; return true; } return false; }
template<typename T>
struct Dijkstra {
public:
	int n;
	struct Edge {
		int to;
		T cost;
	};
	std::vector<std::vector<Edge>> g;
public:
	const T INF = numeric_limits<T>::max();
	std::vector<T> dp;
	Dijkstra() {}
	Dijkstra(int n) : n(n) {
		g.resize(n);
	}
	Dijkstra<T>& operator=(const Dijkstra<T>& obj) {
		cout << "?" << endl;
		this->v = obj.v;
		this->g = obj.g;
		this->dp = obj.dp;
		return *this;
	}
	void add_edge(int from, int to, T weight, bool twoWay = false) {
		g[from].push_back({ to,weight });
		if (twoWay) g[to].push_back({ from,weight });
	}
	int add_vertex() {
		g.push_back(std::vector<Edge>());
		return n++;
	}
	void build(int s) {
		dp.assign(n, INF);
		std::priority_queue<std::pair<T, int>, std::vector<std::pair<T, int>>, std::greater<std::pair<T, int>>> pq;
		dp[s] = 0;
		pq.push({ dp[s], s });
		while (!pq.empty()) {
			auto p = pq.top();
			pq.pop();
			T cost = p.first;
			int v = p.second;
			if (dp[v] < cost) continue;
			for (const auto &e : g[v]) {
				if (dp[e.to] > dp[v] + e.cost) {
					dp[e.to] = dp[v] + e.cost;
					pq.push({ dp[e.to], e.to });
				}
			}
		}
	}
	T get(int t) {
		if (INF == dp[t])return -1;
		else return dp[t];
	}
};
int main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	int n, m; cin >> n >> m;
	Dijkstra<long long> dijkstra(n);
	Dijkstra<long long> dijkstraR(n);
	rep(i, m) {
		int u, v, t; cin >> u >> v >> t;
		u--, v--;
		dijkstra.add_edge(u, v, t, false);
		dijkstraR.add_edge(v, u, t, false);
	}
	vector<long long>ans(n, LINF);
	auto dijkstra1 = dijkstra;
	auto dijkstraR1 = dijkstraR;
	dijkstra.build(n - 2);
	dijkstraR.build(n - 2);//n-1
	dijkstra1.build(n - 1);
	dijkstraR1.build(n - 1);//n
	rep(i, n) {
		long long d0 = dijkstraR1.dp[i];
		long long d1 = dijkstra1.dp[n - 2];
		long long d2 = dijkstra.dp[i];
		if (dijkstraR1.INF == d0)continue;
		if (dijkstra1.INF == d1)continue;
		if (dijkstra.INF == d2)continue;
		chmin(ans[i], d0 + d1 + d2);
	}
	rep(i, n) {
		long long d0 = dijkstraR.dp[i];
		long long d1 = dijkstra.dp[n - 1];
		long long d2 = dijkstra1.dp[i];
		if (dijkstraR1.INF == d0)continue;
		if (dijkstra1.INF == d1)continue;
		if (dijkstra.INF == d2)continue;
		chmin(ans[i], d0 + d1 + d2);
	}
	rep(i, n - 2) {
		if (LINF == ans[i])ans[i] = -1;
		cout << ans[i] << endl;
	}
	return 0;
}
0