이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
/* ************************************************************************** */
/* */
/* ::: ::: ::: */
/* Problem Number: 20987 :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: js9028 <boj.kr/u/js9028> +#+ +#+ +#+ */
/* +#+ +#+ +#+ */
/* https://boj.kr/20987 #+# #+# #+# */
/* Solved: 2024/09/23 23:22:22 by js9028 ### ### ##.kr */
/* */
/* ************************************************************************** */
#include <iostream>
#include <memory.h>
#include <vector>
#include <algorithm>
#include <string>
#include <array>
#include <math.h>
#include <tuple>
#include <map>
#include <queue>
#define fastio (ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL))
using namespace std;
typedef long long ll;
const long long INF = 1e15 + 5;
int n, m;
vector<array<int, 3>> adj[100011];
ll dist[3][100011];
ll solve()
{
priority_queue<array<ll, 3>, vector<array<ll, 3>>, greater<>> pq;
dist[0][1] = 0;
fill(dist[0], dist[0] + n + 3, INF);
fill(dist[1], dist[1] + n + 3, INF);
pq.push({0, 1, 0});
while (!pq.empty())
{
auto [dis, cur, y] = pq.top();
pq.pop();
if (cur == n)
return dis;
if (dist[y][cur] < dis)
continue;
map<int, ll> mn, sum;
for (auto [nxt, clr, cos] : adj[cur])
{
if (mn.find(clr) == mn.end())
mn[clr] = min(dist[1][nxt], dist[0][nxt]);
else
mn[clr] = min(mn[clr], min(dist[1][nxt], dist[0][nxt]));
sum[clr] += cos;
}
for (auto [nxt, clr, cos] : adj[cur])
{
ll nd = dis + cos;
if (dist[1][nxt] > nd)
{
dist[1][nxt] = nd;
pq.push({nd, nxt, 1});
}
if (y && mn[clr] == dis)
{
nd = mn[clr] + sum[clr] - cos;
}
else
{
nd = dis + sum[clr] - cos;
}
if (dist[0][nxt] > nd)
{
dist[0][nxt] = nd;
pq.push({nd, nxt, 0});
}
}
}
return -1;
}
int main()
{
fastio;
cin >> n >> m;
for (int i = 0; i < m; i++)
{
int a, b, c, d;
cin >> a >> b >> c >> d;
adj[a].push_back({b, c, d});
adj[b].push_back({a, c, d});
}
cout << solve();
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |