이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
constexpr size_t N = 100000;
vector<tuple<size_t, size_t, size_t>> g[N];
map<size_t, size_t> ccost[N], f[N];
size_t d[N];
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
size_t n, m;
cin >> n >> m;
for (size_t i = 0; i < m; i++)
{
size_t u, v, c, p;
cin >> u >> v >> c >> p;
g[u - 1].emplace_back(v - 1, c, p);
g[v - 1].emplace_back(u - 1, c, p);
ccost[u - 1][c] += p;
ccost[v - 1][c] += p;
}
auto compare_second = [](tuple<size_t, size_t, size_t> const &a,
tuple<size_t, size_t, size_t> const &b)
{ return get<1>(a) > get<1>(b); };
for (size_t i = 0; i < n; i++)
sort(g[i].begin(), g[i].end(), compare_second);
priority_queue<tuple<size_t, size_t, size_t>,
vector<tuple<size_t, size_t, size_t>>, decltype(compare_second)>
q(compare_second);
fill(d + 1, d + n, SIZE_MAX);
q.emplace(0, 0, 0);
while (!q.empty())
{
auto const [u, dis, c] = q.top();
q.pop();
if ((!c && dis != d[u]) || (c && dis != f[u][c]))
continue;
if (c)
{
auto it = lower_bound(
g[u].begin(), g[u].end(), (tuple<size_t, size_t, size_t>){0, c, 0},
compare_second);
while (it != g[u].end() && get<1>(*it) == c)
{
if (dis + ccost[u][c] - get<2>(*it) < d[get<0>(*it)])
{
d[get<0>(*it)] = dis + ccost[u][c] - get<2>(*it);
q.emplace(get<0>(*it), d[get<0>(*it)], 0);
}
it++;
}
continue;
}
for (auto const &[v, ec, p] : g[u])
{
if (f[v].find(ec) == f[v].end() || dis < f[v][ec])
{
f[v][ec] = dis;
q.emplace(v, dis, ec);
}
if (dis + p < d[v])
{
d[v] = dis + p;
q.emplace(v, d[v], 0);
}
if (dis + ccost[u][ec] - p < d[v])
{
d[v] = dis + ccost[u][ec] - p;
q.emplace(v, d[v], 0);
}
}
}
cout << (d[n - 1] == SIZE_MAX ? -1 : (int64_t)d[n - 1]) << '\n';
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |