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 edge;
bool order;
bool k;
State(const int& a = 0, const bool& b = 0, const bool& c = 0) : edge(a), order(b), k(c) {}
bool operator < (const State& a) const {
return (make_pair(edge, make_pair(order, k)) < make_pair(a.edge, make_pair(a.order, a.k)));
}
};
struct Edge {
int to, cor, p, idx;
bool order;
Edge(const int& a = 0, const int& b = 0, const int& c = 0, const int& d = 0, const bool& e = 0) : to(a), cor(b), p(c), idx(d), order(e) {}
bool operator < (const Edge& a) const {
return (make_pair(cor, idx) < make_pair(a.cor, a.idx));
}
};
const int MAXN = 2e5 + 10;
const int MAXM = 2e5 + 10;
vector<Edge> adj[MAXN];
pii edges[MAXM];
ll d[2][2][MAXM];
bool vis[2][2][MAXM];
bool prop[2][2][MAXM];
int edgeCor[MAXM];
int edgeP[MAXM];
vector<ll> pCor[MAXN];
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
int n, m;
cin >> n >> m;
for (int i = 1; i <= m; i++) {
int a, b, c, p;
cin >> a >> b >> c >> p;
adj[a].push_back(Edge(b, c, p, i, 1));
adj[b].push_back(Edge(a, c, p, i, 0));
edges[i] = {a, b};
edgeCor[i] = c;
edgeP[i] = p;
}
for (int i = 1; i <= n; i++) {
sort(adj[i].begin(), adj[i].end());
}
priority_queue<pair<ll, State>> pq;
for (int v = 1; v <= n; v++) {
int t = adj[v].size();
int start = 0;
ll curr = 0;
vector<ll> ans(t);
for (int i = 0; i < t; i++) {
if (adj[v][start].cor != adj[v][i].cor) {
for (int j = start; j < i; j++) ans[j] = curr;
curr = 0;
start = i;
}
curr += adj[v][i].p;
}
for (int i = start; i < t; i++) ans[i] = curr;
pCor[v] = ans;
}
edges[0] = {1, 0};
pq.push(make_pair(0, State(0, 0, 0)));
vis[0][0][0] = 1;
while (!pq.empty()) {
auto node = pq.top().second;
pq.pop();
if (prop[node.k][node.order][node.edge]) continue;
prop[node.k][node.order][node.edge] = 1;
pii abc = edges[node.edge];
if (node.order) swap(abc.fr, abc.se);
int v = abc.fr;
int x = abc.se;
ll dn = d[node.k][node.order][node.edge];
if (v == n) {
cout << dn << "\n";
return 0;
}
int i = 0;
unordered_map<int, ll> somaP;
for (auto u : adj[v]) {
if (node.k && (u.to == x)) continue;
somaP[u.cor] += u.p;
}
for (auto u : adj[v]) {
if (u.to == x) {
i++;
continue;
}
// sem mudar a cor
State p1(u.idx, u.order, 0);
ll p = (pCor[v][i] - u.p);
if (node.k && (edgeCor[node.edge] == u.cor)) {
p -= edgeP[node.edge];
}
i++;
ll np1 = (dn + p);
if (!vis[p1.k][p1.order][p1.edge]) {
vis[p1.k][p1.order][p1.edge] = 1;
d[p1.k][p1.order][p1.edge] = np1;
pq.push({-np1, p1});
} else if (np1 < d[p1.k][p1.order][p1.edge]) {
d[p1.k][p1.order][p1.edge] = np1;
pq.push({-np1, p1});
}
// mudando
State p2(u.idx, u.order, 1);
ll np2 = (dn + u.p);
if (!vis[p2.k][p2.order][p2.edge]) {
vis[p2.k][p2.order][p2.edge] = 1;
d[p2.k][p2.order][p2.edge] = np2;
pq.push({-np2, p2});
} else if (np2 < d[p2.k][p2.order][p2.edge]) {
d[p2.k][p2.order][p2.edge] = 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... |