이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
#define ll long long
const ll INF = 1e18;
const int N = 101010, M = 202020;
const int V = 2020202;
int n, m;
struct edge {
int a, b, c;
ll p;
} e[2 * M];
vector<ll> sum[N];
vector<pair<ll, int>> g[V];
int cnt;
ll d[V];
vector<int> cs[N], vv[N];
int an[N], sz[N];
void go(int st) {
for (int i = 0; i < cnt; i++) d[i] = INF;
priority_queue<pair<ll, int>, vector<pair<ll, int>>, greater<pair<ll, int>>> q;
d[st] = 0;
q.push({d[st], st});
while (!q.empty()) {
auto t = q.top();
q.pop();
int v = t.second;
if (t.first != d[v]) continue;
for (auto p : g[v]) {
ll w = p.first;
int u = p.second;
//cout << w << " " << u << "\n";
if (d[v] + w < d[u]) {
d[u] = d[v] + w;
q.push({d[u], u});
}
}
}
}
int getpos(int v, int c) {
return lower_bound(cs[v].begin(), cs[v].end(), c) - cs[v].begin();
}
int main() {
#ifdef LOCAL
freopen("input.txt", "r", stdin);
#endif
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n >> m;
for (int i = 0; i < m; i++) {
cin >> e[i].a >> e[i].b >> e[i].c >> e[i].p;
e[i].a--;
e[i].b--;
e[i].c--;
e[i + m] = e[i];
swap(e[i + m].a, e[i + m].b);
}
for (int i = 0; i < 2 * m; i++) cs[e[i].a].push_back(e[i].c);
cnt = 4 * m;
for (int i = 0; i < n; i++) {
sort(cs[i].begin(), cs[i].end());
cs[i].erase(unique(cs[i].begin(), cs[i].end()), cs[i].end());
sz[i] = cs[i].size();
sum[i].resize(sz[i]);
for (int j = 0; j < sz[i]; j++) sum[i][j] = 0;
vv[i].resize(sz[i]);
for (int j = 0; j < sz[i]; j++) vv[i][j] = cnt++;
an[i] = cnt++;
}
for (int i = 0; i < 2 * m; i++) {
int pos = getpos(e[i].a, e[i].c);
sum[e[i].a][pos] += e[i].p;
}
for (int i = 0; i < 2 * m; i++) {
//cout << e[i].a << " " << e[i].b << " " << e[i].c << " " << e[i].p << "\n";
int pos = getpos(e[i].a, e[i].c);
ll s = sum[e[i].a][pos] - e[i].p;
g[an[e[i].a]].push_back({s, i});
g[an[e[i].a]].push_back({0, i + 2 * m});
g[vv[e[i].a][pos]].push_back({s, i});
g[i + 2 * m].push_back({e[i].p, an[e[i].b]});
g[i].push_back({0, an[e[i].b]});
pos = getpos(e[i].b, e[i].c);
g[i + 2 * m].push_back({0, vv[e[i].b][pos]});
}
int st = an[0], fin = an[n - 1];
go(st);
if (d[fin] == INF) {
cout << "-1\n";
} else cout << d[fin] << "\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... |