#include <bits/stdc++.h>
using namespace std;
#define int long long
const int inf = 1e18;
void solve() {
int n, m;
cin >> n >> m;
vector<tuple<int, int, int, int>> edges(m);
vector<map<int, vector<int>>> color_map(n); // node -> {color: [edge indices]}
for (int i = 0; i < m; ++i) {
int x, y, col, wt;
cin >> x >> y >> col >> wt;
x--; y--;
edges[i] = {x, y, col, wt};
color_map[x][col].push_back(i);
color_map[y][col].push_back(i);
}
vector<vector<pair<int, int>>> adj(n); // {adj_node, cost}
for (int i = 0; i < m; ++i) {
auto [x, y, col, wt] = edges[i];
// Process x -> y
int sum_other = 0;
for (int idx : color_map[x][col]) {
if (idx != i) {
sum_other += get<3>(edges[idx]);
}
}
int cost_x = min(wt, sum_other);
adj[x].emplace_back(y, cost_x);
// Process y -> x
int sum_other_rev = 0;
for (int idx : color_map[y][col]) {
if (idx != i) {
sum_other_rev += get<3>(edges[idx]);
}
}
int cost_y = min(wt, sum_other_rev);
adj[y].emplace_back(x, cost_y);
}
vector<int> dist(n, inf);
dist[0] = 0;
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq;
pq.emplace(0, 0);
while (!pq.empty()) {
auto [d, u] = pq.top();
pq.pop();
if (d > dist[u]) continue;
for (auto [v, wt] : adj[u]) {
if (dist[v] > dist[u] + wt) {
dist[v] = dist[u] + wt;
pq.emplace(dist[v], v);
}
}
}
if (dist[n-1] == inf) cout << -1 << endl;
else cout << dist[n-1] << endl;
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
solve();
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... |