#pragma GCC optimize("O3","unroll-loops","Ofast")
#include<iostream>
#include<vector>
#include<queue>
#include<set>
#define ll long long
using namespace std;
int n,m;
vector<bool> crit;
vector<pair<pair<int,int>,pair<ll,ll>>> edges;
void dijk(vector<vector<pair<pair<int,int>,pair<ll,ll>>>> &g,vector<ll> &dist,int s,int skip,bool flag){
dist[s]=0;
priority_queue<pair<int,ll>,vector<pair<int,ll>>,greater<pair<int,ll>>> pq;
vector<int> pre(n+1);
pq.push(make_pair(0,s));
while(!pq.empty()){
auto node=pq.top();
pq.pop();
if(dist[node.second]<node.first) continue;
for(auto x:g[node.second]){
if(x.first.second==skip) continue;
if(dist[x.first.first]>dist[node.second]+x.second.first){
if(flag) pre[x.first.first]=x.first.second;
dist[x.first.first]=dist[node.second]+x.second.first;
pq.push(make_pair(dist[x.first.first],x.first.first));
}
}
if(skip!=-1&&edges[skip].first.second==node.second){
if(dist[edges[skip].first.first]>dist[node.second]+edges[skip].second.first){
dist[edges[skip].first.first]=dist[node.second]+edges[skip].second.first;
pq.push(make_pair(dist[edges[skip].first.first],edges[skip].first.first));
}
}
}
if(flag){
for(int i=1;i<=n;i++){
if(i==s) continue;
crit[pre[i]]=1;
}
}
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
ll a,b,c,d;
cin>>n>>m;
crit.resize(m);
vector<vector<pair<pair<int,int>,pair<ll,ll>>>> graph(n+1),hparg(n+1);
for(int i=0;i<m;i++){
cin>>a>>b>>c>>d;
edges.push_back(make_pair(make_pair(a,b),make_pair(c,d)));
graph[a].push_back(make_pair(make_pair(b,i),make_pair(c,d)));
hparg[b].push_back(make_pair(make_pair(a,i),make_pair(c,d)));
}
vector<ll> from1(n+1,1e18),fromn(n+1,1e18),to1(n+1,1e18),ton(n+1,1e18);
dijk(graph,from1,1,-1,1);
dijk(graph,fromn,n,-1,1);
dijk(hparg,to1,1,-1,1);
dijk(hparg,ton,n,-1,1);
ll ans=from1[n]+fromn[1];
for(int i=0;i<m;i++){
if(crit[i]){
vector<ll> tmp1(n+1,1e18),tmp2(n+1,1e18);
dijk(graph,tmp1,1,i,0);
dijk(graph,tmp2,n,i,0);
ans=min(ans,tmp1[n]+tmp2[1]+edges[i].second.second);
}
else{
ans=min(ans,min(from1[n],from1[edges[i].first.second]+edges[i].second.first+ton[edges[i].first.first])+min(fromn[1],fromn[edges[i].first.second]+edges[i].second.first+to1[edges[i].first.first])+edges[i].second.second);
}
}
if(ans>=1e18) cout<<-1;
else cout<<ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
20 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
20 ms |
472 KB |
Output is correct |
4 |
Correct |
26 ms |
476 KB |
Output is correct |
5 |
Correct |
3 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
17 ms |
468 KB |
Output is correct |
11 |
Correct |
21 ms |
468 KB |
Output is correct |
12 |
Correct |
19 ms |
468 KB |
Output is correct |
13 |
Correct |
6 ms |
468 KB |
Output is correct |
14 |
Correct |
13 ms |
472 KB |
Output is correct |
15 |
Correct |
12 ms |
468 KB |
Output is correct |
16 |
Correct |
12 ms |
460 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
161 ms |
6488 KB |
Output is correct |
2 |
Correct |
160 ms |
6112 KB |
Output is correct |
3 |
Correct |
153 ms |
6648 KB |
Output is correct |
4 |
Correct |
21 ms |
468 KB |
Output is correct |
5 |
Correct |
9 ms |
456 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
63 ms |
6412 KB |
Output is correct |
10 |
Correct |
53 ms |
6244 KB |
Output is correct |
11 |
Correct |
113 ms |
6368 KB |
Output is correct |
12 |
Correct |
106 ms |
6376 KB |
Output is correct |
13 |
Correct |
102 ms |
6144 KB |
Output is correct |
14 |
Correct |
92 ms |
7084 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
468 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
127 ms |
5240 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
131 ms |
6136 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
320 KB |
Output is correct |
8 |
Correct |
50 ms |
6236 KB |
Output is correct |
9 |
Correct |
53 ms |
6152 KB |
Output is correct |
10 |
Correct |
92 ms |
5888 KB |
Output is correct |
11 |
Correct |
87 ms |
6020 KB |
Output is correct |
12 |
Correct |
94 ms |
6028 KB |
Output is correct |
13 |
Correct |
1 ms |
316 KB |
Output is correct |
14 |
Correct |
1 ms |
316 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
320 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
92 ms |
6268 KB |
Output is correct |
20 |
Correct |
86 ms |
5948 KB |
Output is correct |
21 |
Correct |
89 ms |
6292 KB |
Output is correct |
22 |
Correct |
94 ms |
5916 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
20 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
20 ms |
472 KB |
Output is correct |
4 |
Correct |
26 ms |
476 KB |
Output is correct |
5 |
Correct |
3 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
17 ms |
468 KB |
Output is correct |
11 |
Correct |
21 ms |
468 KB |
Output is correct |
12 |
Correct |
19 ms |
468 KB |
Output is correct |
13 |
Correct |
6 ms |
468 KB |
Output is correct |
14 |
Correct |
13 ms |
472 KB |
Output is correct |
15 |
Correct |
12 ms |
468 KB |
Output is correct |
16 |
Correct |
12 ms |
460 KB |
Output is correct |
17 |
Correct |
161 ms |
6488 KB |
Output is correct |
18 |
Correct |
160 ms |
6112 KB |
Output is correct |
19 |
Correct |
153 ms |
6648 KB |
Output is correct |
20 |
Correct |
21 ms |
468 KB |
Output is correct |
21 |
Correct |
9 ms |
456 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
212 KB |
Output is correct |
25 |
Correct |
63 ms |
6412 KB |
Output is correct |
26 |
Correct |
53 ms |
6244 KB |
Output is correct |
27 |
Correct |
113 ms |
6368 KB |
Output is correct |
28 |
Correct |
106 ms |
6376 KB |
Output is correct |
29 |
Correct |
102 ms |
6144 KB |
Output is correct |
30 |
Correct |
92 ms |
7084 KB |
Output is correct |
31 |
Correct |
14 ms |
468 KB |
Output is correct |
32 |
Correct |
1 ms |
340 KB |
Output is correct |
33 |
Correct |
127 ms |
5240 KB |
Output is correct |
34 |
Correct |
1 ms |
340 KB |
Output is correct |
35 |
Correct |
131 ms |
6136 KB |
Output is correct |
36 |
Correct |
1 ms |
212 KB |
Output is correct |
37 |
Correct |
1 ms |
320 KB |
Output is correct |
38 |
Correct |
50 ms |
6236 KB |
Output is correct |
39 |
Correct |
53 ms |
6152 KB |
Output is correct |
40 |
Correct |
92 ms |
5888 KB |
Output is correct |
41 |
Correct |
87 ms |
6020 KB |
Output is correct |
42 |
Correct |
94 ms |
6028 KB |
Output is correct |
43 |
Correct |
1 ms |
316 KB |
Output is correct |
44 |
Correct |
1 ms |
316 KB |
Output is correct |
45 |
Correct |
1 ms |
212 KB |
Output is correct |
46 |
Correct |
1 ms |
212 KB |
Output is correct |
47 |
Correct |
1 ms |
320 KB |
Output is correct |
48 |
Correct |
1 ms |
212 KB |
Output is correct |
49 |
Correct |
92 ms |
6268 KB |
Output is correct |
50 |
Correct |
86 ms |
5948 KB |
Output is correct |
51 |
Correct |
89 ms |
6292 KB |
Output is correct |
52 |
Correct |
94 ms |
5916 KB |
Output is correct |
53 |
Correct |
182 ms |
5904 KB |
Output is correct |
54 |
Correct |
180 ms |
6344 KB |
Output is correct |
55 |
Correct |
166 ms |
6104 KB |
Output is correct |
56 |
Correct |
24 ms |
516 KB |
Output is correct |
57 |
Correct |
23 ms |
400 KB |
Output is correct |
58 |
Correct |
77 ms |
5388 KB |
Output is correct |
59 |
Correct |
93 ms |
5384 KB |
Output is correct |
60 |
Correct |
123 ms |
5432 KB |
Output is correct |
61 |
Correct |
73 ms |
5388 KB |
Output is correct |
62 |
Correct |
87 ms |
5380 KB |
Output is correct |
63 |
Correct |
118 ms |
5376 KB |
Output is correct |
64 |
Correct |
573 ms |
5548 KB |
Output is correct |
65 |
Correct |
623 ms |
5388 KB |
Output is correct |
66 |
Correct |
799 ms |
5616 KB |
Output is correct |
67 |
Correct |
16 ms |
4348 KB |
Output is correct |
68 |
Correct |
57 ms |
6376 KB |
Output is correct |
69 |
Correct |
52 ms |
6476 KB |
Output is correct |
70 |
Correct |
112 ms |
6472 KB |
Output is correct |
71 |
Correct |
112 ms |
6460 KB |
Output is correct |
72 |
Correct |
117 ms |
6268 KB |
Output is correct |
73 |
Correct |
119 ms |
6396 KB |
Output is correct |
74 |
Correct |
111 ms |
6144 KB |
Output is correct |