結果
問題 | No.2569 はじめてのおつかいHard |
ユーザー | kwm_t |
提出日時 | 2023-12-29 23:57:56 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 392 ms / 2,000 ms |
コード長 | 3,103 bytes |
コンパイル時間 | 2,112 ms |
コンパイル使用メモリ | 216,496 KB |
実行使用メモリ | 44,624 KB |
最終ジャッジ日時 | 2024-09-27 16:25:18 |
合計ジャッジ時間 | 4,932 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 48 ms
14,848 KB |
testcase_01 | AC | 48 ms
14,848 KB |
testcase_02 | AC | 49 ms
14,848 KB |
testcase_03 | AC | 49 ms
14,848 KB |
testcase_04 | AC | 49 ms
14,720 KB |
testcase_05 | AC | 392 ms
43,736 KB |
testcase_06 | AC | 177 ms
23,292 KB |
testcase_07 | AC | 351 ms
44,360 KB |
testcase_08 | AC | 373 ms
44,624 KB |
testcase_09 | AC | 100 ms
19,364 KB |
testcase_10 | AC | 2 ms
6,940 KB |
testcase_11 | AC | 2 ms
6,944 KB |
ソースコード
#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; }