이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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());
}
for (pii item : adj[i]) {
int j, idx; tie(j, idx) = item;
sumWeight[i][color[idx]] += weight[idx];
}
}
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);
assert(cmpID[v][nxtArc] == 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;
}
컴파일 시 표준 에러 (stderr) 메시지
Main.cpp: In function 'int main()':
Main.cpp:88:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
88 | 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... |