이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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;
//vector<int> adjQ[MAXN];
//bool visQ[MAXN];
//
//void dfs(int node) {
// if (visQ[node]) return;
// visQ[node] = 1;
// for (auto u : adjQ[node]) dfs(u);
//}
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));
//adjQ[a].push_back(b);
//adjQ[b].push_back(a);
}
//dfs(1);
//if (!visQ[n]) {
// cout << "-1\n";
// return 0;
//}
deque<pair<ll, State>> pq;
pq.push_back(make_pair(0, State(1, 0, 0)));
vis[State(1, 0, 0)] = 1;
while (!pq.empty()) {
auto node = pq.front().second;
pq.pop_front();
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
ll dn = d[node];
if (somaP[u.cor] == u.p) {
State p1(u.to, node.v, 0);
ll p = 0;
ll np1 = (dn + p);
if (!vis[p1]) {
vis[p1] = 1;
d[p1] = np1;
pq.push_front({-np1, p1});
} else if (np1 < d[p1]) {
d[p1] = np1;
pq.push_front({-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_back({-np2, p2});
} else if (np2 < d[p2]) {
d[p2] = np2;
pq.push_back({-np2, p2});
}
}
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |