이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#ifdef LOCAL
#include "debug.h"
#else
#define debug(...) 42
#endif
#pragma GCC optimize("Ofast,unroll-loops")
using namespace std;
#define endl '\n'
#define ll long long
#define all(x) x.begin(), x.end()
map<int, vector<tuple<int, int, int>>> edg[100005];
unordered_map<int, ll> sum[100005], dp2[100005];
ll dp[100005];
int n, m;
void solve() {
priority_queue<tuple<ll, int, int>> pq;
pq.push({0, 1, 0});
while (pq.size() > 0) {
ll dist;
int place, color;
tie(dist, place, color) = pq.top();
dist *= -1;
pq.pop();
if (color) {
if (dist != dp2[place][color]) continue;
for (auto [to, color, cost] : edg[place][color]) {
ll newCost = dist + sum[place][color] - cost;
if (newCost < dp[to]) {
dp[to] = newCost;
pq.push({-dp[to], to, 0});
}
}
}
else {
if (dist != dp[place]) continue;
for (auto x : edg[place]) {
for (auto [to, color, cost] : x.second) {
ll newCost = dist + min((ll) cost, sum[place][color] - (ll) cost);
if (newCost < dp[to]) {
dp[to] = newCost;
pq.push({-dp[to], to, 0});
}
if (!dp2[to].count(color) || dist < dp2[to][color]) {
dp2[to][color] = dist;
pq.push({-dp2[to][color], to, color});
}
}
}
}
}
cout << ((dp[n] == LLONG_MAX) ? -1 : dp[n]);
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n >> m;
for (int i = 2; i <= n; i++) dp[i] = LLONG_MAX;
for (int i = 0; i < m; i++) {
int from, to, color, cost;
cin >> from >> to >> color >> cost;
edg[from][color].push_back({to, color, cost});
edg[to][color].push_back({from, color, cost});
sum[from][color] += cost;
sum[to][color] += cost;
}
solve();
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |