이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n, m;
cin >> n >> m;
vector<vector<array<int, 3>>> g(n);
vector<vector<int>> col(n);
for (int i = 0; i < m; i++) {
int u, v, c, p;
cin >> u >> v >> c >> p;
--u; --v; --c;
g[u].push_back({v, c, p});
g[v].push_back({u, c, p});
col[u].push_back(c);
col[v].push_back(c);
}
const long long inf = 1e18;
vector<vector<long long>> dist(n);
vector<vector<long long>> tot(n);
vector<vector<vector<int>>> ids(n);
for (int i = 0; i < n; i++) {
col[i].push_back(-1);
sort(col[i].begin(), col[i].end());
col[i].erase(unique(col[i].begin(), col[i].end()), col[i].end());
int sz = (int) col[i].size();
dist[i] = vector<long long>(sz, inf);
tot[i].resize(sz);
ids[i].resize(sz);
for (int j = 0; j < (int) g[i].size(); j++) {
array<int, 3> p = g[i][j];
int idx = (int) (lower_bound(col[i].begin(), col[i].end(), p[1]) - col[i].begin());
tot[i][idx] += p[2];
ids[i][idx].push_back(j);
}
}
dist[0][0] = 0;
set<tuple<long long, int, int>> st;
auto Update = [&](int v, long long d, int c) {
c = (int) (lower_bound(col[v].begin(), col[v].end(), c) - col[v].begin());
if (dist[v][c] <= d) {
return;
}
if (dist[v][c] != inf) {
st.erase(st.find({dist[v][c], v, c}));
}
dist[v][c] = d;
st.insert({dist[v][c], v, c});
};
st.emplace(0, 0, 0);
while (!st.empty()) {
auto it = st.begin();
long long d = get<0>(*it);
int i = get<1>(*it);
int c = get<2>(*it);
st.erase(it);
if (c == 0) {
for (auto& p : g[i]) {
Update(p[0], d, p[1]);
int idx = (int) (lower_bound(col[i].begin(), col[i].end(), p[1]) - col[i].begin());
Update(p[0], d + min((long long) p[2], tot[i][idx] - p[2]), -1);
}
} else {
for (int j : ids[i][c]) {
array<int, 3> p = g[i][j];
Update(p[0], d + tot[i][c] - p[2], -1);
}
}
}
if (dist[n - 1][0] == inf) {
cout << -1 << '\n';
} else {
cout << dist[n - 1][0] << '\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... |