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>
using namespace std;
#define pii pair<int, int>
#define fr first
#define se second
#define ll long long
ll INF = 0x3f3f3f3f3f3f3f3f;
struct State {
int v, x;
bool k;
State(const int& a = 0, const int& b = 0, const bool& c = 0) : v(a), x(b), k(c) {}
bool operator < (const State& a) const {
return (make_pair(v, make_pair(x, k)) < make_pair(a.v, make_pair(a.x, a.k)));
}
};
struct Edge {
int to, cor, p;
Edge(const int& a = 0, const int& b = 0, const int& c = 0) : to(a), cor(b), p(c) {}
};
const int MAXN = 1e5 + 10;
vector<Edge> adj[MAXN];
map<State, ll> d;
map<State, int> vis;
map<State, int> prop;
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
int n, m;
cin >> n >> m;
for (int i = 0; i < m; i++) {
int a, b, c, p;
cin >> a >> b >> c >> p;
adj[a].push_back(Edge(b, c, p));
adj[b].push_back(Edge(a, c, p));
}
priority_queue<pair<ll, State>> pq;
pq.push(make_pair(0, State(1, 0, 0)));
vis[State(1, 0, 0)] = 1;
while (!pq.empty()) {
auto node = pq.top().second;
pq.pop();
if (prop[node]) continue;
prop[node] = 1;
if (node.v == n) {
cout << d[node] << "\n";
return 0;
}
map<int, ll> somaP;
for (auto u : adj[node.v]) {
if (node.k && (u.to == node.x)) continue;
somaP[u.cor] += u.p;
}
for (auto u : adj[node.v]) {
if (u.to == node.x) continue;
// sem mudar a cor
State p1(u.to, node.v, 0);
ll p = (somaP[u.cor] - u.p);
ll dn = d[node];
ll np1 = (dn + p);
if (!vis[p1]) {
vis[p1] = 1;
d[p1] = np1;
pq.push({-np1, p1});
} else if (np1 < d[p1]) {
d[p1] = np1;
pq.push({-np1, p1});
}
// mudando
State p2(u.to, node.v, 1);
ll np2 = (dn + u.p);
if (!vis[p2]) {
vis[p2] = 1;
d[p2] = np2;
pq.push({-np2, p2});
} else if (np2 < d[p2]) {
d[p2] = np2;
pq.push({-np2, p2});
}
}
}
cout << "-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... |