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;
using ll = long long;
const int N = 1000;
const int M = 100000;
const ll INF = 1e18;
int n, m;
vector<pair<int, int>> g[N], g0[N];
ll C[M], D[M];
pair<int, int> edges[M];
vector<ll> dijkstra(int s, vector<pair<int, int>> g[]) {
vector<ll> dist(n + 1, INF);
dist[s] = 0;
set<pair<ll, int>> st;
for(int i = 1; i <= n; i++)
st.insert({dist[i], i});
while(!st.empty()) {
s = st.begin()->second;
st.erase(st.begin());
for(auto [to, i] : g[s]) {
if(dist[to] > dist[s] + C[i]) {
st.erase({dist[to], to});
dist[to] = dist[s] + C[i];
st.insert({dist[to], to});
}
}
}
return dist;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
cin >> n >> m;
for(int i = 0; i < m; i++) {
int u, v;
cin >> u >> v;
edges[i] = {u, v};
cin >> C[i] >> D[i];
g[u].push_back({v, i});
g0[v].push_back({u, i});
}
bool subtask2 = 1;
for(int i = 0; i < m; i += 2)
subtask2 &= (edges[i] == edges[i + 1] && C[i] == C[i + 1]);
if(subtask2) {
auto d1 = dijkstra(1, g);
auto dn = dijkstra(n, g);
auto e1 = dijkstra(1, g0);
auto en = dijkstra(n, g0);
ll res = d1[n] + dn[1];
for(int i = 0; i < m; i++) {
auto [u, v] = edges[i];
res = min(res, d1[u] + en[v] + dn[1] + C[i] + D[i]);
res = min(res, dn[v] + e1[u] + d1[n] + C[i] + D[i]);
}
if(res >= INF) res = -1;
cout << res;
return 0;
}
if(m <= 1000) {
ll res = dijkstra(1, g)[n] + dijkstra(n, g)[1];
for(int i = 0; i < m; i++) {
auto [u, v] = edges[i];
g[u].erase(find(g[u].begin(), g[u].end(), make_pair(v, i)));
g[v].push_back({u, i});
res = min(res, dijkstra(1, g)[n] + dijkstra(n, g)[1] + D[i]);
g[u].push_back({v, i});
g[v].erase(find(g[v].begin(), g[v].end(), make_pair(u, i)));
}
if(res >= INF) res = -1;
cout << res;
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... |