This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define ll long long
#define pii pair<int, int>
#define endl '\n'
#define int ll
using namespace std;
const int INF = 2e15;
struct nei
{
int v, c, w, e_idx;
};
int N, M;
vector<vector<nei>> graph;
vector<map<int, int>> color_cost;
vector<pii> conv;
class Compare
{
public:
bool operator()(tuple<int, int, int> &a, tuple<int, int, int> &b)
{
return get<0>(a) > get<0>(b);
}
};
signed main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> N >> M;
graph.resize(N);
color_cost.resize(N);
conv.resize(M + 1, {0, 0});
for (int i = 1; i <= M; i++)
{
int a, b, c, w;
cin >> a >> b >> c >> w;
--a;
--b;
--c;
graph[a].push_back({b, c, w, i});
graph[b].push_back({a, c, w, i});
conv[i] = {c, w};
}
for (int i = 0; i < N; i++)
{
for (nei &u : graph[i])
color_cost[i][u.c] += u.w;
}
vector<map<int, int>> dist(N);
// wei, node, prev color, prev color weight
priority_queue<tuple<int, int, int>, vector<tuple<int, int, int>>, Compare> q;
dist[0][0] = 0;
q.push({0, 0, 0});
while (!q.empty())
{
int w, v, e_id;
tie(w, v, e_id) = q.top();
q.pop();
if (dist[v][e_id] != w)
continue;
for (nei &u : graph[v])
{
int opt1 = w + u.w;
if (!dist[u.v].count(u.e_idx) || dist[u.v][u.e_idx] > opt1)
{
dist[u.v][u.e_idx] = opt1;
q.push({opt1, u.v, u.e_idx});
}
int pc, pw;
tie(pc, pw) = conv[e_id];
int opt2 = w + color_cost[v][u.c] - u.w - (pc == u.c ? pw : 0);
if (!dist[u.v].count(0) || dist[u.v][0] > opt2)
{
dist[u.v][0] = opt2;
q.push({opt2, u.v, 0});
}
}
}
int mn = INF;
for (auto mp : dist[N - 1])
{
mn = min(mn, mp.second);
}
cout << (mn == INF ? -1 : mn) << endl;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |