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>
#define ll long long
#define pii pair<int,int>
using namespace std;
const int mxn=2e5+5;
const ll INF=1e15;
int n,m;
int l[mxn], r[mxn];
ll c[mxn], d[mxn];
vector<int> adj[mxn];
bool blocked[mxn] = {};
bool used[mxn] = {};
void dijkstra(int s, vector<ll> &d, vector<int> & p) {
d.resize(n + 1);
p.resize(n + 1);
for(int i = 1; i <= n; i++) {
d[i] = INF;
p[i] = -1;
}
vector<bool> u(n, false);
d[s] = 0;
for(int i = 1; i <= n; i++) {
int v = -1;
for (int j = 1; j <= n; j++) {
if (!u[j] && (v == -1 || d[j] < d[v]))
v = j;
}
if(d[v] == INF)
break;
u[v] = true;
for (auto edge : adj[v]) {
if(blocked[edge]) continue;
int to = r[edge];
ll len = c[edge];
if (d[v] + len < d[to]) {
d[to] = d[v] + len;
p[to] = edge;
}
}
}
}
int main() {
cin >> n >> m;
ll w[n + 1][n + 1];
for(int i = 1; i <= n; i++) for(int j = 1; j <= n; j++) {
if(i == j) w[i][j] = 0;
else w[i][j] = INF;
}
for(int i = 1; i <= m; i++) {
scanf("%d %d %lld %lld",&l[i],&r[i],&c[i],&d[i]);
adj[l[i]].push_back(i);
w[l[i]][r[i]] = min(w[l[i]][r[i]], c[i]);
}
for(int k = 1; k <= n; k++) {
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= n; j++) {
w[i][j] = min(w[i][j], w[i][k] + w[k][j]);
}
}
}
ll ans = w[1][n] + w[n][1];
int source = 1;
int target = n;
vector<ll> dist1, dist2;
vector<int> last1, last2;
dijkstra(source, dist1, last1);
dijkstra(target, dist2, last2);
vector<int> sedg;
if(dist1[target] < INF) {
int nd = target;
while(nd != source) {
used[last1[nd]] = true;
sedg.push_back(last1[nd]);
nd = l[last1[nd]];
}
}
if(dist2[source] < INF) {
int nd = source;
while(nd != target) {
used[last2[nd]] = true;
sedg.push_back(last2[nd]);
nd = l[last2[nd]];
}
}
// for(int i = 1; i <= m; i++) {
// if(used[i]) continue;
// ans = min(ans, w[source][target] + w[target][r[i]] + d[i] + c[i] + w[l[i]][source]);
// ans = min(ans, d[i] + w[source][r[i]] + c[i] + w[l[i]][target] + w[target][source]);
// ans = min(ans, d[i] + w[source][r[i]] + c[i] + w[l[i]][target] + w[target][r[i]] + c[i] + w[l[i]][source]);
// }
for(int i = 1; i <= m; i++) sedg.push_back(i);
for(int i : sedg) {
blocked[i] = true;
vector<int> cl;
l[m + 1] = r[i];
r[m + 1] = l[i];
c[m + 1] = c[i];
adj[l[m + 1]].push_back(m + 1);
dijkstra(source, dist1, cl);
dijkstra(target, dist2, cl);
ans = min(ans, dist1[target] + dist2[source]);
blocked[i] = false;
adj[l[m + 1]].pop_back();
}
if(ans >= INF) cout<<-1<<endl;
else cout<<ans<<endl;
}
Compilation message (stderr)
ho_t4.cpp: In function 'int main()':
ho_t4.cpp:62:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
62 | scanf("%d %d %lld %lld",&l[i],&r[i],&c[i],&d[i]);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |