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 int long long
#define nl endl
using namespace std;
multiset<pair<int,int> > graph[201];
int Dijkstra(int u, int v){
priority_queue<pair<int,int>, vector<pair<int,int> >, greater<pair<int,int> > > pq;
vector<int> vis(201, 0);
vector<int> dist(201, 1e18);
dist[u] = 0;
pq.push(make_pair(0, u));
while(!pq.empty()){
int p = pq.top().second;
int dis = pq.top().first;
pq.pop();
if(vis[p]) continue;
vis[p] = 1;
for(auto ch : graph[p]){
if(dis + ch.second < dist[ch.first]){
dist[ch.first] = dis + ch.second;
pq.push(make_pair(dist[ch.first], ch.first));
}
}
}
return dist[v];
}
signed main(){
ios_base::sync_with_stdio(0);
cin.tie(NULL);cout.tie(NULL);
int n, m;
cin>>n>>m;
vector<vector<int> > dist(n+1, vector<int>(n+1, 1e18));
int U[m], V[m], c[m], d[m];
for(int i=0; i<m; i++){
cin>>U[i]>>V[i]>>c[i]>>d[i];
graph[U[i]].insert(make_pair(V[i], c[i]));
dist[U[i]][V[i]] = min(c[i], dist[U[i]][V[i]]);
}
for(int i=1; i<=n; i++) dist[i][i] = 0;
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];
//cout<<ans<<nl;
for(int i=0; i<m; i++){
int u = U[i], v = V[i];
if(min(dist[1][n], dist[1][v] + c[i] + dist[u][n]) + min(dist[n][1], dist[n][v] + c[i] + dist[u][1]) + d[i] >= ans) continue;
graph[u].erase(graph[u].find(make_pair(v, c[i])));
graph[v].insert(make_pair(u, c[i]));
ans = min(ans, Dijkstra(1, n) + Dijkstra(n, 1) + d[i]);
//cout<<i<<" "<<Dijkstra(1, n) + Dijkstra(n, 1) + d[i]<<nl;
graph[u].insert(make_pair(v, c[i]));
graph[v].erase(graph[v].find(make_pair(u, c[i])));
}
if(ans >= 1e18) cout<<-1;
else cout<<ans;
}
# | 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... |