이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include<bits/stdc++.h>
using namespace std;
vector<int> kosten;
void dijkstra(vector<set<pair<int,int>>>& adj, vector<long long>& dist, int start, vector<pair<int,int>>&parent){
priority_queue<pair<long long,int>, vector<pair<long long,int>>, greater<pair<long long,int>>> pq;
dist[start] = 0;
pq.push({0,start});
while(!pq.empty()){
long long wo = pq.top().second, preis = pq.top().first;
pq.pop();
if(dist[wo]== preis){
for(auto[ziel, index]: adj[wo]){
if(dist[ziel] > kosten[index]+preis){
dist[ziel] = kosten[index]+preis;
parent[ziel] = {wo, index};
pq.push({dist[ziel], ziel});
}
}
}
}
}
signed main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int v, e; cin >> v >> e;
vector<set<pair<int,int>>> adj(v), adj2(v);
vector<tuple<int,int,int,int>> edges(e);
vector<int> umdrehkosten(e);
kosten = vector<int>(e);
for(int i=0; i<e; i++){
int a, b; cin >> a >> b >> kosten[i] >> umdrehkosten[i];
a--; b--;
adj[a].insert({b, i});
adj2[b].insert({a, i});//umgedrehter Graph
edges[i] = make_tuple(a,b,kosten[i],umdrehkosten[i]);
}
vector<long long> von0(v, 1e17), vonn_r(v, 1e17), vonn(v, 1e17), von0_r(v, 1e17);
vector<pair<int,int>> parent0(v, {-1, -1}), parent2(v, {-1, -1}), parentn(v, {-1, -1});
dijkstra(adj, von0, 0, parent0);
dijkstra(adj2, vonn_r, v-1, parent2);
dijkstra(adj2, von0_r, 0, parent2);
dijkstra(adj, vonn, v-1, parentn);
int ort = v-1;
vector<bool> used(e, false);
while(parent0[ort].first!=-1){
used[parent0[ort].second] = true;
ort = parent0[ort].first;
}
ort = 0;
while(parentn[ort].first!=-1){
used[parentn[ort].second] = true;
ort = parentn[ort].first;
}
long long res = min((long long)1e17, (long long)von0[v-1]+vonn[0]);//keine Kante gedreht
for(int i=0; i<e; i++){
auto [start, ende, cost, r_cost] = edges[i];
if(used[i]){
adj[start].erase({ende, i});
adj[ende].insert({start, i});
vector<long long> dist(v, 1e17), dist2(v, 1e17);
dijkstra(adj, dist, 0, parent0);
dijkstra(adj, dist2, v-1, parent0);
res = min(res, dist[v-1]+dist2[0]+r_cost);
adj[start].insert({ende, i});
adj[ende].erase({start, i});
}else{
long long a = min(vonn[0], vonn[ende] + cost + von0_r[start]);
long long b = min(von0[v-1], von0[ende] + cost + vonn_r[start]);
res = min(res, a + b + r_cost);
}
}
if(res!=1e17) cout << res;
else cout << -1;
return 0;
}
# | 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... |