#include <bits/stdc++.h>
#ifdef NON_SUBMIT
#define TEST(n) (n)
#define tout cerr
#else
#define TEST(n) ((void)0)
#define tout cin
#endif
using namespace std;
vector<tuple<int,int,int>> adj[200], radj[200];
vector<tuple<int,int,int,int,int>> E;
int N, P[200];
long long dist1[200], dist2[200], rdist1[200], rdist2[200], dist[200];
bool MP[2][50000];
void dijkstra(int c, long long *dist, vector<tuple<int,int,int>> *adj)
{
priority_queue<pair<long long,int>> Q;
for(int i=0;i<N;i++) {
dist[i]=(i!=c)*0x1f1f1f1f1f1f1f1fLL;
P[i]=-1;
}
Q.emplace(0,c);
while(!Q.empty()) {
auto[t,c]=Q.top();
Q.pop();
if(-t!=dist[c]) continue;
for(auto[n,w,i]: adj[c]) if(dist[n]>dist[c]+w) {
dist[n]=dist[c]+w;
P[n]=i;
Q.emplace(-dist[n],n);
}
}
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(NULL); cout.tie(NULL);
TEST(freopen("input.txt","r",stdin));
TEST(freopen("output.txt","w",stdout));
TEST(freopen("debug.txt","w",stderr));
int M;
long long ans;
priority_queue<pair<long long,int>> Q;
cin>>N>>M;
E.resize(M);
for(int i=0;i<M;i++) {
auto &[u,v,w,x,idx]=E[i];
cin>>u>>v>>x>>w; idx=i;
adj[--u].emplace_back(--v,x,i);
radj[v].emplace_back(u,x,i);
}
dijkstra(N-1,dist2,radj); dijkstra(0,dist1,adj);
for(int c=0;c<N;c++) if(P[c]>-1) MP[0][P[c]]=true;
dijkstra(0,rdist2,radj); dijkstra(N-1,rdist1,adj);
for(int c=0;c<N;c++) if(P[c]>-1) MP[1][P[c]]=true;
ans=dist1[N-1]+rdist1[0];
for(auto[u,v,w,x,i]: E) {
long long res=w;
if(MP[0][i]) {
for(int i=0;i<N;i++) dist[i]=(i!=0)*0x1f1f1f1f1f1f1f1fLL;
Q.emplace(0,0);
while(!Q.empty()) {
auto[t,c]=Q.top();
Q.pop();
if(-t!=dist[c]) continue;
if(c==v && dist[u]>dist[v]+x) {
dist[u]=dist[v]+x;
Q.emplace(-dist[u],u);
}
for(auto[n,w,j]: adj[c]) if(i!=j && dist[n]>dist[c]+w) {
dist[n]=dist[c]+w;
Q.emplace(-dist[n],n);
}
}
res+=dist[N-1];
}
else res+=min(dist1[N-1],dist1[v]+dist2[u]+x);
if(MP[1][i]) {
for(int i=0;i<N;i++) dist[i]=(i!=N-1)*0x1f1f1f1f1f1f1f1fLL;
Q.emplace(0,N-1);
while(!Q.empty()) {
auto[t,c]=Q.top();
Q.pop();
if(-t!=dist[c]) continue;
if(c==v && dist[u]>dist[v]+x) {
dist[u]=dist[v]+x;
Q.emplace(-dist[u],u);
}
for(auto[n,w,j]: adj[c]) if(i!=j && dist[n]>dist[c]+w) {
dist[n]=dist[c]+w;
Q.emplace(-dist[n],n);
}
}
res+=dist[0];
}
else res+=min(rdist1[0],rdist1[v]+rdist2[u]+x);
ans=min(ans,res);
}
cout<<(ans<0x1f1f1f1f1f1f1f1fLL ? ans:-1)<<'\n';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
376 KB |
Output is correct |
2 |
Correct |
5 ms |
376 KB |
Output is correct |
3 |
Correct |
16 ms |
376 KB |
Output is correct |
4 |
Correct |
16 ms |
376 KB |
Output is correct |
5 |
Correct |
6 ms |
504 KB |
Output is correct |
6 |
Correct |
5 ms |
376 KB |
Output is correct |
7 |
Correct |
5 ms |
376 KB |
Output is correct |
8 |
Correct |
5 ms |
376 KB |
Output is correct |
9 |
Correct |
5 ms |
504 KB |
Output is correct |
10 |
Correct |
22 ms |
504 KB |
Output is correct |
11 |
Correct |
19 ms |
248 KB |
Output is correct |
12 |
Correct |
20 ms |
376 KB |
Output is correct |
13 |
Correct |
7 ms |
504 KB |
Output is correct |
14 |
Correct |
11 ms |
376 KB |
Output is correct |
15 |
Correct |
10 ms |
380 KB |
Output is correct |
16 |
Correct |
14 ms |
376 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
88 ms |
3320 KB |
Output is correct |
2 |
Correct |
86 ms |
3448 KB |
Output is correct |
3 |
Correct |
91 ms |
3372 KB |
Output is correct |
4 |
Correct |
14 ms |
504 KB |
Output is correct |
5 |
Correct |
9 ms |
376 KB |
Output is correct |
6 |
Correct |
5 ms |
376 KB |
Output is correct |
7 |
Correct |
5 ms |
376 KB |
Output is correct |
8 |
Correct |
5 ms |
376 KB |
Output is correct |
9 |
Correct |
42 ms |
3320 KB |
Output is correct |
10 |
Correct |
43 ms |
3320 KB |
Output is correct |
11 |
Correct |
69 ms |
3448 KB |
Output is correct |
12 |
Correct |
67 ms |
3320 KB |
Output is correct |
13 |
Correct |
65 ms |
3320 KB |
Output is correct |
14 |
Correct |
61 ms |
3576 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
376 KB |
Output is correct |
2 |
Correct |
5 ms |
376 KB |
Output is correct |
3 |
Correct |
53 ms |
2552 KB |
Output is correct |
4 |
Correct |
5 ms |
380 KB |
Output is correct |
5 |
Correct |
59 ms |
3320 KB |
Output is correct |
6 |
Correct |
5 ms |
376 KB |
Output is correct |
7 |
Correct |
5 ms |
376 KB |
Output is correct |
8 |
Correct |
34 ms |
3320 KB |
Output is correct |
9 |
Correct |
36 ms |
3320 KB |
Output is correct |
10 |
Correct |
54 ms |
3324 KB |
Output is correct |
11 |
Correct |
47 ms |
3320 KB |
Output is correct |
12 |
Correct |
48 ms |
3448 KB |
Output is correct |
13 |
Correct |
5 ms |
376 KB |
Output is correct |
14 |
Correct |
5 ms |
376 KB |
Output is correct |
15 |
Correct |
5 ms |
376 KB |
Output is correct |
16 |
Correct |
5 ms |
376 KB |
Output is correct |
17 |
Correct |
5 ms |
376 KB |
Output is correct |
18 |
Correct |
5 ms |
376 KB |
Output is correct |
19 |
Correct |
49 ms |
3576 KB |
Output is correct |
20 |
Correct |
49 ms |
3320 KB |
Output is correct |
21 |
Correct |
48 ms |
3320 KB |
Output is correct |
22 |
Correct |
50 ms |
3320 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
376 KB |
Output is correct |
2 |
Correct |
5 ms |
376 KB |
Output is correct |
3 |
Correct |
16 ms |
376 KB |
Output is correct |
4 |
Correct |
16 ms |
376 KB |
Output is correct |
5 |
Correct |
6 ms |
504 KB |
Output is correct |
6 |
Correct |
5 ms |
376 KB |
Output is correct |
7 |
Correct |
5 ms |
376 KB |
Output is correct |
8 |
Correct |
5 ms |
376 KB |
Output is correct |
9 |
Correct |
5 ms |
504 KB |
Output is correct |
10 |
Correct |
22 ms |
504 KB |
Output is correct |
11 |
Correct |
19 ms |
248 KB |
Output is correct |
12 |
Correct |
20 ms |
376 KB |
Output is correct |
13 |
Correct |
7 ms |
504 KB |
Output is correct |
14 |
Correct |
11 ms |
376 KB |
Output is correct |
15 |
Correct |
10 ms |
380 KB |
Output is correct |
16 |
Correct |
14 ms |
376 KB |
Output is correct |
17 |
Correct |
88 ms |
3320 KB |
Output is correct |
18 |
Correct |
86 ms |
3448 KB |
Output is correct |
19 |
Correct |
91 ms |
3372 KB |
Output is correct |
20 |
Correct |
14 ms |
504 KB |
Output is correct |
21 |
Correct |
9 ms |
376 KB |
Output is correct |
22 |
Correct |
5 ms |
376 KB |
Output is correct |
23 |
Correct |
5 ms |
376 KB |
Output is correct |
24 |
Correct |
5 ms |
376 KB |
Output is correct |
25 |
Correct |
42 ms |
3320 KB |
Output is correct |
26 |
Correct |
43 ms |
3320 KB |
Output is correct |
27 |
Correct |
69 ms |
3448 KB |
Output is correct |
28 |
Correct |
67 ms |
3320 KB |
Output is correct |
29 |
Correct |
65 ms |
3320 KB |
Output is correct |
30 |
Correct |
61 ms |
3576 KB |
Output is correct |
31 |
Correct |
13 ms |
376 KB |
Output is correct |
32 |
Correct |
5 ms |
376 KB |
Output is correct |
33 |
Correct |
53 ms |
2552 KB |
Output is correct |
34 |
Correct |
5 ms |
380 KB |
Output is correct |
35 |
Correct |
59 ms |
3320 KB |
Output is correct |
36 |
Correct |
5 ms |
376 KB |
Output is correct |
37 |
Correct |
5 ms |
376 KB |
Output is correct |
38 |
Correct |
34 ms |
3320 KB |
Output is correct |
39 |
Correct |
36 ms |
3320 KB |
Output is correct |
40 |
Correct |
54 ms |
3324 KB |
Output is correct |
41 |
Correct |
47 ms |
3320 KB |
Output is correct |
42 |
Correct |
48 ms |
3448 KB |
Output is correct |
43 |
Correct |
5 ms |
376 KB |
Output is correct |
44 |
Correct |
5 ms |
376 KB |
Output is correct |
45 |
Correct |
5 ms |
376 KB |
Output is correct |
46 |
Correct |
5 ms |
376 KB |
Output is correct |
47 |
Correct |
5 ms |
376 KB |
Output is correct |
48 |
Correct |
5 ms |
376 KB |
Output is correct |
49 |
Correct |
49 ms |
3576 KB |
Output is correct |
50 |
Correct |
49 ms |
3320 KB |
Output is correct |
51 |
Correct |
48 ms |
3320 KB |
Output is correct |
52 |
Correct |
50 ms |
3320 KB |
Output is correct |
53 |
Correct |
104 ms |
3320 KB |
Output is correct |
54 |
Correct |
100 ms |
3448 KB |
Output is correct |
55 |
Correct |
98 ms |
3320 KB |
Output is correct |
56 |
Correct |
16 ms |
376 KB |
Output is correct |
57 |
Correct |
21 ms |
376 KB |
Output is correct |
58 |
Correct |
87 ms |
2552 KB |
Output is correct |
59 |
Correct |
90 ms |
2680 KB |
Output is correct |
60 |
Correct |
94 ms |
2556 KB |
Output is correct |
61 |
Correct |
82 ms |
2552 KB |
Output is correct |
62 |
Correct |
91 ms |
2552 KB |
Output is correct |
63 |
Correct |
92 ms |
2604 KB |
Output is correct |
64 |
Correct |
646 ms |
3180 KB |
Output is correct |
65 |
Correct |
605 ms |
3272 KB |
Output is correct |
66 |
Correct |
564 ms |
3280 KB |
Output is correct |
67 |
Correct |
23 ms |
2612 KB |
Output is correct |
68 |
Correct |
43 ms |
3324 KB |
Output is correct |
69 |
Correct |
42 ms |
3320 KB |
Output is correct |
70 |
Correct |
73 ms |
3324 KB |
Output is correct |
71 |
Correct |
72 ms |
3324 KB |
Output is correct |
72 |
Correct |
73 ms |
3320 KB |
Output is correct |
73 |
Correct |
76 ms |
3576 KB |
Output is correct |
74 |
Correct |
70 ms |
3448 KB |
Output is correct |