이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
//#define DEBUG
void setIO(string name) {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cin.exceptions(istream::failbit);
#ifdef LOCAL
freopen((name + ".in").c_str(), "r", stdin);
freopen((name + ".out").c_str(), "w", stdout);
freopen((name + ".out").c_str(), "w", stderr);
#endif
}
const int maxn = 1e5 + 5;
const long long inf = 1e18;
struct edge {
long long e, c, p;
};
vector<edge> adj[maxn];
long long dist[maxn];
int main() {
setIO("1");
int n, m;
cin >> n >> m;
for (int i = 0; i < m; ++i) {
int a, b, c, p;
cin >> a >> b >> c >> p;
--a, --b;
adj[a].push_back({b, c, p});
adj[b].push_back({a, c, p});
}
for (int i = 0; i < n; ++i) {
dist[i] = inf;
}
dist[0] = 0;
priority_queue<pair<long long, int>, vector<pair<long long, int>>, greater<>> q;
q.push({0, 0});
while (!q.empty()) {
map<int, pair<int, long long>> colors;
pair<long long, int> cur = q.top();
q.pop();
long long node = cur.second, cost = cur.first;
if (dist[node] != cost) {
continue;
}
for (auto i : adj[node]) {
++colors[i.c].first;
colors[i.c].second += i.p;
}
for (auto i : adj[node]) {
if (colors[i.c].first == 1) {
if (dist[i.e] > cost) {
#ifdef DEBUG
cout << node << " " << i.e << " " << cost << "\n";
#endif
q.push({dist[i.e] = cost, i.e});
}
} else {
long long nc = cost + min(i.p, colors[i.c].second - i.p);
if (dist[i.e] > nc) {
#ifdef DEBUG
cout << node << " " << i.e << " " << cost << " " << nc << "\n";
#endif
q.push({dist[i.e] = nc, i.e});
}
}
}
}
#ifdef DEBUG
for (int i = 0; i < n; ++i) {
cout << dist[i] << " ";
}
cout << "\n";
#endif
cout << (dist[n - 1] == inf ? -1 : dist[n - 1]) << "\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... |