이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#define INF 0x3f3f3f3f3f3f3f3f
#define MX 100000
#define ll long long
using namespace std;
struct Edge {
int color, next;
ll price;
Edge (int _color = 0, int _next = 0, ll _price = 0) {
color = _color, next = _next, price = _price;
}
};
struct Node {
ll dist;
int color, cur;
Node (ll _dist = 0, int _color = 0, int _cur = 0) {
dist = _dist, color = _color, cur = _cur;
}
bool operator<(const Node& x) const { return dist > x.dist; }
};
int N, M;
map<int, vector<Edge>> adj[MX + 1];
ll dist1[MX + 1];
map<int, ll> dist2[MX + 1], sum[MX + 1];
priority_queue<Node> q;
int main() {;
cin >> N >> M;
for (int i = 1; i <= M; i++) {
int a, b, c;
ll d;
cin >> a >> b >> c >> d;
adj[a][c].push_back(Edge(c, b, d));
adj[b][c].push_back(Edge(c, a, d));
sum[a][c] += d;
sum[b][c] += d;
}
fill(dist1 + 1, dist1 + 1 + N, INF);
dist1[1] = 0;
q.push(Node(0, 0, 1));
while (q.size()) {
ll cost = q.top().dist;
int node = q.top().cur, color = q.top().color;
q.pop();
if (color) {
if (dist2[node][color] != cost) continue;
for (Edge edge : adj[node][color]) {
// We can't flip i in this case
ll case1 = sum[node][color] - edge.price;
if (case1 + cost < dist1[edge.next]) {
dist1[edge.next] = case1 + cost;
q.push(Node(dist1[edge.next], 0, edge.next));
}
}
}
else {
if (dist1[node] != cost) continue;
for (auto& c : adj[node]) {
for (Edge edge : c.second) {
// Case 1: We don't flip j
ll case1 = sum[node][edge.color] - edge.price + cost;
if (case1 < dist1[edge.next]) {
dist1[edge.next] = case1;
q.push(Node(dist1[edge.next], 0, edge.next));
}
// Case 2: We flip j but not another edge of the same colour
ll case2 = edge.price + cost;
if (case2 < dist1[edge.next]) {
dist1[edge.next] = case2;
q.push(Node(dist1[edge.next], 0, edge.next));
}
// Case 3: We flip j and another edge of the same colour
ll case3 = cost;
if (!dist2[edge.next].count(edge.color) || case3 < dist2[edge.next][edge.color]) {
dist2[edge.next][edge.color] = case3;
q.push(Node(dist2[edge.next][edge.color], edge.color, edge.next));
}
}
}
}
}
if (dist1[N] >= INF) cout << "-1\n";
else cout << dist1[N] << "\n";
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |