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 pb push_back
#define f first
#define sc second
using namespace std;
typedef long long int ll;
typedef string str;
const ll inf = 1e17;
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
int n, m; cin >> n >> m;
vector<array<int, 4>> edges(m);
for(int i = 0; i < m; i++){
cin >> edges[i][0] >> edges[i][1] >> edges[i][2] >> edges[i][3];
edges[i][0]--, edges[i][1]--;
}
ll ans = inf;
for(int tt = 0; tt <= m; tt++){
vector<vector<pair<int, ll>>> v(n);
ll cur = 0;
for(int i = 0; i < m; i++){
if(tt == i){
v[edges[i][1]].pb({edges[i][0], edges[i][2]});
cur+=edges[i][3];
}
else{
v[edges[i][0]].pb({edges[i][1], edges[i][2]});
}
}
priority_queue<pair<ll, int>> pq;
vector<ll> dis(n, inf);
vector<bool> bl(n, 0);
dis[0] = 0;
pq.push({0, 0});
while(!pq.empty()){
int nd = pq.top().sc; pq.pop();
if(bl[nd]) continue;
bl[nd] = 1;
for(auto [x, w]: v[nd]) if(dis[nd]+w < dis[x]){
dis[x] = dis[nd]+w;
pq.push({-dis[x], x});
}
}
cur+=dis[n-1];
cur = min(cur, inf);
bl.assign(n, 0);
dis.assign(n, inf);
dis[n-1] = 0;
pq.push({0, n-1});
while(!pq.empty()){
int nd = pq.top().sc; pq.pop();
if(bl[nd]) continue;
bl[nd] = 1;
for(auto [x, w]: v[nd]) if(dis[nd]+w < dis[x]){
dis[x] = dis[nd]+w;
pq.push({-dis[x], x});
}
}
cur+=dis[0];
ans = min(ans, cur);
}
if(ans >= inf) 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... |