이 제출은 이전 버전의 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];
ve<ll> dijkstra(int st, bool type){
priority_queue<pair<ll,int>, ve<pair<ll,int>>, greater<pair<ll,int>>> pq;
ve<ll> dist(n+1, INF);
dist[st] = 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]){
int to = y[j];
if(type) to = x[j];
if(dist[to] > (c[j] + dist[u])){
dist[to] = c[j] + dist[u];
pq.push({dist[to], to});
}
}
}
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);
ve<ll> distn = dijkstra(n, 0);
ve<ll> distrev1 = dijkstra(1, 1);
ve<ll> distrevn = dijkstra(n, 1);
ll aff = min(dist1[n] + distn[1], (ll)INF);
for (int i = 0; i < m; ++i)
{
aff = min(aff, dist1[y[i]] + distrevn[x[i]] + distn[1] + 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[y[i]] + distrev1[x[i]] + 2*c[i] + 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... |