#include "bits/stdc++.h"
using namespace std;
#define FAST ios_base::sync_with_stdio(false); cin.tie(0);
#define pb push_back
#define eb emplace_back
#define ins insert
#define f first
#define s second
#define cbr cerr << "hi\n"
#define mmst(x, v) memset((x), v, sizeof ((x)))
#define siz(x) ll(x.size())
#define all(x) (x).begin(), (x).end()
#define lbd(x,y) (lower_bound(all(x),y)-x.begin())
#define ubd(x,y) (upper_bound(all(x),y)-x.begin())
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); //can be used by calling rng() or shuffle(A, A+n, rng)
inline long long rand(long long x, long long y) { return (rng() % (y+1-x)) + x; } //inclusivesss
string inline to_string(char c) {string s(1,c);return s;} template<typename T> inline T gcd(T a,T b){ return a==0?llabs(b):gcd(b%a,a); }
typedef long long ll;
typedef long double ld;
#define FOR(i,s,e) for(ll i=s;i<=ll(e);++i)
#define DEC(i,s,e) for(ll i=s;i>=ll(e);--i)
typedef pair<ll,ll>pi; typedef pair<ll,pi>spi; typedef pair<pi,pi>dpi;
#define LLINF ((long long)1e18)
#define INF int(1e9+1e6)
#define MAXN (50006)
ll n,m,dist[206][206],disty[206];
vector<ll>v[206];
struct edge{
ll a,b,c,d;
}E[MAXN];
ll sp(ll start,ll ep){
mmst(disty,-1);
priority_queue<pi,vector<pi>,greater<pi>> pq;
disty[start]=0, pq.emplace(disty[start],start);
while(pq.size()){
ll x=pq.top().s,d=pq.top().f;
pq.pop();
if(disty[x]^d)continue;
for(auto i:v[x]) if(E[i].a==x&&(disty[E[i].b]==-1||disty[E[i].b]>d+E[i].c)) {
disty[E[i].b]=d+E[i].c;
pq.emplace(disty[E[i].b],E[i].b);
}
}
return disty[ep] == -1 ? LLINF : disty[ep];
}
ll get_dist(){
return sp(1,n) + sp(n,1);
}
int main(){
FAST
cin>>n>>m;
FOR(i,1,n)FOR(j,1,n)dist[i][j]=(i==j)?0:LLINF;
FOR(i,1,m){
ll a,b,c,d;cin>>a>>b>>c>>d;
v[a].pb(i), v[b].pb(i);
E[i]={a,b,c,d};
dist[a][b]=min(dist[a][b],c);
}
FOR(k,1,n)FOR(i,1,n)FOR(j,1,n)dist[i][j]=min(dist[i][j],dist[i][k]+dist[k][j]);
ll ans=min(LLINF,dist[1][n]+dist[n][1]);
vector<int> p; FOR(i,1,m)p.pb(i); shuffle(all(p),rng);
shuffle(all(p),rng);
for(auto i:p) if(min(dist[1][n],dist[1][E[i].b]+E[i].c+dist[E[i].a][n])+min(dist[n][1],dist[n][E[i].b]+E[i].c+dist[E[i].a][1])+E[i].d < ans) {
swap(E[i].a,E[i].b);
ans=min(ans,get_dist()+E[i].d);
swap(E[i].a,E[i].b);
}
if(ans>=LLINF)ans=-1;
cout<<ans<<'\n';
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
768 KB |
Output is correct |
2 |
Correct |
13 ms |
768 KB |
Output is correct |
3 |
Correct |
14 ms |
768 KB |
Output is correct |
4 |
Correct |
14 ms |
768 KB |
Output is correct |
5 |
Correct |
5 ms |
512 KB |
Output is correct |
6 |
Correct |
13 ms |
768 KB |
Output is correct |
7 |
Correct |
5 ms |
384 KB |
Output is correct |
8 |
Correct |
4 ms |
384 KB |
Output is correct |
9 |
Correct |
5 ms |
384 KB |
Output is correct |
10 |
Correct |
19 ms |
768 KB |
Output is correct |
11 |
Correct |
33 ms |
896 KB |
Output is correct |
12 |
Correct |
15 ms |
768 KB |
Output is correct |
13 |
Correct |
14 ms |
768 KB |
Output is correct |
14 |
Correct |
14 ms |
768 KB |
Output is correct |
15 |
Correct |
15 ms |
768 KB |
Output is correct |
16 |
Correct |
14 ms |
768 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
39 ms |
4984 KB |
Output is correct |
2 |
Correct |
42 ms |
4856 KB |
Output is correct |
3 |
Correct |
40 ms |
5000 KB |
Output is correct |
4 |
Correct |
15 ms |
896 KB |
Output is correct |
5 |
Correct |
14 ms |
768 KB |
Output is correct |
6 |
Correct |
14 ms |
768 KB |
Output is correct |
7 |
Correct |
14 ms |
768 KB |
Output is correct |
8 |
Correct |
5 ms |
384 KB |
Output is correct |
9 |
Correct |
54 ms |
5000 KB |
Output is correct |
10 |
Correct |
53 ms |
5112 KB |
Output is correct |
11 |
Correct |
41 ms |
5112 KB |
Output is correct |
12 |
Correct |
43 ms |
4988 KB |
Output is correct |
13 |
Correct |
42 ms |
5120 KB |
Output is correct |
14 |
Correct |
43 ms |
5248 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
768 KB |
Output is correct |
2 |
Correct |
13 ms |
640 KB |
Output is correct |
3 |
Correct |
31 ms |
3968 KB |
Output is correct |
4 |
Correct |
14 ms |
640 KB |
Output is correct |
5 |
Correct |
36 ms |
4736 KB |
Output is correct |
6 |
Correct |
5 ms |
384 KB |
Output is correct |
7 |
Correct |
5 ms |
384 KB |
Output is correct |
8 |
Correct |
47 ms |
4856 KB |
Output is correct |
9 |
Correct |
42 ms |
4752 KB |
Output is correct |
10 |
Correct |
37 ms |
4856 KB |
Output is correct |
11 |
Correct |
38 ms |
4744 KB |
Output is correct |
12 |
Correct |
37 ms |
4872 KB |
Output is correct |
13 |
Correct |
5 ms |
384 KB |
Output is correct |
14 |
Correct |
5 ms |
384 KB |
Output is correct |
15 |
Correct |
5 ms |
384 KB |
Output is correct |
16 |
Correct |
5 ms |
384 KB |
Output is correct |
17 |
Correct |
5 ms |
384 KB |
Output is correct |
18 |
Correct |
5 ms |
384 KB |
Output is correct |
19 |
Correct |
38 ms |
4872 KB |
Output is correct |
20 |
Correct |
40 ms |
4856 KB |
Output is correct |
21 |
Correct |
39 ms |
4856 KB |
Output is correct |
22 |
Correct |
39 ms |
4864 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
768 KB |
Output is correct |
2 |
Correct |
13 ms |
768 KB |
Output is correct |
3 |
Correct |
14 ms |
768 KB |
Output is correct |
4 |
Correct |
14 ms |
768 KB |
Output is correct |
5 |
Correct |
5 ms |
512 KB |
Output is correct |
6 |
Correct |
13 ms |
768 KB |
Output is correct |
7 |
Correct |
5 ms |
384 KB |
Output is correct |
8 |
Correct |
4 ms |
384 KB |
Output is correct |
9 |
Correct |
5 ms |
384 KB |
Output is correct |
10 |
Correct |
19 ms |
768 KB |
Output is correct |
11 |
Correct |
33 ms |
896 KB |
Output is correct |
12 |
Correct |
15 ms |
768 KB |
Output is correct |
13 |
Correct |
14 ms |
768 KB |
Output is correct |
14 |
Correct |
14 ms |
768 KB |
Output is correct |
15 |
Correct |
15 ms |
768 KB |
Output is correct |
16 |
Correct |
14 ms |
768 KB |
Output is correct |
17 |
Correct |
39 ms |
4984 KB |
Output is correct |
18 |
Correct |
42 ms |
4856 KB |
Output is correct |
19 |
Correct |
40 ms |
5000 KB |
Output is correct |
20 |
Correct |
15 ms |
896 KB |
Output is correct |
21 |
Correct |
14 ms |
768 KB |
Output is correct |
22 |
Correct |
14 ms |
768 KB |
Output is correct |
23 |
Correct |
14 ms |
768 KB |
Output is correct |
24 |
Correct |
5 ms |
384 KB |
Output is correct |
25 |
Correct |
54 ms |
5000 KB |
Output is correct |
26 |
Correct |
53 ms |
5112 KB |
Output is correct |
27 |
Correct |
41 ms |
5112 KB |
Output is correct |
28 |
Correct |
43 ms |
4988 KB |
Output is correct |
29 |
Correct |
42 ms |
5120 KB |
Output is correct |
30 |
Correct |
43 ms |
5248 KB |
Output is correct |
31 |
Correct |
14 ms |
768 KB |
Output is correct |
32 |
Correct |
13 ms |
640 KB |
Output is correct |
33 |
Correct |
31 ms |
3968 KB |
Output is correct |
34 |
Correct |
14 ms |
640 KB |
Output is correct |
35 |
Correct |
36 ms |
4736 KB |
Output is correct |
36 |
Correct |
5 ms |
384 KB |
Output is correct |
37 |
Correct |
5 ms |
384 KB |
Output is correct |
38 |
Correct |
47 ms |
4856 KB |
Output is correct |
39 |
Correct |
42 ms |
4752 KB |
Output is correct |
40 |
Correct |
37 ms |
4856 KB |
Output is correct |
41 |
Correct |
38 ms |
4744 KB |
Output is correct |
42 |
Correct |
37 ms |
4872 KB |
Output is correct |
43 |
Correct |
5 ms |
384 KB |
Output is correct |
44 |
Correct |
5 ms |
384 KB |
Output is correct |
45 |
Correct |
5 ms |
384 KB |
Output is correct |
46 |
Correct |
5 ms |
384 KB |
Output is correct |
47 |
Correct |
5 ms |
384 KB |
Output is correct |
48 |
Correct |
5 ms |
384 KB |
Output is correct |
49 |
Correct |
38 ms |
4872 KB |
Output is correct |
50 |
Correct |
40 ms |
4856 KB |
Output is correct |
51 |
Correct |
39 ms |
4856 KB |
Output is correct |
52 |
Correct |
39 ms |
4864 KB |
Output is correct |
53 |
Correct |
40 ms |
4856 KB |
Output is correct |
54 |
Correct |
39 ms |
4844 KB |
Output is correct |
55 |
Correct |
39 ms |
4744 KB |
Output is correct |
56 |
Correct |
14 ms |
768 KB |
Output is correct |
57 |
Correct |
14 ms |
768 KB |
Output is correct |
58 |
Correct |
35 ms |
3968 KB |
Output is correct |
59 |
Correct |
38 ms |
4088 KB |
Output is correct |
60 |
Correct |
46 ms |
4088 KB |
Output is correct |
61 |
Correct |
38 ms |
3960 KB |
Output is correct |
62 |
Correct |
151 ms |
4088 KB |
Output is correct |
63 |
Correct |
328 ms |
3960 KB |
Output is correct |
64 |
Correct |
112 ms |
4268 KB |
Output is correct |
65 |
Correct |
245 ms |
4288 KB |
Output is correct |
66 |
Correct |
467 ms |
4408 KB |
Output is correct |
67 |
Correct |
24 ms |
3444 KB |
Output is correct |
68 |
Correct |
60 ms |
4992 KB |
Output is correct |
69 |
Correct |
55 ms |
4992 KB |
Output is correct |
70 |
Correct |
41 ms |
4984 KB |
Output is correct |
71 |
Correct |
45 ms |
5112 KB |
Output is correct |
72 |
Correct |
41 ms |
5000 KB |
Output is correct |
73 |
Correct |
40 ms |
5112 KB |
Output is correct |
74 |
Correct |
42 ms |
5112 KB |
Output is correct |