#include <bits/stdc++.h>
using namespace std;
#define int long long
#ifdef DEBUG
#define dbg(...) printf(__VA_ARGS__);
#else
#define dbg(...)
#endif
#define sf scanf
#define pf printf
#define fi first
#define se second
#define pb emplace_back
#define sz(x) (int)x.size()
#define mnto(x,y) x=min(x,(__typeof__(x))y)
#define mxto(x,y) x=max(x,(__typeof__(x))y)
#define INF 1023456789123456789ll
#define all(x) x.begin(), x.end()
typedef long long ll;
typedef long double ld;
typedef pair<int, int> ii;
typedef pair<ll, ll> pll;
typedef tuple<int, int, int> iii;
typedef tuple<int, int, int, int> iiii;
typedef vector<int> vi;
typedef vector<ii> vii;
typedef vector<pll> vll;
mt19937 rng(time(0));
#define maxn 205
#define maxm 50005
int n,m,u[maxm],v[maxm],c[maxm],d[maxm],dd[maxn];
int dist[maxn][maxn];
vi AL[maxn];
priority_queue<ii,vector<ii>,greater<ii>> pq;
int dij(int s,int t){
for(int i=0;i<n;++i)dd[i]=INF;
dd[s]=0;pq.push(ii(0,s));
while(!pq.empty()){
ii pr=pq.top();pq.pop();
if(dd[pr.se]<pr.fi)continue;
for(int i:AL[pr.se]){
if(dd[v[i]]>dd[u[i]]+c[i]){
dd[v[i]]=dd[u[i]]+c[i];
pq.push(ii(dd[v[i]],v[i]));
}
}
}
return dd[t];
}
int32_t main(){
sf("%lld%lld",&n,&m);
for(int i=0;i<n;++i){
for(int j=0;j<n;++j){
if(i!=j)dist[i][j]=INF;
}
}
for(int i=0;i<m;++i){
sf("%lld%lld%lld%lld",&u[i],&v[i],&c[i],&d[i]);
--u[i];--v[i];
mnto(dist[u[i]][v[i]],c[i]);
AL[u[i]].pb(i);
}
for(int k=0;k<n;++k){
for(int i=0;i<n;++i){
for(int j=0;j<n;++j){
mnto(dist[i][j],dist[i][k]+dist[k][j]);
}
}
}
int ans=min(INF,dist[0][n-1]+dist[n-1][0]);
for(int i=0;i<m;++i){
int d1=min(dist[0][n-1],dist[0][v[i]]+c[i]+dist[u[i]][n-1]);
int d2=min(dist[n-1][0],dist[n-1][v[i]]+c[i]+dist[u[i]][0]);
if(d1+d2+d[i]<ans){
AL[u[i]].erase(find(all(AL[u[i]]),i));
AL[v[i]].pb(i);
swap(u[i],v[i]);
mnto(ans,dij(0,n-1)+dij(n-1,0)+d[i]);
swap(u[i],v[i]);
AL[u[i]].pb(i);
AL[v[i]].pop_back();
}
}
if(ans==INF)ans=-1;
pf("%lld\n",ans);
}
Compilation message
ho_t4.cpp: In function 'int32_t main()':
ho_t4.cpp:57:4: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
57 | sf("%lld%lld",&n,&m);
| ^
ho_t4.cpp:64:5: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
64 | sf("%lld%lld%lld%lld",&u[i],&v[i],&c[i],&d[i]);
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
708 KB |
Output is correct |
2 |
Correct |
10 ms |
660 KB |
Output is correct |
3 |
Correct |
10 ms |
720 KB |
Output is correct |
4 |
Correct |
10 ms |
716 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
10 ms |
660 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
0 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
14 ms |
728 KB |
Output is correct |
11 |
Correct |
23 ms |
712 KB |
Output is correct |
12 |
Correct |
28 ms |
704 KB |
Output is correct |
13 |
Correct |
11 ms |
716 KB |
Output is correct |
14 |
Correct |
11 ms |
716 KB |
Output is correct |
15 |
Correct |
10 ms |
712 KB |
Output is correct |
16 |
Correct |
10 ms |
716 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
41 ms |
3848 KB |
Output is correct |
2 |
Correct |
32 ms |
3864 KB |
Output is correct |
3 |
Correct |
32 ms |
3908 KB |
Output is correct |
4 |
Correct |
11 ms |
716 KB |
Output is correct |
5 |
Correct |
11 ms |
716 KB |
Output is correct |
6 |
Correct |
10 ms |
572 KB |
Output is correct |
7 |
Correct |
12 ms |
664 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
40 ms |
4000 KB |
Output is correct |
10 |
Correct |
35 ms |
4000 KB |
Output is correct |
11 |
Correct |
45 ms |
4036 KB |
Output is correct |
12 |
Correct |
33 ms |
4004 KB |
Output is correct |
13 |
Correct |
33 ms |
4056 KB |
Output is correct |
14 |
Correct |
32 ms |
4156 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
716 KB |
Output is correct |
2 |
Correct |
10 ms |
656 KB |
Output is correct |
3 |
Correct |
26 ms |
3036 KB |
Output is correct |
4 |
Correct |
10 ms |
660 KB |
Output is correct |
5 |
Correct |
31 ms |
3680 KB |
Output is correct |
6 |
Correct |
0 ms |
332 KB |
Output is correct |
7 |
Correct |
0 ms |
316 KB |
Output is correct |
8 |
Correct |
36 ms |
3760 KB |
Output is correct |
9 |
Correct |
37 ms |
3768 KB |
Output is correct |
10 |
Correct |
44 ms |
3708 KB |
Output is correct |
11 |
Correct |
42 ms |
3768 KB |
Output is correct |
12 |
Correct |
32 ms |
3748 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
0 ms |
332 KB |
Output is correct |
15 |
Correct |
0 ms |
332 KB |
Output is correct |
16 |
Correct |
1 ms |
332 KB |
Output is correct |
17 |
Correct |
0 ms |
332 KB |
Output is correct |
18 |
Correct |
1 ms |
332 KB |
Output is correct |
19 |
Correct |
31 ms |
3788 KB |
Output is correct |
20 |
Correct |
42 ms |
3780 KB |
Output is correct |
21 |
Correct |
40 ms |
3788 KB |
Output is correct |
22 |
Correct |
42 ms |
3760 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
708 KB |
Output is correct |
2 |
Correct |
10 ms |
660 KB |
Output is correct |
3 |
Correct |
10 ms |
720 KB |
Output is correct |
4 |
Correct |
10 ms |
716 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
10 ms |
660 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
0 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
14 ms |
728 KB |
Output is correct |
11 |
Correct |
23 ms |
712 KB |
Output is correct |
12 |
Correct |
28 ms |
704 KB |
Output is correct |
13 |
Correct |
11 ms |
716 KB |
Output is correct |
14 |
Correct |
11 ms |
716 KB |
Output is correct |
15 |
Correct |
10 ms |
712 KB |
Output is correct |
16 |
Correct |
10 ms |
716 KB |
Output is correct |
17 |
Correct |
41 ms |
3848 KB |
Output is correct |
18 |
Correct |
32 ms |
3864 KB |
Output is correct |
19 |
Correct |
32 ms |
3908 KB |
Output is correct |
20 |
Correct |
11 ms |
716 KB |
Output is correct |
21 |
Correct |
11 ms |
716 KB |
Output is correct |
22 |
Correct |
10 ms |
572 KB |
Output is correct |
23 |
Correct |
12 ms |
664 KB |
Output is correct |
24 |
Correct |
1 ms |
332 KB |
Output is correct |
25 |
Correct |
40 ms |
4000 KB |
Output is correct |
26 |
Correct |
35 ms |
4000 KB |
Output is correct |
27 |
Correct |
45 ms |
4036 KB |
Output is correct |
28 |
Correct |
33 ms |
4004 KB |
Output is correct |
29 |
Correct |
33 ms |
4056 KB |
Output is correct |
30 |
Correct |
32 ms |
4156 KB |
Output is correct |
31 |
Correct |
10 ms |
716 KB |
Output is correct |
32 |
Correct |
10 ms |
656 KB |
Output is correct |
33 |
Correct |
26 ms |
3036 KB |
Output is correct |
34 |
Correct |
10 ms |
660 KB |
Output is correct |
35 |
Correct |
31 ms |
3680 KB |
Output is correct |
36 |
Correct |
0 ms |
332 KB |
Output is correct |
37 |
Correct |
0 ms |
316 KB |
Output is correct |
38 |
Correct |
36 ms |
3760 KB |
Output is correct |
39 |
Correct |
37 ms |
3768 KB |
Output is correct |
40 |
Correct |
44 ms |
3708 KB |
Output is correct |
41 |
Correct |
42 ms |
3768 KB |
Output is correct |
42 |
Correct |
32 ms |
3748 KB |
Output is correct |
43 |
Correct |
1 ms |
332 KB |
Output is correct |
44 |
Correct |
0 ms |
332 KB |
Output is correct |
45 |
Correct |
0 ms |
332 KB |
Output is correct |
46 |
Correct |
1 ms |
332 KB |
Output is correct |
47 |
Correct |
0 ms |
332 KB |
Output is correct |
48 |
Correct |
1 ms |
332 KB |
Output is correct |
49 |
Correct |
31 ms |
3788 KB |
Output is correct |
50 |
Correct |
42 ms |
3780 KB |
Output is correct |
51 |
Correct |
40 ms |
3788 KB |
Output is correct |
52 |
Correct |
42 ms |
3760 KB |
Output is correct |
53 |
Correct |
31 ms |
3812 KB |
Output is correct |
54 |
Correct |
32 ms |
3804 KB |
Output is correct |
55 |
Correct |
31 ms |
3792 KB |
Output is correct |
56 |
Correct |
11 ms |
716 KB |
Output is correct |
57 |
Correct |
10 ms |
712 KB |
Output is correct |
58 |
Correct |
38 ms |
3172 KB |
Output is correct |
59 |
Correct |
30 ms |
3212 KB |
Output is correct |
60 |
Correct |
30 ms |
3204 KB |
Output is correct |
61 |
Correct |
29 ms |
3208 KB |
Output is correct |
62 |
Correct |
66 ms |
3192 KB |
Output is correct |
63 |
Correct |
121 ms |
3184 KB |
Output is correct |
64 |
Correct |
84 ms |
3556 KB |
Output is correct |
65 |
Correct |
205 ms |
3632 KB |
Output is correct |
66 |
Correct |
417 ms |
3548 KB |
Output is correct |
67 |
Correct |
18 ms |
2760 KB |
Output is correct |
68 |
Correct |
40 ms |
3968 KB |
Output is correct |
69 |
Correct |
35 ms |
4128 KB |
Output is correct |
70 |
Correct |
33 ms |
4036 KB |
Output is correct |
71 |
Correct |
33 ms |
3908 KB |
Output is correct |
72 |
Correct |
32 ms |
4052 KB |
Output is correct |
73 |
Correct |
32 ms |
4048 KB |
Output is correct |
74 |
Correct |
33 ms |
3952 KB |
Output is correct |