#include <bits/stdc++.h>
using namespace std;
#define int long long
#define MAXN 205
#define MAXM 50005
int n,m;
int dist[MAXN][MAXN];
array<int,4> edg[MAXM];
vector<pair<int,int> > adj[MAXN];
int EDGST;
int dijkstra(int st, int rev){
int dis[MAXN];
for(int i=0; i<MAXN; i++) dis[i] = 1e17;
dis[st] = 0;
priority_queue<pair<int,int>,vector<pair<int,int> >,greater<pair<int,int> > > pq;//w,nd
pq.push({0,st});
while(!pq.empty()){
int wei = pq.top().first, v=pq.top().second; pq.pop();
if(dis[v] != wei) continue;
for(auto ahc : adj[v]){
if(ahc.second==rev) continue;
int nd = ahc.first; int nw = edg[ahc.second][2];
if(wei + nw < dis[nd]){
dis[nd] = wei+nw;
pq.push({dis[nd],nd});
}
}
if(v==EDGST){
int nd = edg[rev][0]; int nw = edg[rev][2];
if(wei + nw < dis[nd]){
dis[nd] = wei+nw;
pq.push({dis[nd],nd});
}
}
}
if(st==n) return dis[1];
else return dis[n];
}
int32_t main(){
for(int i=0; i<MAXN; i++){
for(int j=0; j<MAXN; j++){
if(i==j) dist[i][j]=0;
else dist[i][j]=1e17;
}
}
cin>>n>>m;
for(int i=0; i<m; i++){
int u,v,c,d;
cin>>u>>v>>c>>d;
edg[i] = {u,v,c,d};
adj[u].push_back(make_pair(v,i));
dist[u][v]=min(dist[u][v],c);
}
for(int k=1; k<=n; k++){
for(int i=1; i<=n; i++){
for(int j=1; j<=n; j++){
dist[i][j] = min(dist[i][j], dist[i][k]+dist[k][j]);
}
}
}
int an = dist[1][n] + dist[n][1];
for(int i=0; i<m; i++){
//can lower?
int dis_to = min(dist[1][n], dist[1][edg[i][1]] + edg[i][2] + dist[edg[i][0]][n]);
int dis_fr = min(dist[n][1], dist[n][edg[i][1]] + edg[i][2] + dist[edg[i][0]][1]);
if(dis_to + dis_fr + edg[i][3] < an){
EDGST = edg[i][1];
int dis1 = dijkstra(1,i);
int dis2 = dijkstra(n,i);
//if(i==1) cout<<dis1<<' '<<dis2<<endl;
an = min(an, dis1+dis2+edg[i][3]);
}
}
if(an < 1e15) cout<<an;
else cout<<-1;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
768 KB |
Output is correct |
2 |
Correct |
14 ms |
640 KB |
Output is correct |
3 |
Correct |
15 ms |
768 KB |
Output is correct |
4 |
Correct |
15 ms |
768 KB |
Output is correct |
5 |
Correct |
7 ms |
768 KB |
Output is correct |
6 |
Correct |
13 ms |
640 KB |
Output is correct |
7 |
Correct |
5 ms |
640 KB |
Output is correct |
8 |
Correct |
5 ms |
640 KB |
Output is correct |
9 |
Correct |
6 ms |
768 KB |
Output is correct |
10 |
Correct |
18 ms |
768 KB |
Output is correct |
11 |
Correct |
29 ms |
888 KB |
Output is correct |
12 |
Correct |
15 ms |
768 KB |
Output is correct |
13 |
Correct |
15 ms |
768 KB |
Output is correct |
14 |
Correct |
15 ms |
768 KB |
Output is correct |
15 |
Correct |
15 ms |
768 KB |
Output is correct |
16 |
Correct |
15 ms |
768 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
91 ms |
4472 KB |
Output is correct |
2 |
Correct |
94 ms |
4528 KB |
Output is correct |
3 |
Correct |
103 ms |
4600 KB |
Output is correct |
4 |
Correct |
17 ms |
896 KB |
Output is correct |
5 |
Correct |
15 ms |
768 KB |
Output is correct |
6 |
Correct |
14 ms |
640 KB |
Output is correct |
7 |
Correct |
14 ms |
640 KB |
Output is correct |
8 |
Correct |
5 ms |
640 KB |
Output is correct |
9 |
Correct |
102 ms |
4600 KB |
Output is correct |
10 |
Correct |
99 ms |
4600 KB |
Output is correct |
11 |
Correct |
98 ms |
4600 KB |
Output is correct |
12 |
Correct |
104 ms |
4728 KB |
Output is correct |
13 |
Correct |
105 ms |
4728 KB |
Output is correct |
14 |
Correct |
97 ms |
4856 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
768 KB |
Output is correct |
2 |
Correct |
13 ms |
640 KB |
Output is correct |
3 |
Correct |
68 ms |
3424 KB |
Output is correct |
4 |
Correct |
17 ms |
768 KB |
Output is correct |
5 |
Correct |
85 ms |
4344 KB |
Output is correct |
6 |
Correct |
5 ms |
640 KB |
Output is correct |
7 |
Correct |
5 ms |
640 KB |
Output is correct |
8 |
Correct |
87 ms |
4344 KB |
Output is correct |
9 |
Correct |
89 ms |
4344 KB |
Output is correct |
10 |
Correct |
89 ms |
4592 KB |
Output is correct |
11 |
Correct |
88 ms |
4472 KB |
Output is correct |
12 |
Correct |
86 ms |
4472 KB |
Output is correct |
13 |
Correct |
5 ms |
640 KB |
Output is correct |
14 |
Correct |
5 ms |
640 KB |
Output is correct |
15 |
Correct |
5 ms |
640 KB |
Output is correct |
16 |
Correct |
5 ms |
640 KB |
Output is correct |
17 |
Correct |
5 ms |
640 KB |
Output is correct |
18 |
Correct |
5 ms |
640 KB |
Output is correct |
19 |
Correct |
84 ms |
4472 KB |
Output is correct |
20 |
Correct |
85 ms |
4472 KB |
Output is correct |
21 |
Correct |
84 ms |
4344 KB |
Output is correct |
22 |
Correct |
84 ms |
4344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
768 KB |
Output is correct |
2 |
Correct |
14 ms |
640 KB |
Output is correct |
3 |
Correct |
15 ms |
768 KB |
Output is correct |
4 |
Correct |
15 ms |
768 KB |
Output is correct |
5 |
Correct |
7 ms |
768 KB |
Output is correct |
6 |
Correct |
13 ms |
640 KB |
Output is correct |
7 |
Correct |
5 ms |
640 KB |
Output is correct |
8 |
Correct |
5 ms |
640 KB |
Output is correct |
9 |
Correct |
6 ms |
768 KB |
Output is correct |
10 |
Correct |
18 ms |
768 KB |
Output is correct |
11 |
Correct |
29 ms |
888 KB |
Output is correct |
12 |
Correct |
15 ms |
768 KB |
Output is correct |
13 |
Correct |
15 ms |
768 KB |
Output is correct |
14 |
Correct |
15 ms |
768 KB |
Output is correct |
15 |
Correct |
15 ms |
768 KB |
Output is correct |
16 |
Correct |
15 ms |
768 KB |
Output is correct |
17 |
Correct |
91 ms |
4472 KB |
Output is correct |
18 |
Correct |
94 ms |
4528 KB |
Output is correct |
19 |
Correct |
103 ms |
4600 KB |
Output is correct |
20 |
Correct |
17 ms |
896 KB |
Output is correct |
21 |
Correct |
15 ms |
768 KB |
Output is correct |
22 |
Correct |
14 ms |
640 KB |
Output is correct |
23 |
Correct |
14 ms |
640 KB |
Output is correct |
24 |
Correct |
5 ms |
640 KB |
Output is correct |
25 |
Correct |
102 ms |
4600 KB |
Output is correct |
26 |
Correct |
99 ms |
4600 KB |
Output is correct |
27 |
Correct |
98 ms |
4600 KB |
Output is correct |
28 |
Correct |
104 ms |
4728 KB |
Output is correct |
29 |
Correct |
105 ms |
4728 KB |
Output is correct |
30 |
Correct |
97 ms |
4856 KB |
Output is correct |
31 |
Correct |
15 ms |
768 KB |
Output is correct |
32 |
Correct |
13 ms |
640 KB |
Output is correct |
33 |
Correct |
68 ms |
3424 KB |
Output is correct |
34 |
Correct |
17 ms |
768 KB |
Output is correct |
35 |
Correct |
85 ms |
4344 KB |
Output is correct |
36 |
Correct |
5 ms |
640 KB |
Output is correct |
37 |
Correct |
5 ms |
640 KB |
Output is correct |
38 |
Correct |
87 ms |
4344 KB |
Output is correct |
39 |
Correct |
89 ms |
4344 KB |
Output is correct |
40 |
Correct |
89 ms |
4592 KB |
Output is correct |
41 |
Correct |
88 ms |
4472 KB |
Output is correct |
42 |
Correct |
86 ms |
4472 KB |
Output is correct |
43 |
Correct |
5 ms |
640 KB |
Output is correct |
44 |
Correct |
5 ms |
640 KB |
Output is correct |
45 |
Correct |
5 ms |
640 KB |
Output is correct |
46 |
Correct |
5 ms |
640 KB |
Output is correct |
47 |
Correct |
5 ms |
640 KB |
Output is correct |
48 |
Correct |
5 ms |
640 KB |
Output is correct |
49 |
Correct |
84 ms |
4472 KB |
Output is correct |
50 |
Correct |
85 ms |
4472 KB |
Output is correct |
51 |
Correct |
84 ms |
4344 KB |
Output is correct |
52 |
Correct |
84 ms |
4344 KB |
Output is correct |
53 |
Correct |
92 ms |
4344 KB |
Output is correct |
54 |
Correct |
93 ms |
4600 KB |
Output is correct |
55 |
Correct |
87 ms |
4472 KB |
Output is correct |
56 |
Correct |
15 ms |
768 KB |
Output is correct |
57 |
Correct |
15 ms |
768 KB |
Output is correct |
58 |
Correct |
78 ms |
3704 KB |
Output is correct |
59 |
Correct |
75 ms |
3580 KB |
Output is correct |
60 |
Correct |
85 ms |
3704 KB |
Output is correct |
61 |
Correct |
75 ms |
3704 KB |
Output is correct |
62 |
Correct |
103 ms |
3696 KB |
Output is correct |
63 |
Correct |
156 ms |
3580 KB |
Output is correct |
64 |
Correct |
130 ms |
4164 KB |
Output is correct |
65 |
Correct |
259 ms |
4196 KB |
Output is correct |
66 |
Correct |
419 ms |
4220 KB |
Output is correct |
67 |
Correct |
71 ms |
3696 KB |
Output is correct |
68 |
Correct |
100 ms |
4600 KB |
Output is correct |
69 |
Correct |
102 ms |
4600 KB |
Output is correct |
70 |
Correct |
98 ms |
4728 KB |
Output is correct |
71 |
Correct |
97 ms |
4588 KB |
Output is correct |
72 |
Correct |
100 ms |
4728 KB |
Output is correct |
73 |
Correct |
99 ms |
4728 KB |
Output is correct |
74 |
Correct |
101 ms |
4600 KB |
Output is correct |