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 int long long
#define pii pair<long long, long long>
#define fr first
#define se second
int 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, int> d;
map<State, bool> vis;
map<State, bool> prop;
map<State, State> dad;
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<int, 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;
map<int, int> 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);
int p = (somaP[u.cor] - u.p);
int dn = d[node];
int 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);
int 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});
}
}
}
int ans = INF;
for (int i = 1; i <= n; i++) {
if (vis[State(n, i, 0)]) ans = min(ans, d[State(n, i, 0)]);
if (vis[State(n, i, 1)]) ans = min(ans, d[State(n, i, 1)]);
}
if (ans == INF) cout << "-1\n";
else cout << ans << "\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... |