Submission #417313

# Submission time Handle Problem Language Result Execution time Memory
417313 2021-06-03T14:42:28 Z AmineWeslati Olympic Bus (JOI20_ho_t4) C++14
37 / 100
1000 ms 4860 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(int &x, int y){x=min(x,y);}

const int INF=1e9+100;
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});
	}
}

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

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

	while(sz(q)){
		int u=q.top().se,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);
int to1[MX],toN[MX],from1[MX],fromN[MX];
void precompute(){
	build();

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

	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();

	int ans=2e9;
	if(toN[1]!=INF && to1[N]!=INF) ans=toN[1]+to1[N]; 
	FOR(i,0,M){
		if(chosen[i]){
			swap(U[i],V[i]); 
			build();
			swap(U[i],V[i]); 

			bool yes=1;

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

			bool c1=(max(max(from1[u],toN[v]),fromN[1])!=INF),
			c2=(max(max(from1[N],to1[v]),fromN[u])!=INF),
			c3=(max(max(from1[u],toN[v]),max(fromN[u],to1[v]))!=INF);

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

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

# Verdict Execution time Memory Grader output
1 Correct 4 ms 1740 KB Output is correct
2 Correct 1 ms 1708 KB Output is correct
3 Correct 4 ms 1740 KB Output is correct
4 Correct 5 ms 1680 KB Output is correct
5 Correct 3 ms 1740 KB Output is correct
6 Correct 1 ms 1740 KB Output is correct
7 Correct 1 ms 1612 KB Output is correct
8 Correct 1 ms 1612 KB Output is correct
9 Correct 2 ms 1740 KB Output is correct
10 Correct 22 ms 1772 KB Output is correct
11 Correct 30 ms 1740 KB Output is correct
12 Correct 27 ms 1764 KB Output is correct
13 Correct 3 ms 1740 KB Output is correct
14 Correct 3 ms 1740 KB Output is correct
15 Correct 3 ms 1740 KB Output is correct
16 Correct 3 ms 1740 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 71 ms 3552 KB Output is correct
2 Correct 68 ms 3676 KB Output is correct
3 Correct 74 ms 3580 KB Output is correct
4 Correct 6 ms 1712 KB Output is correct
5 Correct 4 ms 1740 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 71 ms 3820 KB Output is correct
10 Correct 68 ms 3756 KB Output is correct
11 Correct 78 ms 3608 KB Output is correct
12 Correct 73 ms 3624 KB Output is correct
13 Correct 71 ms 3556 KB Output is correct
14 Correct 75 ms 3560 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 1740 KB Output is correct
2 Correct 2 ms 1740 KB Output is correct
3 Correct 49 ms 3660 KB Output is correct
4 Correct 2 ms 1740 KB Output is correct
5 Correct 60 ms 4456 KB Output is correct
6 Correct 2 ms 1696 KB Output is correct
7 Correct 2 ms 1612 KB Output is correct
8 Correct 59 ms 4672 KB Output is correct
9 Correct 59 ms 4768 KB Output is correct
10 Correct 62 ms 4504 KB Output is correct
11 Correct 60 ms 4492 KB Output is correct
12 Correct 64 ms 4388 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 1 ms 1612 KB Output is correct
17 Correct 1 ms 1612 KB Output is correct
18 Correct 1 ms 1700 KB Output is correct
19 Correct 63 ms 4396 KB Output is correct
20 Correct 64 ms 4452 KB Output is correct
21 Correct 62 ms 4420 KB Output is correct
22 Correct 62 ms 4436 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 1740 KB Output is correct
2 Correct 1 ms 1708 KB Output is correct
3 Correct 4 ms 1740 KB Output is correct
4 Correct 5 ms 1680 KB Output is correct
5 Correct 3 ms 1740 KB Output is correct
6 Correct 1 ms 1740 KB Output is correct
7 Correct 1 ms 1612 KB Output is correct
8 Correct 1 ms 1612 KB Output is correct
9 Correct 2 ms 1740 KB Output is correct
10 Correct 22 ms 1772 KB Output is correct
11 Correct 30 ms 1740 KB Output is correct
12 Correct 27 ms 1764 KB Output is correct
13 Correct 3 ms 1740 KB Output is correct
14 Correct 3 ms 1740 KB Output is correct
15 Correct 3 ms 1740 KB Output is correct
16 Correct 3 ms 1740 KB Output is correct
17 Correct 71 ms 3552 KB Output is correct
18 Correct 68 ms 3676 KB Output is correct
19 Correct 74 ms 3580 KB Output is correct
20 Correct 6 ms 1712 KB Output is correct
21 Correct 4 ms 1740 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 71 ms 3820 KB Output is correct
26 Correct 68 ms 3756 KB Output is correct
27 Correct 78 ms 3608 KB Output is correct
28 Correct 73 ms 3624 KB Output is correct
29 Correct 71 ms 3556 KB Output is correct
30 Correct 75 ms 3560 KB Output is correct
31 Correct 3 ms 1740 KB Output is correct
32 Correct 2 ms 1740 KB Output is correct
33 Correct 49 ms 3660 KB Output is correct
34 Correct 2 ms 1740 KB Output is correct
35 Correct 60 ms 4456 KB Output is correct
36 Correct 2 ms 1696 KB Output is correct
37 Correct 2 ms 1612 KB Output is correct
38 Correct 59 ms 4672 KB Output is correct
39 Correct 59 ms 4768 KB Output is correct
40 Correct 62 ms 4504 KB Output is correct
41 Correct 60 ms 4492 KB Output is correct
42 Correct 64 ms 4388 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 1 ms 1612 KB Output is correct
47 Correct 1 ms 1612 KB Output is correct
48 Correct 1 ms 1700 KB Output is correct
49 Correct 63 ms 4396 KB Output is correct
50 Correct 64 ms 4452 KB Output is correct
51 Correct 62 ms 4420 KB Output is correct
52 Correct 62 ms 4436 KB Output is correct
53 Correct 68 ms 4528 KB Output is correct
54 Correct 73 ms 4548 KB Output is correct
55 Correct 72 ms 4672 KB Output is correct
56 Correct 4 ms 1712 KB Output is correct
57 Correct 3 ms 1740 KB Output is correct
58 Correct 209 ms 3864 KB Output is correct
59 Correct 231 ms 3876 KB Output is correct
60 Correct 316 ms 3884 KB Output is correct
61 Correct 207 ms 3888 KB Output is correct
62 Correct 232 ms 4008 KB Output is correct
63 Correct 316 ms 3752 KB Output is correct
64 Correct 774 ms 4736 KB Output is correct
65 Correct 827 ms 4632 KB Output is correct
66 Execution timed out 1075 ms 4860 KB Time limit exceeded
67 Halted 0 ms 0 KB -