#include <bits/stdc++.h>
using namespace std;
#define int long long
const int INF = 1e15;
struct edge{
int u,v,c,d;
};
edge edges[50001];
set<int> adj[201];
bool visited[50001];
int dist[201][201];
int n;
int dijkastra(){
priority_queue<pair<int,int>> q;
for(bool&i:visited)i=false;
int ans1 = INF;
q.emplace(0,1);
while(!q.empty()){
auto curr = q.top();q.pop();curr.first=-curr.first;
if(visited[curr.second])continue;
visited[curr.second]=true;
if(curr.second==n){ans1=curr.first;break;}
for(auto&i:adj[curr.second]){
if(!visited[edges[i].v])q.emplace(-curr.first-edges[i].c,edges[i].v);
}
}
while(!q.empty())q.pop();
for(bool&i:visited)i=false;
int ans2 = INF;
q.emplace(0,n);
while(!q.empty()){
auto curr = q.top();q.pop();curr.first=-curr.first;
if(visited[curr.second])continue;
visited[curr.second]=true;
if(curr.second==1){ans2=curr.first;break;}
for(auto&i:adj[curr.second]){
if(!visited[edges[i].v])q.emplace(-curr.first-edges[i].c,edges[i].v);
}
}
return ans1+ans2;
}
int32_t main(){
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int m;
cin >> n >> m;
for(auto&i:dist)for(int&j:i)j=INF;
for(int i=1;i<=m;i++){
int u,v,c,d;
cin >> u >> v >> c >> d;
edges[i] = {u,v,c,d};
adj[u].insert(i);
dist[u][v]=min(dist[u][v],c);
}
for(int i=1;i<=n;i++)dist[i][i]=0;
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 baseans = dist[1][n]+dist[n][1];
for(int i=1;i<=m;i++){
int to = min(dist[1][n],dist[1][edges[i].v]+dist[edges[i].u][n]+edges[i].c);
int back = min(dist[n][1],dist[n][edges[i].v]+dist[edges[i].u][1]+edges[i].c);
if(to+back+edges[i].d<baseans){
adj[edges[i].u].erase(i);
adj[edges[i].v].insert(i);
swap(edges[i].u,edges[i].v);
baseans = min(baseans,edges[i].d+dijkastra());
adj[edges[i].u].erase(i);
adj[edges[i].v].insert(i);
swap(edges[i].u,edges[i].v);
}
}
cout << (baseans>=INF ? -1 : baseans) << '\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
856 KB |
Output is correct |
2 |
Correct |
11 ms |
860 KB |
Output is correct |
3 |
Correct |
8 ms |
860 KB |
Output is correct |
4 |
Correct |
8 ms |
900 KB |
Output is correct |
5 |
Correct |
1 ms |
856 KB |
Output is correct |
6 |
Correct |
8 ms |
860 KB |
Output is correct |
7 |
Correct |
1 ms |
604 KB |
Output is correct |
8 |
Correct |
1 ms |
604 KB |
Output is correct |
9 |
Correct |
1 ms |
860 KB |
Output is correct |
10 |
Correct |
12 ms |
952 KB |
Output is correct |
11 |
Correct |
28 ms |
856 KB |
Output is correct |
12 |
Correct |
8 ms |
860 KB |
Output is correct |
13 |
Correct |
8 ms |
888 KB |
Output is correct |
14 |
Correct |
8 ms |
856 KB |
Output is correct |
15 |
Correct |
9 ms |
860 KB |
Output is correct |
16 |
Correct |
8 ms |
896 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
26 ms |
5464 KB |
Output is correct |
2 |
Correct |
39 ms |
6144 KB |
Output is correct |
3 |
Correct |
27 ms |
5212 KB |
Output is correct |
4 |
Correct |
11 ms |
860 KB |
Output is correct |
5 |
Correct |
8 ms |
856 KB |
Output is correct |
6 |
Correct |
8 ms |
860 KB |
Output is correct |
7 |
Correct |
8 ms |
604 KB |
Output is correct |
8 |
Correct |
0 ms |
604 KB |
Output is correct |
9 |
Correct |
67 ms |
6648 KB |
Output is correct |
10 |
Correct |
43 ms |
6320 KB |
Output is correct |
11 |
Correct |
39 ms |
6248 KB |
Output is correct |
12 |
Correct |
35 ms |
6624 KB |
Output is correct |
13 |
Correct |
32 ms |
6352 KB |
Output is correct |
14 |
Correct |
33 ms |
6416 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
860 KB |
Output is correct |
2 |
Correct |
8 ms |
604 KB |
Output is correct |
3 |
Correct |
22 ms |
4188 KB |
Output is correct |
4 |
Correct |
8 ms |
860 KB |
Output is correct |
5 |
Correct |
28 ms |
5312 KB |
Output is correct |
6 |
Correct |
1 ms |
604 KB |
Output is correct |
7 |
Correct |
1 ms |
600 KB |
Output is correct |
8 |
Correct |
76 ms |
5912 KB |
Output is correct |
9 |
Correct |
95 ms |
5920 KB |
Output is correct |
10 |
Correct |
31 ms |
5716 KB |
Output is correct |
11 |
Correct |
30 ms |
5720 KB |
Output is correct |
12 |
Correct |
42 ms |
5712 KB |
Output is correct |
13 |
Correct |
1 ms |
604 KB |
Output is correct |
14 |
Correct |
1 ms |
604 KB |
Output is correct |
15 |
Correct |
0 ms |
604 KB |
Output is correct |
16 |
Correct |
1 ms |
604 KB |
Output is correct |
17 |
Correct |
1 ms |
604 KB |
Output is correct |
18 |
Correct |
1 ms |
600 KB |
Output is correct |
19 |
Correct |
33 ms |
5972 KB |
Output is correct |
20 |
Correct |
36 ms |
6028 KB |
Output is correct |
21 |
Correct |
32 ms |
5972 KB |
Output is correct |
22 |
Correct |
37 ms |
5968 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
856 KB |
Output is correct |
2 |
Correct |
11 ms |
860 KB |
Output is correct |
3 |
Correct |
8 ms |
860 KB |
Output is correct |
4 |
Correct |
8 ms |
900 KB |
Output is correct |
5 |
Correct |
1 ms |
856 KB |
Output is correct |
6 |
Correct |
8 ms |
860 KB |
Output is correct |
7 |
Correct |
1 ms |
604 KB |
Output is correct |
8 |
Correct |
1 ms |
604 KB |
Output is correct |
9 |
Correct |
1 ms |
860 KB |
Output is correct |
10 |
Correct |
12 ms |
952 KB |
Output is correct |
11 |
Correct |
28 ms |
856 KB |
Output is correct |
12 |
Correct |
8 ms |
860 KB |
Output is correct |
13 |
Correct |
8 ms |
888 KB |
Output is correct |
14 |
Correct |
8 ms |
856 KB |
Output is correct |
15 |
Correct |
9 ms |
860 KB |
Output is correct |
16 |
Correct |
8 ms |
896 KB |
Output is correct |
17 |
Correct |
26 ms |
5464 KB |
Output is correct |
18 |
Correct |
39 ms |
6144 KB |
Output is correct |
19 |
Correct |
27 ms |
5212 KB |
Output is correct |
20 |
Correct |
11 ms |
860 KB |
Output is correct |
21 |
Correct |
8 ms |
856 KB |
Output is correct |
22 |
Correct |
8 ms |
860 KB |
Output is correct |
23 |
Correct |
8 ms |
604 KB |
Output is correct |
24 |
Correct |
0 ms |
604 KB |
Output is correct |
25 |
Correct |
67 ms |
6648 KB |
Output is correct |
26 |
Correct |
43 ms |
6320 KB |
Output is correct |
27 |
Correct |
39 ms |
6248 KB |
Output is correct |
28 |
Correct |
35 ms |
6624 KB |
Output is correct |
29 |
Correct |
32 ms |
6352 KB |
Output is correct |
30 |
Correct |
33 ms |
6416 KB |
Output is correct |
31 |
Correct |
11 ms |
860 KB |
Output is correct |
32 |
Correct |
8 ms |
604 KB |
Output is correct |
33 |
Correct |
22 ms |
4188 KB |
Output is correct |
34 |
Correct |
8 ms |
860 KB |
Output is correct |
35 |
Correct |
28 ms |
5312 KB |
Output is correct |
36 |
Correct |
1 ms |
604 KB |
Output is correct |
37 |
Correct |
1 ms |
600 KB |
Output is correct |
38 |
Correct |
76 ms |
5912 KB |
Output is correct |
39 |
Correct |
95 ms |
5920 KB |
Output is correct |
40 |
Correct |
31 ms |
5716 KB |
Output is correct |
41 |
Correct |
30 ms |
5720 KB |
Output is correct |
42 |
Correct |
42 ms |
5712 KB |
Output is correct |
43 |
Correct |
1 ms |
604 KB |
Output is correct |
44 |
Correct |
1 ms |
604 KB |
Output is correct |
45 |
Correct |
0 ms |
604 KB |
Output is correct |
46 |
Correct |
1 ms |
604 KB |
Output is correct |
47 |
Correct |
1 ms |
604 KB |
Output is correct |
48 |
Correct |
1 ms |
600 KB |
Output is correct |
49 |
Correct |
33 ms |
5972 KB |
Output is correct |
50 |
Correct |
36 ms |
6028 KB |
Output is correct |
51 |
Correct |
32 ms |
5972 KB |
Output is correct |
52 |
Correct |
37 ms |
5968 KB |
Output is correct |
53 |
Correct |
29 ms |
5468 KB |
Output is correct |
54 |
Correct |
38 ms |
5468 KB |
Output is correct |
55 |
Correct |
32 ms |
5460 KB |
Output is correct |
56 |
Correct |
11 ms |
860 KB |
Output is correct |
57 |
Correct |
8 ms |
860 KB |
Output is correct |
58 |
Correct |
46 ms |
5468 KB |
Output is correct |
59 |
Correct |
49 ms |
5544 KB |
Output is correct |
60 |
Correct |
59 ms |
5248 KB |
Output is correct |
61 |
Correct |
58 ms |
5552 KB |
Output is correct |
62 |
Correct |
404 ms |
5536 KB |
Output is correct |
63 |
Correct |
936 ms |
5492 KB |
Output is correct |
64 |
Correct |
140 ms |
5720 KB |
Output is correct |
65 |
Correct |
351 ms |
5884 KB |
Output is correct |
66 |
Correct |
692 ms |
5980 KB |
Output is correct |
67 |
Correct |
35 ms |
6336 KB |
Output is correct |
68 |
Correct |
65 ms |
6272 KB |
Output is correct |
69 |
Correct |
48 ms |
6308 KB |
Output is correct |
70 |
Correct |
34 ms |
6352 KB |
Output is correct |
71 |
Correct |
45 ms |
6532 KB |
Output is correct |
72 |
Correct |
35 ms |
6228 KB |
Output is correct |
73 |
Correct |
36 ms |
6656 KB |
Output is correct |
74 |
Correct |
51 ms |
6636 KB |
Output is correct |