#include <bits/stdc++.h>
#define N 205
#define M 50005
#define int long long
using namespace std;
struct edge{
int u,v,c,d,i;
};
vector<edge> edges(M);
vector<edge> adj[N];
int dp[N][N],dp2[N][N],dp3[N][N];
vector<pair<int,int>> dijkstra(int st,int nd){
vector<int> dist(N,1e18);
vector<pair<int,int>> par(N);
dist[st] = 0;
priority_queue<pair<int,int>> q;
q.push({0,st});
while(q.size()){
auto tp = q.top();
q.pop();
tp.first *=-1;
if(tp.second == nd){
vector<pair<int,int>> path;
int now = nd;
path.push_back({nd,0});
while(now != st){
path.push_back(par[now]);
now = par[now].first;
}
reverse(path.begin(),path.end());
return path;
}
if(tp.first != dist[tp.second])continue;
for(auto u:adj[tp.second]){
if(dist[u.v] > dist[u.u] + u.c){
dist[u.v] = dist[u.u] + u.c;
par[u.v] = {u.u,u.i};
q.push({-dist[u.v],u.v});
}
}
}
return {};
}
void solve(){
int n,m;
cin >> n >> m;
for(int i=1;i<=m;i++){
int u,v,c,d;
cin >> u >> v >> c >> d;
adj[u].push_back({u,v,c,d,i});
edges[i] = {u,v,c,d};
}
auto path1 = dijkstra(1,n);
auto path2 = dijkstra(n,1);
map<int,int> mp1,mp2;
for(int i = 0;i<(int)path1.size()-1;i++){
mp1[path1[i].second] = i+1;
}
for(int i = 0;i<(int)path2.size()-1;i++){
mp2[path2[i].second] = i+1;
}
for(int i=0;i<N;i++){
for(int j=0;j<N;j++){
dp[i][j] = dp2[i][j] = dp3[i][j] = 1e18;
}
dp[i][i] = dp2[i][i] = dp3[i][i] = 0;
}
for(int i=1;i<=m;i++){
dp[edges[i].u][edges[i].v] = min(dp[edges[i].u][edges[i].v],edges[i].c);
if(!mp1[i]){
dp2[edges[i].u][edges[i].v] = min(dp2[edges[i].u][edges[i].v],edges[i].c);
}
if(!mp2[i]){
dp3[edges[i].u][edges[i].v] = min(dp3[edges[i].u][edges[i].v],edges[i].c);
}
}
for(int x=1;x<=n;x++){
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
dp[i][j] = min(dp[i][j],dp[i][x] + dp[x][j]);
dp2[i][j] = min(dp2[i][j],dp2[i][x] + dp2[x][j]);
dp3[i][j] = min(dp3[i][j],dp3[i][x] + dp3[x][j]);
}
}
}
int ans = 1e18;
ans = min(ans,dp[1][n] + dp[n][1]);
for(int i=1;i<=m;i++){
int now = edges[i].d;
if(mp1[i]){
int mini = 1e18;
for(int l=0;l<=mp1[i]-1;l++){
for(int r = mp1[i];r<path1.size();r++){
int x = path1[l].first;
int y = path1[r].first;
mini = min(mini, dp[1][x] + dp2[x][y] + dp[y][n]);
}
}
now += mini;
}
else{
now += min(dp[1][n],dp[1][edges[i].v] + edges[i].c + dp[edges[i].u][n]);
}
if(mp2[i]){
int mini = 1e18;
for(int l=0;l<=mp2[i]-1;l++){
for(int r = mp2[i];r<path2.size();r++){
int x = path2[l].first;
int y = path2[r].first;
mini = min(mini, dp[n][x] + dp3[x][y] + dp[y][1]);
}
}
now += mini;
}
else{
now += min(dp[n][1],dp[n][edges[i].v] + edges[i].c + dp[edges[i].u][1]);
}
ans = min(ans,now);
}
cout << (ans < 1e18?ans:-1);
}
int32_t main(){
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
#ifdef Local
freopen("in.txt","r",stdin);
freopen("out.txt","w",stdout);
#endif
int t=1;
//cin>>t;
while(t--){
solve();
}
#ifdef Local
cout<<endl<<fixed<<setprecision(2)<<1000.0 * clock() / CLOCKS_PER_SEC<< " milliseconds ";
#endif
}
Compilation message
ho_t4.cpp: In function 'void solve()':
ho_t4.cpp:93:37: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
93 | for(int r = mp1[i];r<path1.size();r++){
| ~^~~~~~~~~~~~~
ho_t4.cpp:107:37: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
107 | for(int r = mp2[i];r<path2.size();r++){
| ~^~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
33 ms |
3404 KB |
Output is correct |
2 |
Correct |
31 ms |
3276 KB |
Output is correct |
3 |
Correct |
27 ms |
3404 KB |
Output is correct |
4 |
Correct |
26 ms |
3408 KB |
Output is correct |
5 |
Correct |
3 ms |
3408 KB |
Output is correct |
6 |
Correct |
24 ms |
3280 KB |
Output is correct |
7 |
Correct |
2 ms |
3284 KB |
Output is correct |
8 |
Correct |
2 ms |
3196 KB |
Output is correct |
9 |
Correct |
3 ms |
3412 KB |
Output is correct |
10 |
Correct |
46 ms |
3404 KB |
Output is correct |
11 |
Correct |
40 ms |
3404 KB |
Output is correct |
12 |
Correct |
47 ms |
3464 KB |
Output is correct |
13 |
Correct |
26 ms |
3404 KB |
Output is correct |
14 |
Correct |
26 ms |
3468 KB |
Output is correct |
15 |
Correct |
26 ms |
3404 KB |
Output is correct |
16 |
Correct |
26 ms |
3404 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
89 ms |
12356 KB |
Output is correct |
2 |
Correct |
109 ms |
12488 KB |
Output is correct |
3 |
Correct |
87 ms |
12548 KB |
Output is correct |
4 |
Correct |
34 ms |
3588 KB |
Output is correct |
5 |
Correct |
35 ms |
3404 KB |
Output is correct |
6 |
Correct |
27 ms |
3276 KB |
Output is correct |
7 |
Correct |
25 ms |
3276 KB |
Output is correct |
8 |
Correct |
2 ms |
3276 KB |
Output is correct |
9 |
Correct |
87 ms |
12736 KB |
Output is correct |
10 |
Correct |
83 ms |
12752 KB |
Output is correct |
11 |
Correct |
87 ms |
12312 KB |
Output is correct |
12 |
Correct |
84 ms |
12316 KB |
Output is correct |
13 |
Correct |
88 ms |
12544 KB |
Output is correct |
14 |
Correct |
87 ms |
13504 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
30 ms |
3408 KB |
Output is correct |
2 |
Correct |
27 ms |
3328 KB |
Output is correct |
3 |
Correct |
71 ms |
10660 KB |
Output is correct |
4 |
Correct |
24 ms |
3276 KB |
Output is correct |
5 |
Correct |
82 ms |
12504 KB |
Output is correct |
6 |
Correct |
2 ms |
3276 KB |
Output is correct |
7 |
Correct |
2 ms |
3276 KB |
Output is correct |
8 |
Correct |
83 ms |
12708 KB |
Output is correct |
9 |
Correct |
88 ms |
12616 KB |
Output is correct |
10 |
Correct |
81 ms |
12356 KB |
Output is correct |
11 |
Correct |
85 ms |
12488 KB |
Output is correct |
12 |
Correct |
80 ms |
12484 KB |
Output is correct |
13 |
Correct |
2 ms |
3148 KB |
Output is correct |
14 |
Correct |
2 ms |
3276 KB |
Output is correct |
15 |
Correct |
2 ms |
3276 KB |
Output is correct |
16 |
Correct |
2 ms |
3252 KB |
Output is correct |
17 |
Correct |
2 ms |
3276 KB |
Output is correct |
18 |
Correct |
2 ms |
3276 KB |
Output is correct |
19 |
Correct |
94 ms |
12744 KB |
Output is correct |
20 |
Correct |
82 ms |
12484 KB |
Output is correct |
21 |
Correct |
84 ms |
12888 KB |
Output is correct |
22 |
Correct |
87 ms |
12792 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
33 ms |
3404 KB |
Output is correct |
2 |
Correct |
31 ms |
3276 KB |
Output is correct |
3 |
Correct |
27 ms |
3404 KB |
Output is correct |
4 |
Correct |
26 ms |
3408 KB |
Output is correct |
5 |
Correct |
3 ms |
3408 KB |
Output is correct |
6 |
Correct |
24 ms |
3280 KB |
Output is correct |
7 |
Correct |
2 ms |
3284 KB |
Output is correct |
8 |
Correct |
2 ms |
3196 KB |
Output is correct |
9 |
Correct |
3 ms |
3412 KB |
Output is correct |
10 |
Correct |
46 ms |
3404 KB |
Output is correct |
11 |
Correct |
40 ms |
3404 KB |
Output is correct |
12 |
Correct |
47 ms |
3464 KB |
Output is correct |
13 |
Correct |
26 ms |
3404 KB |
Output is correct |
14 |
Correct |
26 ms |
3468 KB |
Output is correct |
15 |
Correct |
26 ms |
3404 KB |
Output is correct |
16 |
Correct |
26 ms |
3404 KB |
Output is correct |
17 |
Correct |
89 ms |
12356 KB |
Output is correct |
18 |
Correct |
109 ms |
12488 KB |
Output is correct |
19 |
Correct |
87 ms |
12548 KB |
Output is correct |
20 |
Correct |
34 ms |
3588 KB |
Output is correct |
21 |
Correct |
35 ms |
3404 KB |
Output is correct |
22 |
Correct |
27 ms |
3276 KB |
Output is correct |
23 |
Correct |
25 ms |
3276 KB |
Output is correct |
24 |
Correct |
2 ms |
3276 KB |
Output is correct |
25 |
Correct |
87 ms |
12736 KB |
Output is correct |
26 |
Correct |
83 ms |
12752 KB |
Output is correct |
27 |
Correct |
87 ms |
12312 KB |
Output is correct |
28 |
Correct |
84 ms |
12316 KB |
Output is correct |
29 |
Correct |
88 ms |
12544 KB |
Output is correct |
30 |
Correct |
87 ms |
13504 KB |
Output is correct |
31 |
Correct |
30 ms |
3408 KB |
Output is correct |
32 |
Correct |
27 ms |
3328 KB |
Output is correct |
33 |
Correct |
71 ms |
10660 KB |
Output is correct |
34 |
Correct |
24 ms |
3276 KB |
Output is correct |
35 |
Correct |
82 ms |
12504 KB |
Output is correct |
36 |
Correct |
2 ms |
3276 KB |
Output is correct |
37 |
Correct |
2 ms |
3276 KB |
Output is correct |
38 |
Correct |
83 ms |
12708 KB |
Output is correct |
39 |
Correct |
88 ms |
12616 KB |
Output is correct |
40 |
Correct |
81 ms |
12356 KB |
Output is correct |
41 |
Correct |
85 ms |
12488 KB |
Output is correct |
42 |
Correct |
80 ms |
12484 KB |
Output is correct |
43 |
Correct |
2 ms |
3148 KB |
Output is correct |
44 |
Correct |
2 ms |
3276 KB |
Output is correct |
45 |
Correct |
2 ms |
3276 KB |
Output is correct |
46 |
Correct |
2 ms |
3252 KB |
Output is correct |
47 |
Correct |
2 ms |
3276 KB |
Output is correct |
48 |
Correct |
2 ms |
3276 KB |
Output is correct |
49 |
Correct |
94 ms |
12744 KB |
Output is correct |
50 |
Correct |
82 ms |
12484 KB |
Output is correct |
51 |
Correct |
84 ms |
12888 KB |
Output is correct |
52 |
Correct |
87 ms |
12792 KB |
Output is correct |
53 |
Correct |
100 ms |
12864 KB |
Output is correct |
54 |
Correct |
84 ms |
12924 KB |
Output is correct |
55 |
Correct |
83 ms |
12796 KB |
Output is correct |
56 |
Correct |
25 ms |
3404 KB |
Output is correct |
57 |
Correct |
26 ms |
3404 KB |
Output is correct |
58 |
Correct |
79 ms |
11004 KB |
Output is correct |
59 |
Correct |
79 ms |
11044 KB |
Output is correct |
60 |
Correct |
79 ms |
11048 KB |
Output is correct |
61 |
Correct |
78 ms |
11076 KB |
Output is correct |
62 |
Correct |
82 ms |
11044 KB |
Output is correct |
63 |
Correct |
75 ms |
11116 KB |
Output is correct |
64 |
Correct |
84 ms |
11208 KB |
Output is correct |
65 |
Correct |
87 ms |
11200 KB |
Output is correct |
66 |
Correct |
82 ms |
11084 KB |
Output is correct |
67 |
Correct |
58 ms |
10784 KB |
Output is correct |
68 |
Correct |
87 ms |
13384 KB |
Output is correct |
69 |
Correct |
86 ms |
13408 KB |
Output is correct |
70 |
Correct |
84 ms |
13140 KB |
Output is correct |
71 |
Correct |
107 ms |
13012 KB |
Output is correct |
72 |
Correct |
84 ms |
13148 KB |
Output is correct |
73 |
Correct |
85 ms |
13200 KB |
Output is correct |
74 |
Correct |
84 ms |
13272 KB |
Output is correct |