이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#pragma GCC optimize("O3")
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
#define ll long long
const ll N = 201, M = 50001;
ll n, m;
struct Edge {
ll u, v, i, c;
};
vector<pair<ll, pair<ll, bool>>> adj[N];
ll c[M], d[M];
Edge edges[M];
ll sssp(ll source, ll i, ll dest) {
vector<ll> dist(n + 1, 1e15);
dist[source] = 0;
priority_queue<pair<ll, ll>> pq;
pq.push({0, source});
while (!pq.empty()) {
ll node = pq.top().second, _dist = -pq.top().first;
pq.pop();
if (node == dest) {
return dist[node];
}
for (auto &nb : adj[node]) {
if (nb.second.second) {
if (nb.second.first != i) {
continue;
}
} else if (nb.second.first == i) {
continue;
}
if (dist[nb.first] > _dist + c[nb.second.first]) {
dist[nb.first] = _dist + c[nb.second.first];
pq.push({-dist[nb.first], nb.first});
}
}
}
return dist[dest];
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n >> m;
for (ll i = 1; i <= n; ++i) {
adj[i].clear();
}
vector<vector<ll>> dist(n + 1, vector<ll>(n + 1, 1e15));
for (ll i = 1; i <= n; ++i) {
dist[i][i] = 0;
}
for (ll i = 0, u, v; i < m; ++i) {
cin >> u >> v >> c[i] >> d[i];
adj[u].push_back({v, {i, 0}}), adj[v].push_back({u, {i, 1}});
edges[i].u = u, edges[i].v = v, edges[i].i = i, edges[i].c = c[i];
dist[u][v] = min(dist[u][v], c[i]);
}
for (ll k = 1; k <= n; ++k) {
for (ll i = 1; i <= n; ++i) {
for (ll j = 1; j <= n; ++j) {
dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]);
}
}
}
ll ans = min((ll)1e15, dist[1][n] + dist[n][1]);
for (auto &[u, v, i, c] : edges) {
ll d1 = min(dist[1][n], dist[1][v] + c + dist[u][n]);
ll d2 = min(dist[n][1], dist[n][v] + c + dist[u][1]);
if (d1 + d2 + d[i] < ans) {
ll actual = d[i] + sssp(1, i, n) + sssp(n, i, 1);
ans = min(ans, actual);
}
}
if (ans == (ll)(1e15)) {
ans = -1;
}
cout << ans << "\n";
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |