이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include<bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#define INF 1e15
#define ve vector
#define vi ve<int>
#define ii pair<int,int>
#define vii ve<ii>
#define pb push_back
#define fi first
#define se second
#define ll long long
using namespace __gnu_pbds;
using namespace std;
const int nax = 1e4+5;
const int kax = 1e5+5;
const int MOD = 1e9+7;
template <class T> using Tree = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
int x[kax], y[kax];
ll c[kax], d[kax];
int n,m;
vi adj[2][nax];
set<int> way1, way2;
ve<ll> dijkstra(int st, bool type, int del){
priority_queue<pair<ll,int>, ve<pair<ll,int>>, greater<pair<ll,int>>> pq;
ve<ll> dist(n+1, INF);
dist[st] = 0;
vi par(n+1,0);
pq.push({0ll, st});
while(!pq.empty()){
ll cst = pq.top().fi;
int u = pq.top().se;
pq.pop();
if(dist[u] != cst) continue;
for(auto j : adj[type][u]){
if(j == del) continue;
int to = y[j];
if(type) to = x[j];
if(dist[to] > (c[j] + dist[u])){
dist[to] = c[j] + dist[u];
par[to] = j;
pq.push({dist[to], to});
}
}
if(del != -1 && u == y[del] && dist[x[del]] > (c[del] + dist[u])){
dist[x[del]] = c[del] + dist[u];
pq.push({dist[x[del]], x[del]});
}
}
if(way1.size() == 0){
if(dist[n] == INF) way1.insert(-1);
int u = n;
while(par[u]){
way1.insert(par[u]);
u = x[par[u]];
}
} else if(way2.size() == 0){
if(dist[1] == INF) way2.insert(-1);
int u = 1;
while(par[u]){
way2.insert(par[u]);
u = x[par[u]];
}
}
return dist;
}
int main(){
ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
cin >> n >> m;
for (int i = 0; i < m; ++i)
{
cin >> x[i] >> y[i] >> c[i] >> d[i];
adj[0][x[i]].pb(i);
adj[1][y[i]].pb(i);
}
ve<ll> dist1 = dijkstra(1, 0, -1), distn = dijkstra(n, 0, -1), distrev1 = dijkstra(1, 1, -1) , distrevn = dijkstra(n, 1, -1);
ll aff = min(dist1[n] + distn[1], (ll)INF);
for (int i = 0; i < m; ++i)
{
if(!way1.count(i) && !way2.count(i)){
aff = min(aff, dist1[y[i]] + distrevn[x[i]] + distn[y[i]] + distrev1[x[i]] + 2*c[i] + d[i]);
aff = min(aff, dist1[n] + distn[y[i]] + distrev1[x[i]] + c[i] + d[i]);
aff = min(aff, dist1[y[i]] + distrevn[x[i]] + distn[1] + c[i] + d[i]);
} else {
ve<ll> cur1 = dijkstra(1, 0, i), curn = dijkstra(n, 0, i);
aff = min(aff, cur1[n] + curn[1] + d[i]);
}
}
if(aff == INF) cout << -1 << endl;
else cout << aff << endl;
}
# | 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... |