結果
| 問題 |
No.1601 With Animals into Institute
|
| コンテスト | |
| ユーザー |
25__toma
|
| 提出日時 | 2021-07-10 14:27:55 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 738 ms / 3,000 ms |
| コード長 | 2,664 bytes |
| コンパイル時間 | 2,641 ms |
| コンパイル使用メモリ | 209,324 KB |
| 最終ジャッジ日時 | 2025-01-23 00:08:58 |
|
ジャッジサーバーID (参考情報) |
judge5 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 36 |
ソースコード
#include"bits/stdc++.h"
using namespace std;
#define REP(k,m,n) for(int (k)=(m);(k)<(n);(k)++)
#define rep(i,n) REP((i),0,(n))
using ll = long long;
using pll = pair<ll, ll>;
using tp3 = tuple<ll, ll, ll>;
constexpr int INF = 1 << 28;
constexpr ll INFL = 1ll << 60;
constexpr int dh[4] = { 0,1,0,-1 };
constexpr int dw[4] = { -1,0,1,0 };
bool isin(const int H, const int W, const int h, const int w) {
return 0 <= h && h < H && 0 <= w && w < W;
}
// ============ template finished ============
using Pos = ll;
using Dist = ll;
using HaveAnimal = bool;
using Edge = tuple<Pos, Dist, HaveAnimal>;
using Graph = vector<vector<Edge>>;
vector<pll> calc_goals_by_dijkstra(const Graph& graph) {
const int N = graph.size();
vector<ll> dists(N, INFL);
vector<ll> haveAnimalDists(N, INFL);
priority_queue<pll, vector<pll>, greater<pll>> pq;
dists[N - 1] = 0;
pq.push(make_pair(0, N - 1));
while (!pq.empty()) {
auto[dist, pos] = pq.top(); pq.pop();
for (auto[nextPos, edgeDist, haveAnimal] : graph[pos]) {
ll nextDist = dist + edgeDist;
if (nextDist < dists[nextPos]) {
dists[nextPos] = nextDist;
pq.push(make_pair(nextDist, nextPos));
}
if (haveAnimal && nextDist < haveAnimalDists[nextPos]) {
haveAnimalDists[nextPos] = nextDist;
}
}
}
vector<pll> goals;
rep(i, N)if (haveAnimalDists[i] != INFL) {
goals.push_back(make_pair(haveAnimalDists[i], i));
}
return goals;
}
vector<ll> dijkstra_with_many_goals(const Graph& graph, const vector<pll>& goals) {
const int N = graph.size();
vector<ll> dists(N, INFL);
priority_queue<pll, vector<pll>, greater<pll>> pq;
for (auto[dist, pos] : goals) {
dists[pos] = dist;
pq.push(make_pair(dist, pos));
}
while (!pq.empty()) {
auto[dist, pos] = pq.top(); pq.pop();
for (auto[nextPos, edgeDist, _] : graph[pos]) {
ll nextDist = dist + edgeDist;
if (nextDist < dists[nextPos]) {
dists[nextPos] = nextDist;
pq.push(make_pair(nextDist, nextPos));
}
}
}
return dists;
}
int main()
{
int N, M;
cin >> N >> M;
Graph graph(N);
rep(i, M) {
ll A, B, C, X;
cin >> A >> B >> C >> X;
A--; B--;
graph[A].push_back(make_tuple(B, C, X == 1));
graph[B].push_back(make_tuple(A, C, X == 1));
}
auto goals = calc_goals_by_dijkstra(graph);
auto dists = dijkstra_with_many_goals(graph, goals);
rep(i, N - 1)cout << dists[i] << endl;
return 0;
}
25__toma