#include<bits/stdc++.h>
#define int long long
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<int,int>, vector<pair<int,int>>, greater<pair<int,int>>> pq;
dist[start] = 0;
pq.push({0,start});
while(!pq.empty()){
int 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);
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;
}
int 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{
int a = min(vonn[0], vonn[ende] + cost + von0_r[start]);
int 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;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
468 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
2 ms |
468 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
284 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
17 ms |
468 KB |
Output is correct |
11 |
Correct |
22 ms |
536 KB |
Output is correct |
12 |
Correct |
23 ms |
524 KB |
Output is correct |
13 |
Correct |
1 ms |
468 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Correct |
1 ms |
468 KB |
Output is correct |
16 |
Correct |
1 ms |
468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
55 ms |
8960 KB |
Output is correct |
2 |
Correct |
51 ms |
8940 KB |
Output is correct |
3 |
Correct |
49 ms |
8972 KB |
Output is correct |
4 |
Correct |
2 ms |
596 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
34 ms |
8976 KB |
Output is correct |
10 |
Correct |
39 ms |
8940 KB |
Output is correct |
11 |
Correct |
61 ms |
8904 KB |
Output is correct |
12 |
Correct |
46 ms |
8916 KB |
Output is correct |
13 |
Correct |
42 ms |
8924 KB |
Output is correct |
14 |
Correct |
44 ms |
8980 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
41 ms |
6976 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
69 ms |
9000 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
46 ms |
8956 KB |
Output is correct |
9 |
Correct |
35 ms |
8884 KB |
Output is correct |
10 |
Correct |
40 ms |
8884 KB |
Output is correct |
11 |
Correct |
40 ms |
8904 KB |
Output is correct |
12 |
Correct |
41 ms |
8880 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
47 ms |
8908 KB |
Output is correct |
20 |
Correct |
42 ms |
8908 KB |
Output is correct |
21 |
Correct |
50 ms |
8872 KB |
Output is correct |
22 |
Correct |
41 ms |
8892 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
468 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
2 ms |
468 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
284 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
17 ms |
468 KB |
Output is correct |
11 |
Correct |
22 ms |
536 KB |
Output is correct |
12 |
Correct |
23 ms |
524 KB |
Output is correct |
13 |
Correct |
1 ms |
468 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Correct |
1 ms |
468 KB |
Output is correct |
16 |
Correct |
1 ms |
468 KB |
Output is correct |
17 |
Correct |
55 ms |
8960 KB |
Output is correct |
18 |
Correct |
51 ms |
8940 KB |
Output is correct |
19 |
Correct |
49 ms |
8972 KB |
Output is correct |
20 |
Correct |
2 ms |
596 KB |
Output is correct |
21 |
Correct |
1 ms |
468 KB |
Output is correct |
22 |
Correct |
1 ms |
468 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
0 ms |
212 KB |
Output is correct |
25 |
Correct |
34 ms |
8976 KB |
Output is correct |
26 |
Correct |
39 ms |
8940 KB |
Output is correct |
27 |
Correct |
61 ms |
8904 KB |
Output is correct |
28 |
Correct |
46 ms |
8916 KB |
Output is correct |
29 |
Correct |
42 ms |
8924 KB |
Output is correct |
30 |
Correct |
44 ms |
8980 KB |
Output is correct |
31 |
Correct |
1 ms |
468 KB |
Output is correct |
32 |
Correct |
1 ms |
340 KB |
Output is correct |
33 |
Correct |
41 ms |
6976 KB |
Output is correct |
34 |
Correct |
1 ms |
340 KB |
Output is correct |
35 |
Correct |
69 ms |
9000 KB |
Output is correct |
36 |
Correct |
0 ms |
212 KB |
Output is correct |
37 |
Correct |
0 ms |
212 KB |
Output is correct |
38 |
Correct |
46 ms |
8956 KB |
Output is correct |
39 |
Correct |
35 ms |
8884 KB |
Output is correct |
40 |
Correct |
40 ms |
8884 KB |
Output is correct |
41 |
Correct |
40 ms |
8904 KB |
Output is correct |
42 |
Correct |
41 ms |
8880 KB |
Output is correct |
43 |
Correct |
0 ms |
212 KB |
Output is correct |
44 |
Correct |
0 ms |
212 KB |
Output is correct |
45 |
Correct |
1 ms |
212 KB |
Output is correct |
46 |
Correct |
1 ms |
212 KB |
Output is correct |
47 |
Correct |
0 ms |
212 KB |
Output is correct |
48 |
Correct |
0 ms |
212 KB |
Output is correct |
49 |
Correct |
47 ms |
8908 KB |
Output is correct |
50 |
Correct |
42 ms |
8908 KB |
Output is correct |
51 |
Correct |
50 ms |
8872 KB |
Output is correct |
52 |
Correct |
41 ms |
8892 KB |
Output is correct |
53 |
Correct |
59 ms |
8912 KB |
Output is correct |
54 |
Correct |
80 ms |
9000 KB |
Output is correct |
55 |
Correct |
64 ms |
9000 KB |
Output is correct |
56 |
Correct |
2 ms |
468 KB |
Output is correct |
57 |
Correct |
1 ms |
468 KB |
Output is correct |
58 |
Correct |
448 ms |
7232 KB |
Output is correct |
59 |
Correct |
462 ms |
7284 KB |
Output is correct |
60 |
Correct |
649 ms |
7244 KB |
Output is correct |
61 |
Correct |
429 ms |
7376 KB |
Output is correct |
62 |
Correct |
459 ms |
7288 KB |
Output is correct |
63 |
Correct |
629 ms |
7280 KB |
Output is correct |
64 |
Correct |
945 ms |
7776 KB |
Output is correct |
65 |
Execution timed out |
1039 ms |
8756 KB |
Time limit exceeded |
66 |
Halted |
0 ms |
0 KB |
- |