이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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;
vector<ll> dijkstra(vector<vector<pair<ll, int>>> v, int source){
int n = v.size();
vector<ll> dis(n, inf);
vector<bool> bl(n, 0);
priority_queue<pair<ll, int>> pq;
dis[source] = 0;
pq.push({0, source});
while(!pq.empty()){
int nd = pq.top().sc; pq.pop();
if(bl[nd]) continue;
bl[nd] = 1;
for(auto [w, x]: v[nd]) if(dis[nd]+w < dis[x]){
dis[x] = dis[nd]+w;
pq.push({-dis[x], x});
}
}
return dis;
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
int n, m; cin >> n >> m;
vector<array<int, 4>> e(m);
vector<vector<pair<ll, int>>> v(n), vr(n);
for(int i = 0; i < m; i++){
cin >> e[i][0] >> e[i][1] >> e[i][2] >> e[i][3];
e[i][0]--, e[i][1]--;
v[e[i][0]].pb({e[i][2], e[i][1]});
vr[e[i][1]].pb({e[i][2], e[i][0]});
}
auto d1 = dijkstra(v, 0);
auto d2 = dijkstra(vr, n-1);
auto d3 = dijkstra(v, n-1);
auto d4 = dijkstra(vr, 0);
ll ans = inf;
ans = min(ans, d1[n-1]+d3[0]);
for(int i = 0; i < m; i++){
int a = e[i][0], b = e[i][1];
ll w = e[i][2], c = e[i][3];
ans = min(ans, d1[b]+w+c+d2[a]+d3[0]);
ans = min(ans, d1[n-1]+d3[b]+w+c+d4[a]);
ans = min(ans, c+d1[b]+w+d2[a]+d3[b]+w+d4[a]);
}
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... |