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;
#define int long long
#define INF 1e18
#define endl '\n'
const int N = 205;
struct node {
int u, v, c, d;
};
multiset<pair<int, int>> graph[N];
vector<int> dijkstra(int src) {
vector<int> dist(N, INF);
vector<bool> vis(N, false);
dist[src] = 0;
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq;
pq.push({0, src});
while (!pq.empty()) {
int u = pq.top().second;
pq.pop();
if (vis[u]) continue;
vis[u] = true;
for (auto& [v, weight] : graph[u]) {
if (dist[v] > dist[u] + weight) {
dist[v] = dist[u] + weight;
pq.push({dist[v], v});
}
}
}
return dist;
}
void solve() {
int n, m;
cin >> n >> m;
vector<node> edges(m);
for (int i = 0; i < m; ++i) {
cin >> edges[i].u >> edges[i].v >> edges[i].c >> edges[i].d;
graph[edges[i].u].insert({edges[i].v, edges[i].c});
}
vector<vector<int>> dist(n + 1, vector<int>(n + 1, INF));
for (int i = 1; i <= n; ++i) dist[i][i] = 0;
for (auto& e : edges) {
dist[e.u][e.v] = min(dist[e.u][e.v], e.c);
}
for (int k = 1; k <= n; ++k) {
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= n; ++j) {
dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]);
}
}
}
int ans = dist[1][n] + dist[n][1];
for (auto& e : edges) {
int u = e.u, v = e.v;
if (min(dist[1][n], dist[1][v] + e.c + dist[u][n]) + min(dist[n][1], dist[n][v] + e.c + dist[u][1]) + e.d >= ans) continue;
graph[u].erase(graph[u].find({v, e.c}));
graph[v].insert({u, e.c});
auto dis1 = dijkstra(1);
auto dis2 = dijkstra(n);
ans = min(ans, dis1[n] + dis2[1] + e.d);
graph[v].erase(graph[v].find({u, e.c}));
graph[u].insert({v, e.c});
}
if (ans >= INF) cout << -1 << endl;
else cout << ans << endl;
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
solve();
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... |