This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<ll,ll> pl;
typedef pair<int,int> pii;
typedef tuple<ll,int,int,int> tpl;
#define all(a) a.begin(), a.end()
#define filter(a) a.erase(unique(all(a)), a.end())
const int mn = 2e5 + 5;
vector<ll> dist[2][mn], cmpID[mn];
map<int,ll> sumWeight[mn];
vector<bool> vis[2][mn];
ll color[mn], weight[mn];
vector<pii> adj[mn];
int getArc (int u, int targ) {
return lower_bound(all(cmpID[u]), targ) - cmpID[u].begin();
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
int n, m; cin >> n >> m;
for (int i = 1; i <= m; i++) {
int a, b; cin >> a >> b >> color[i] >> weight[i];
cmpID[a].push_back(i), cmpID[b].push_back(i);
adj[a].emplace_back(b, i), adj[b].emplace_back(a, i);
}
for (int i = 1; i <= n; i++) {
sort(all(cmpID[i]));
for (int j = 0; j < 2; j++) {
dist[j][i] = vector<ll>(cmpID[i].size(), LLONG_MAX);
vis[j][i] = vector<bool>(cmpID[i].size());
}
if (dist[0][i].size() != cmpID[i].size()) {
cout << "bruh";
exit(0);
}
if (dist[1][i].size() != cmpID[i].size()) {
cout << "bruh";
exit(0);
}
for (pii item : adj[i]) {
int j, idx; tie(j, idx) = item;
sumWeight[i][color[idx]] += weight[idx];
}
}
assert(dist[1][1].size());
priority_queue<tpl> pq;
pq.emplace(0, 1, 1, 0), dist[1][1][0] = 0;
while (pq.size()) {
ll dst; int keepColor, u, arc;
tie(dst, keepColor, u, arc) = pq.top(); pq.pop();
if (vis[keepColor][u][arc]) continue;
vis[keepColor][u][arc] = 1;
int eID = cmpID[u][arc], preColor = (u == 1 ? -1 : color[eID]);
for (pii item : adj[u]) {
int v, idx; tie(v, idx) = item;
int nxtArc = getArc(v, idx);
if (color[idx] != preColor || !keepColor) {
// use color[idx] as the current color
ll curW = sumWeight[u][color[idx]] - weight[idx];
if (preColor == color[idx]) curW -= weight[eID];
if (dist[keepColor][u][arc] + curW < dist[1][v][nxtArc]) {
dist[1][v][nxtArc] = dist[keepColor][u][arc] + curW;
pq.emplace(-dist[1][v][nxtArc], 1, v, nxtArc);
}
}
if (1) {
// use a new color
if (dist[keepColor][u][arc] + weight[idx] < dist[0][v][nxtArc]) {
dist[0][v][nxtArc] = dist[keepColor][u][arc] + weight[idx];
pq.emplace(-dist[0][v][nxtArc], 0, v, nxtArc);
}
}
}
}
ll ans = LLONG_MAX;
for (int j = 0; j < 2; j++)
for (int i = 0; i < cmpID[n].size(); i++) ans = min(ans, dist[j][n][i]);
cout << (ans == LLONG_MAX ? -1 : ans);
return 0;
}
Compilation message (stderr)
Main.cpp: In function 'int main()':
Main.cpp:97:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
97 | for (int i = 0; i < cmpID[n].size(); i++) ans = min(ans, dist[j][n][i]);
| ~~^~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |