#include <bits/stdc++.h>
#define pb push_back
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int MAX_N = 200;
const int MAX_M = 50000;
const ll INF = 1e14;
int N, M;
vector<pii> gp[2][MAX_N+1];
int u[MAX_M+1], v[MAX_M+1];
ll d[MAX_M+1], c[MAX_M+1];
ll dp[2][2][MAX_N+1];
ll ans;
vector<int> vt;
int ST;
priority_queue<pll, vector<pll>, greater<pll> > pq;
bool vst[MAX_N+1];
int from[MAX_N+1];
void solve(int x, int y){
for(int i=1; i<=N; i++){
dp[x][y][i] = INF;
vst[i] = false;
from[i] = 0;
}
dp[x][y][ST] = 0;
pq.push({0LL, (ll)ST});
while(!pq.empty()){
pll now = pq.top(); pq.pop();
if(vst[now.second]) continue;
vst[now.second] = true;
if(from[now.second]!=0 && x==0){
vt.pb(from[now.second]);
}
for(pii p : gp[x][now.second]){
if(dp[x][y][p.first]>dp[x][y][now.second] + c[p.second]){
from[p.first] = p.second;
dp[x][y][p.first] = dp[x][y][now.second] + c[p.second];
pq.push({dp[x][y][p.first], p.first});
}
}
}
}
bool chk[MAX_M+1];
vector<pii> gp2[MAX_N+1];
ll dp2[MAX_N+1], dp3[MAX_N+1];
void solve2(int x){
for(int i=1; i<=N; i++){
//while(!gp2[i].empty()) gp2[i].pop_back();
dp2[i] = dp3[i] = INF;
vst[i] = false;
}
/*for(int i=1; i<=M; i++){
if(i!=x){
gp2[u[i]].pb({v[i], (int)c[i]});
}else{
gp2[v[i]].pb({u[i], (int)c[i]});
}
}*/
gp[0][v[x]].pb({u[x], x});
pq.push({0LL, 1LL});
dp2[1] = 0;
while(!pq.empty()){
pll now = pq.top(); pq.pop();
if(vst[now.second]) continue;
vst[now.second] = true;
for(pii p : gp[0][now.second]){
if(now.second==u[x] && p.second==x) continue;
if(dp2[p.first]>dp2[now.second] + c[p.second]){
dp2[p.first] = dp2[now.second] + c[p.second];
pq.push({dp2[p.first], p.first});
}
}
}
for(int i=1; i<=N; i++) vst[i] = false;
pq.push({0LL, (ll)N});
dp3[N] = 0;
while(!pq.empty()){
pll now = pq.top(); pq.pop();
if(vst[now.second]) continue;
vst[now.second] = true;
for(pii p : gp[0][now.second]){
if(now.second==u[x] && p.second==x) continue;
if(dp3[p.first]>dp3[now.second] + c[p.second]){
dp3[p.first] = dp3[now.second] + c[p.second];
pq.push({dp3[p.first], p.first});
}
}
}
gp[0][v[x]].pop_back();
//cout<<x<<" "<<dp2[N]<<" "<<dp3[1]<<" "<<d[x]<<endl;
ans = min(ans, dp2[N] + dp3[1] + d[x]);
}
int main(){
scanf("%d%d", &N, &M);
for(int i=1; i<=M; i++){
scanf("%d%d%lld%lld", &u[i], &v[i], &c[i], &d[i]);
gp[0][u[i]].pb({v[i], i});
gp[1][v[i]].pb({u[i], i});
}
ST = 1; solve(0, 0); ST = N; solve(0, 1);
ST = 1; solve(1, 0); ST = N; solve(1, 1);
ans = dp[0][0][N] + dp[0][1][1];
for(int i : vt){
if(!chk[i]){
solve2(i);
chk[i] = true;
}
}
/*for(int i=0; i<2; i++){
for(int j=0; j<2; j++){
for(int k=1; k<=N; k++){
cout<<dp[i][j][k]<<" ";
}
cout<<endl;
}
}*/
for(int i=1; i<=M; i++){
if(!chk[i]){
//solve2(i);
ll dist = d[i];
if(dp[0][0][u[i]]<=dp[0][0][v[i]]+c[i]) dist+=dp[0][0][N];
else dist+=min(dp[0][0][N], dp[0][0][v[i]] + c[i] + dp[1][1][u[i]]);
if(dp[0][1][u[i]]<=dp[0][1][v[i]]+c[i]) dist+=dp[0][1][1];
else dist+=min(dp[0][1][1], dp[0][1][v[i]] + c[i] + dp[1][0][u[i]]);
// cout<<u[i]<<" "<<v[i]<<" "<<c[i]<<" "<<d[i]<<endl;
// cout<<dp[0][0][v[i]]<<" "<<dp[1][1][u[i]]<<" "<<dp[0][1][v[i]]<<" "<<dp[1][0][u[i]]<<endl;
ans = min(ans, dist);
}
}
if(ans>=INF){
printf("-1");
}else{
printf("%lld\n", ans);
}
return 0;
}
Compilation message
ho_t4.cpp: In function 'int main()':
ho_t4.cpp:112:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d", &N, &M);
~~~~~^~~~~~~~~~~~~~~~
ho_t4.cpp:114:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d%lld%lld", &u[i], &v[i], &c[i], &d[i]);
~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
376 KB |
Output is correct |
2 |
Correct |
5 ms |
376 KB |
Output is correct |
3 |
Correct |
21 ms |
376 KB |
Output is correct |
4 |
Correct |
23 ms |
376 KB |
Output is correct |
5 |
Correct |
6 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 |
5 ms |
376 KB |
Output is correct |
10 |
Correct |
27 ms |
376 KB |
Output is correct |
11 |
Correct |
30 ms |
376 KB |
Output is correct |
12 |
Correct |
27 ms |
376 KB |
Output is correct |
13 |
Correct |
8 ms |
376 KB |
Output is correct |
14 |
Correct |
14 ms |
376 KB |
Output is correct |
15 |
Correct |
13 ms |
504 KB |
Output is correct |
16 |
Correct |
13 ms |
376 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
153 ms |
2936 KB |
Output is correct |
2 |
Correct |
156 ms |
3064 KB |
Output is correct |
3 |
Correct |
151 ms |
2936 KB |
Output is correct |
4 |
Correct |
20 ms |
504 KB |
Output is correct |
5 |
Correct |
12 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 |
57 ms |
2808 KB |
Output is correct |
10 |
Correct |
53 ms |
2808 KB |
Output is correct |
11 |
Correct |
105 ms |
2936 KB |
Output is correct |
12 |
Correct |
103 ms |
2936 KB |
Output is correct |
13 |
Correct |
105 ms |
3064 KB |
Output is correct |
14 |
Correct |
86 ms |
2936 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
504 KB |
Output is correct |
2 |
Correct |
5 ms |
376 KB |
Output is correct |
3 |
Correct |
95 ms |
2168 KB |
Output is correct |
4 |
Correct |
5 ms |
376 KB |
Output is correct |
5 |
Correct |
118 ms |
2936 KB |
Output is correct |
6 |
Correct |
5 ms |
376 KB |
Output is correct |
7 |
Correct |
5 ms |
376 KB |
Output is correct |
8 |
Correct |
50 ms |
2936 KB |
Output is correct |
9 |
Correct |
51 ms |
2808 KB |
Output is correct |
10 |
Correct |
85 ms |
2808 KB |
Output is correct |
11 |
Correct |
75 ms |
2808 KB |
Output is correct |
12 |
Correct |
88 ms |
3044 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 |
86 ms |
2936 KB |
Output is correct |
20 |
Correct |
77 ms |
2808 KB |
Output is correct |
21 |
Correct |
80 ms |
2808 KB |
Output is correct |
22 |
Correct |
82 ms |
2808 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
376 KB |
Output is correct |
2 |
Correct |
5 ms |
376 KB |
Output is correct |
3 |
Correct |
21 ms |
376 KB |
Output is correct |
4 |
Correct |
23 ms |
376 KB |
Output is correct |
5 |
Correct |
6 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 |
5 ms |
376 KB |
Output is correct |
10 |
Correct |
27 ms |
376 KB |
Output is correct |
11 |
Correct |
30 ms |
376 KB |
Output is correct |
12 |
Correct |
27 ms |
376 KB |
Output is correct |
13 |
Correct |
8 ms |
376 KB |
Output is correct |
14 |
Correct |
14 ms |
376 KB |
Output is correct |
15 |
Correct |
13 ms |
504 KB |
Output is correct |
16 |
Correct |
13 ms |
376 KB |
Output is correct |
17 |
Correct |
153 ms |
2936 KB |
Output is correct |
18 |
Correct |
156 ms |
3064 KB |
Output is correct |
19 |
Correct |
151 ms |
2936 KB |
Output is correct |
20 |
Correct |
20 ms |
504 KB |
Output is correct |
21 |
Correct |
12 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 |
57 ms |
2808 KB |
Output is correct |
26 |
Correct |
53 ms |
2808 KB |
Output is correct |
27 |
Correct |
105 ms |
2936 KB |
Output is correct |
28 |
Correct |
103 ms |
2936 KB |
Output is correct |
29 |
Correct |
105 ms |
3064 KB |
Output is correct |
30 |
Correct |
86 ms |
2936 KB |
Output is correct |
31 |
Correct |
16 ms |
504 KB |
Output is correct |
32 |
Correct |
5 ms |
376 KB |
Output is correct |
33 |
Correct |
95 ms |
2168 KB |
Output is correct |
34 |
Correct |
5 ms |
376 KB |
Output is correct |
35 |
Correct |
118 ms |
2936 KB |
Output is correct |
36 |
Correct |
5 ms |
376 KB |
Output is correct |
37 |
Correct |
5 ms |
376 KB |
Output is correct |
38 |
Correct |
50 ms |
2936 KB |
Output is correct |
39 |
Correct |
51 ms |
2808 KB |
Output is correct |
40 |
Correct |
85 ms |
2808 KB |
Output is correct |
41 |
Correct |
75 ms |
2808 KB |
Output is correct |
42 |
Correct |
88 ms |
3044 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 |
86 ms |
2936 KB |
Output is correct |
50 |
Correct |
77 ms |
2808 KB |
Output is correct |
51 |
Correct |
80 ms |
2808 KB |
Output is correct |
52 |
Correct |
82 ms |
2808 KB |
Output is correct |
53 |
Correct |
156 ms |
2868 KB |
Output is correct |
54 |
Correct |
171 ms |
2936 KB |
Output is correct |
55 |
Correct |
156 ms |
2808 KB |
Output is correct |
56 |
Correct |
22 ms |
376 KB |
Output is correct |
57 |
Correct |
20 ms |
376 KB |
Output is correct |
58 |
Correct |
111 ms |
2176 KB |
Output is correct |
59 |
Correct |
132 ms |
2296 KB |
Output is correct |
60 |
Correct |
159 ms |
2296 KB |
Output is correct |
61 |
Correct |
103 ms |
2296 KB |
Output is correct |
62 |
Correct |
120 ms |
2296 KB |
Output is correct |
63 |
Correct |
164 ms |
2296 KB |
Output is correct |
64 |
Correct |
709 ms |
2688 KB |
Output is correct |
65 |
Correct |
760 ms |
2940 KB |
Output is correct |
66 |
Correct |
976 ms |
2816 KB |
Output is correct |
67 |
Correct |
26 ms |
2160 KB |
Output is correct |
68 |
Correct |
61 ms |
2808 KB |
Output is correct |
69 |
Correct |
57 ms |
2808 KB |
Output is correct |
70 |
Correct |
129 ms |
2904 KB |
Output is correct |
71 |
Correct |
114 ms |
2808 KB |
Output is correct |
72 |
Correct |
114 ms |
2808 KB |
Output is correct |
73 |
Correct |
120 ms |
2936 KB |
Output is correct |
74 |
Correct |
113 ms |
2952 KB |
Output is correct |