이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define sp ' '
#define en '\n'
#define smin(a, b) a = min(a, b)
#define smax(a, b) a = max(a, b)
using namespace std;
const int N = 200 + 2;
const int M = 5e4 + 2;
const ll linf = 1e18;
ll dist[N][N], di[N];
vector<pair<int, int>> g[N];
int u[M], v[M], c[M], d[M], is[M], prv[N], n, m;
void Dijkstra(int x) {
for (int i = 0; i < n; i++) di[i] = linf;
vector<int> was(n);
priority_queue<pair<ll, int>> pq;
di[x] = 0;
pq.push({0, x});
while (!pq.empty()) {
ll v = -pq.top().first;
int s = pq.top().second;
pq.pop();
if (was[s] == 1) continue;
was[s] = 1;
for (auto zz : g[s]) {
int u = zz.first; int i = zz.second;
if (di[u] > v + c[i]) {
di[u] = v + c[i];
pq.push({-di[u], u});
prv[u] = i;
}
}
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n >> m;
for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) if (i != j) dist[i][j] = linf;
for (int i = 0; i < m; i++) {
cin >> u[i] >> v[i] >> c[i] >> d[i];
u[i] -= 1; v[i] -= 1;
smin(dist[u[i]][v[i]],(ll) c[i]);
g[u[i]].push_back({v[i], i});
}
for (int k = 0; k < n; k++) for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) smin(dist[i][j], dist[i][k] + dist[k][j]);
for (int i = 0; i < n; i++) prv[i] = -1;
Dijkstra(0);
for (int i = 0; i < n; i++) if (prv[i] != -1) is[prv[i]] = 1;
for (int i = 0; i < n; i++) prv[i] = -1;
Dijkstra(n - 1);
for (int i = 0; i < n; i++) if (prv[i] != -1) is[prv[i]] = 1;
for (int i = 0; i < m; i++) cout << is[i] << sp;
cout << en;
ll ans = linf;
if (dist[0][n - 1] != linf && dist[n - 1][0] != linf) ans = dist[0][n - 1] + dist[n - 1][0];
for (int i = 0; i < m; i++) {
if (is[i] == 0) {
ll d1 = min(dist[0][n - 1], dist[0][v[i]] + c[i] + dist[u[i]][n - 1]);
ll d2 = min(dist[n - 1][0], dist[n - 1][v[i]] + c[i] + dist[u[i]][0]);
if (d1 < linf && d2 < linf) smin(ans, d1 + d2 + d[i]);
continue;
}
for (int j = 0; j < n; j++) g[i].clear();
for (int j = 0; j < m; j++) {
if (i == j) g[v[j]].push_back({u[j], j});
else g[u[j]].push_back({v[j], j});
}
ll ret = d[i];
Dijkstra(0);
if (di[n - 1] == linf) continue;
ret += di[n - 1];
Dijkstra(n - 1);
if (di[0] == linf) continue;
ret += di[0];
smin(ans, ret);
}
if (ans == linf) ans = -1;
cout << ans << en;
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... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |