Submission #417283

# Submission time Handle Problem Language Result Execution time Memory
417283 2021-06-03T14:21:25 Z AmineWeslati Olympic Bus (JOI20_ho_t4) C++14
37 / 100
1000 ms 4972 KB
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef vector<int>vi; 
#define pb push_back
#define sz(x) (int)x.size()
#define all(x) begin(x),end(x)

typedef pair<int,int>pi;
#define fi first
#define se second

#define FOR(i,a,b) for(int i=a; i<b; i++)
#define ROF(i,a,b) for(int i=b-1; i>=a; i--)

void IO() {
#ifdef LOCAL
    freopen("input.txt", "r", stdin); 
    freopen("output.txt", "w", stdout);
#endif
}
//--------------------------------

void ckmin(ll &x, ll y){x=min(x,y);}

const ll INF=1e18;
const int MX=5e4+10;

int N,M,U[MX],V[MX],W[MX],C[MX];

vector<pair<pi,int>>adj[MX];
void build(){
	FOR(i,1,N+1) adj[i].clear();
	FOR(i,0,M){
		int u=U[i],v=V[i],w=W[i];
		adj[u].pb({{v,w},i});
	}
}

ll dist[MX];
int par[MX],par_idx[MX];
void dijkstra(int s){
	FOR(i,1,N+1) dist[i]=INF;
	dist[s]=0;

	priority_queue<pair<ll,int>,vector<pair<ll,int>>,greater<pair<ll,int>>>q;
	q.push({0,s});

	while(sz(q)){
		int u=q.top().se; 
		ll d=q.top().fi;
		q.pop();

		if(dist[u]<d) continue;

		for(auto it: adj[u]){
			int v=it.fi.fi,w=it.fi.se,idx=it.se; 
			if(dist[v]>d+w){
				dist[v]=d+w; 
				par[v]=u; 
				par_idx[v]=idx;
				q.push({dist[v],v});
			}
		}
	}
}

vi chosen(MX,0);
ll to1[MX],toN[MX],from1[MX],fromN[MX];
void precompute(){
	build();

	dijkstra(1);
	FOR(i,1,N+1) from1[i]=dist[i];
	//FOR(i,1,N+1) cout << dist[i] << ' '; cout << endl;

	int u=N; 
	while(dist[N]!=INF && u!=1){
		chosen[par_idx[u]]=1;
		u=par[u];
	}

	dijkstra(N);
	FOR(i,1,N+1) fromN[i]=dist[i];

	u=1;
	while(dist[1]!=INF && u!=N){
		chosen[par_idx[u]]=1;
		u=par[u];
	}
	

	FOR(i,0,M) swap(U[i],V[i]);
	build();
	FOR(i,0,M) swap(U[i],V[i]);

	dijkstra(1);
	FOR(i,1,N+1) to1[i]=dist[i];

	dijkstra(N);
	FOR(i,1,N+1) toN[i]=dist[i];
}

int main(){
	IO();

	cin>>N>>M;
	FOR(i,0,M) cin>>U[i]>>V[i]>>W[i]>>C[i];

	precompute();

	ll ans=toN[1]+to1[N]; 
	FOR(i,0,M){
		if(chosen[i]){
			swap(U[i],V[i]); 
			build();
			swap(U[i],V[i]); 

			ll x=C[i];
			dijkstra(1);
			x+=dist[N];
			dijkstra(N);
			x+=dist[1];
			ckmin(ans,x);
		}
		else{
			int u=U[i],v=V[i];
			swap(u,v);

			ckmin(ans,from1[u]+W[i]+C[i]+toN[v]+fromN[1]);
			ckmin(ans,from1[N]+fromN[u]+C[i]+W[i]+to1[v]);
			ckmin(ans,from1[u]+W[i]+C[i]+toN[v]
				+fromN[u]+W[i]+to1[v]);
		}
	}

	if(ans>=INF) ans=-1;
	cout << ans << endl;
}

# Verdict Execution time Memory Grader output
1 Correct 4 ms 1708 KB Output is correct
2 Correct 2 ms 1740 KB Output is correct
3 Correct 3 ms 1740 KB Output is correct
4 Correct 4 ms 1704 KB Output is correct
5 Correct 3 ms 1740 KB Output is correct
6 Correct 2 ms 1740 KB Output is correct
7 Correct 2 ms 1612 KB Output is correct
8 Correct 2 ms 1740 KB Output is correct
9 Correct 3 ms 1740 KB Output is correct
10 Correct 28 ms 1716 KB Output is correct
11 Correct 29 ms 1740 KB Output is correct
12 Correct 28 ms 1728 KB Output is correct
13 Correct 3 ms 1740 KB Output is correct
14 Correct 4 ms 1704 KB Output is correct
15 Correct 3 ms 1740 KB Output is correct
16 Correct 3 ms 1708 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 70 ms 4612 KB Output is correct
2 Correct 68 ms 4676 KB Output is correct
3 Correct 73 ms 4812 KB Output is correct
4 Correct 4 ms 1868 KB Output is correct
5 Correct 3 ms 1744 KB Output is correct
6 Correct 2 ms 1740 KB Output is correct
7 Correct 2 ms 1740 KB Output is correct
8 Correct 1 ms 1612 KB Output is correct
9 Correct 76 ms 4972 KB Output is correct
10 Correct 66 ms 4948 KB Output is correct
11 Correct 88 ms 4800 KB Output is correct
12 Correct 72 ms 4816 KB Output is correct
13 Correct 75 ms 4800 KB Output is correct
14 Correct 74 ms 4756 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 1740 KB Output is correct
2 Correct 2 ms 1740 KB Output is correct
3 Correct 52 ms 3680 KB Output is correct
4 Correct 2 ms 1740 KB Output is correct
5 Correct 77 ms 4468 KB Output is correct
6 Correct 2 ms 1612 KB Output is correct
7 Correct 2 ms 1740 KB Output is correct
8 Correct 60 ms 4700 KB Output is correct
9 Correct 58 ms 4736 KB Output is correct
10 Correct 68 ms 4460 KB Output is correct
11 Correct 67 ms 4564 KB Output is correct
12 Correct 70 ms 4444 KB Output is correct
13 Correct 1 ms 1612 KB Output is correct
14 Correct 1 ms 1612 KB Output is correct
15 Correct 1 ms 1692 KB Output is correct
16 Correct 2 ms 1612 KB Output is correct
17 Correct 1 ms 1612 KB Output is correct
18 Correct 1 ms 1612 KB Output is correct
19 Correct 60 ms 4400 KB Output is correct
20 Correct 61 ms 4452 KB Output is correct
21 Correct 63 ms 4408 KB Output is correct
22 Correct 60 ms 4548 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 1708 KB Output is correct
2 Correct 2 ms 1740 KB Output is correct
3 Correct 3 ms 1740 KB Output is correct
4 Correct 4 ms 1704 KB Output is correct
5 Correct 3 ms 1740 KB Output is correct
6 Correct 2 ms 1740 KB Output is correct
7 Correct 2 ms 1612 KB Output is correct
8 Correct 2 ms 1740 KB Output is correct
9 Correct 3 ms 1740 KB Output is correct
10 Correct 28 ms 1716 KB Output is correct
11 Correct 29 ms 1740 KB Output is correct
12 Correct 28 ms 1728 KB Output is correct
13 Correct 3 ms 1740 KB Output is correct
14 Correct 4 ms 1704 KB Output is correct
15 Correct 3 ms 1740 KB Output is correct
16 Correct 3 ms 1708 KB Output is correct
17 Correct 70 ms 4612 KB Output is correct
18 Correct 68 ms 4676 KB Output is correct
19 Correct 73 ms 4812 KB Output is correct
20 Correct 4 ms 1868 KB Output is correct
21 Correct 3 ms 1744 KB Output is correct
22 Correct 2 ms 1740 KB Output is correct
23 Correct 2 ms 1740 KB Output is correct
24 Correct 1 ms 1612 KB Output is correct
25 Correct 76 ms 4972 KB Output is correct
26 Correct 66 ms 4948 KB Output is correct
27 Correct 88 ms 4800 KB Output is correct
28 Correct 72 ms 4816 KB Output is correct
29 Correct 75 ms 4800 KB Output is correct
30 Correct 74 ms 4756 KB Output is correct
31 Correct 4 ms 1740 KB Output is correct
32 Correct 2 ms 1740 KB Output is correct
33 Correct 52 ms 3680 KB Output is correct
34 Correct 2 ms 1740 KB Output is correct
35 Correct 77 ms 4468 KB Output is correct
36 Correct 2 ms 1612 KB Output is correct
37 Correct 2 ms 1740 KB Output is correct
38 Correct 60 ms 4700 KB Output is correct
39 Correct 58 ms 4736 KB Output is correct
40 Correct 68 ms 4460 KB Output is correct
41 Correct 67 ms 4564 KB Output is correct
42 Correct 70 ms 4444 KB Output is correct
43 Correct 1 ms 1612 KB Output is correct
44 Correct 1 ms 1612 KB Output is correct
45 Correct 1 ms 1692 KB Output is correct
46 Correct 2 ms 1612 KB Output is correct
47 Correct 1 ms 1612 KB Output is correct
48 Correct 1 ms 1612 KB Output is correct
49 Correct 60 ms 4400 KB Output is correct
50 Correct 61 ms 4452 KB Output is correct
51 Correct 63 ms 4408 KB Output is correct
52 Correct 60 ms 4548 KB Output is correct
53 Correct 71 ms 4648 KB Output is correct
54 Correct 74 ms 4508 KB Output is correct
55 Correct 74 ms 4576 KB Output is correct
56 Correct 4 ms 1740 KB Output is correct
57 Correct 4 ms 1728 KB Output is correct
58 Correct 220 ms 3912 KB Output is correct
59 Correct 255 ms 3896 KB Output is correct
60 Correct 328 ms 3908 KB Output is correct
61 Correct 210 ms 3780 KB Output is correct
62 Correct 233 ms 3904 KB Output is correct
63 Correct 332 ms 3892 KB Output is correct
64 Correct 844 ms 4852 KB Output is correct
65 Correct 927 ms 4756 KB Output is correct
66 Execution timed out 1071 ms 4892 KB Time limit exceeded
67 Halted 0 ms 0 KB -