Submission #211528

# Submission time Handle Problem Language Result Execution time Memory
211528 2020-03-20T16:51:05 Z kshitij_sodani Olympic Bus (JOI20_ho_t4) C++17
0 / 100
1000 ms 132328 KB
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
//typedef  long long int;
#define mp make_pair
#define pb push_back
#define a first
#define b second
vector<pair<int,int>> adj[201];
vector<int> cost[201];
int dis[201][201];
int yes[201][201];
map<pair<int,int>,int> dob;

int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	int n,m;
	memset(dis,-1,sizeof(dis));
	memset(yes,0,sizeof(yes));
	cin>>n>>m;

	int ac,bb,cc,dd;
	
	for(int i=0;i<m;i++){
		cin>>ac>>bb>>cc>>dd;
		adj[ac-1].pb({bb-1,cc});
		cost[ac-1].pb(dd);
		if(dis[ac-1][bb-1]==-1){
			dis[ac-1][bb-1]=cc;
		}
		dis[ac-1][bb-1]=min(dis[ac-1][bb-1],cc);
	}
	for(int i=0;i<n;i++){
		dis[i][i]=0;
	}

	for(int k=0;k<n;k++){
		for(int i=0;i<n;i++){
			for(int j=0;j<n;j++){
				if(dis[i][k]!=-1 and dis[k][j]!=-1){
					if(dis[i][j]==-1){
						dis[i][j]=dis[i][k]+dis[k][j];
					}
					else{
						dis[i][j]=min(dis[i][j],dis[i][k]+dis[k][j]);
					}
				}
			}
		}
	}
	
	int ans=-1;
	if(dis[0][n-1]>-1 and dis[n-1][0]>-1){
		ans=dis[0][n-1]+dis[n-1][0];
	}
	priority_queue<pair<int,int>> cd;
	int dist3[n];
	pair<int,int> par[n];
	for(int i=0;i<n;i++){
		dist3[i]=-1;
	}
	dist3[0]=0;
	cd.push({0,0});

	while(!cd.empty()){
		pair<int,int> bb=cd.top();
		cd.pop();
		bb.a=-bb.a;
		int k=0;
		for(auto jj:adj[bb.b]){
			if(dist3[jj.a]==-1 or dist3[jj.a]>bb.a+jj.b ){
				dist3[jj.a]=bb.a+jj.b;
				cd.push({-dist3[jj.a],jj.a});
				par[jj.a]={bb.b,k};
			}
			k+=1;
		}
	}
	int tot5=0;
	if(dist3[n-1]>-1){
		int nn=n-1;
		while(nn!=0){
			yes[par[nn].a][par[nn].b]=1;
			nn=par[nn].a;
			tot5+=1;
		}
	}
	pair<int,int> par2[n];

	int dist4[n];

	for(int i=0;i<n;i++){
		dist4[i]=-1;
	}
	dist4[n-1]=0;
	cd.push({0,n-1});

	while(!cd.empty()){
		pair<int,int> bb=cd.top();
		cd.pop();

		bb.a=-bb.a;
//		cout<<bb.b<<" "<<bb.a<<endl;
		int k=0;
		for(auto jj:adj[bb.b]){
			if(dist4[jj.a]==-1 or dist4[jj.a]>bb.a+jj.b ){
				dist4[jj.a]=bb.a+jj.b;
				cd.push({-dist4[jj.a],jj.a});
				par[jj.a]={bb.b,k};
			}
			k+=1;
		}
	}
	if(dist4[0]>-1){
		int nn=0;
		while(nn!=n-1){
			yes[par[nn].a][par[nn].b]=1;
			nn=par[nn].a;
			tot5+=1;
		}
	}

	for(int i=0;i<n;i++){
		for(int j=0;j<adj[i].size();j++){
			int nn=adj[i][j].a;
			if(yes[i][j]==0){
				if(dis[i][0]>-1 and dis[0][n-1]>-1 and dis[n-1][nn]>-1){

					if(ans==-1){
						ans=dis[i][0]+cost[i][j]+dis[n-1][nn]+dis[0][n-1]+adj[i][j].b;
					}
					ans=min(ans,dis[i][0]+cost[i][j]+dis[n-1][nn]+dis[0][n-1]+adj[i][j].b);
				}
				if(dis[i][n-1]>-1 and dis[n-1][0]>-1 and dis[0][nn]>-1){

					if(ans==-1){
						ans=dis[i][n-1]+cost[i][j]+dis[0][nn]+dis[n-1][0]+adj[i][j].b;
					}
					ans=min(ans,dis[i][n-1]+cost[i][j]+dis[0][nn]+dis[n-1][0]+adj[i][j].b);
				}
				if(dis[0][nn]>-1 and dis[n-1][nn]>-1 and dis[i][0]>-1 and dis[i][n-1]>0){
					if(ans==-1){
						ans=dis[0][nn]+dis[n-1][nn]+dis[i][0]+dis[i][n-1]+adj[i][j].b*2+cost[i][j];
					}
					ans=min(ans,dis[0][nn]+dis[n-1][nn]+dis[i][0]+dis[i][n-1]+adj[i][j].b*2+cost[i][j]);
				}
			}
			else{
				priority_queue<pair<int,int>> cc;
				int dist[n];
				for(int i=0;i<n;i++){
					dist[i]=-1;
				}
				dist[0]=0;
				cc.push({0,0});
				while(!cc.empty()){
					pair<int,int> bb=cc.top();
					cc.pop();

					bb.a=-bb.a;
			//		cout<<bb.b<<" "<<bb.a<<endl;
					for(auto jj:adj[bb.b]){
						if(dist[jj.a]==-1 or dist[jj.a]>bb.a+jj.b ){
							if(bb.b!=i or jj.a!=nn){
								dist[jj.a]=bb.a+jj.b;
								cc.push({-dist[jj.a],jj.a});
							}
						}
					}
					if(bb.b==nn){
						if(dist[i]==-1 or dist[i]>dist[bb.b]+adj[i][j].b){
							dist[i]=dist[bb.b]+adj[i][j].b;
							cc.push({-dist[i],i});
						}
					}
				}
				int dist2[n];
				for(int i=0;i<n;i++){
					dist2[i]=-1;
				}
				dist2[n-1]=0;
				cc.push({0,n-1});
				while(!cc.empty()){
					pair<int,int> bb=cc.top();
					cc.pop();
					bb.a=-bb.a;
					for(auto jj:adj[bb.b]){
						if(dist2[jj.a]==-1 or dist2[jj.a]>bb.a+jj.b ){
							if(bb.b!=i or jj.a!=nn){
								dist2[jj.a]=bb.a+jj.b;
								cc.push({-dist2[jj.a],jj.a});
							}
						}
					}
					if(bb.b==nn){
						if(dist2[i]==-1 or dist2[i]>dist2[bb.b]+adj[i][j].b){
							dist2[i]=dist2[bb.a]+adj[i][j].b;
							cc.push({-dist2[i],i});
						}
					}
				}
				if(dist[n-1]>-1 and dist2[0]>-1){
					if(ans==-1){
						ans=dist[n-1]+dist2[0]+cost[i][j];
					}
					ans=min(ans,dist[n-1]+dist2[0]+cost[i][j]);
				}
			}
		}
	}
	cout<<ans<<endl;



	return 0;
}

Compilation message

ho_t4.cpp: In function 'int main()':
ho_t4.cpp:125:16: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for(int j=0;j<adj[i].size();j++){
               ~^~~~~~~~~~~~~~
ho_t4.cpp:89:16: warning: unused variable 'par2' [-Wunused-variable]
  pair<int,int> par2[n];
                ^~~~
# Verdict Execution time Memory Grader output
1 Execution timed out 1101 ms 132264 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 45 ms 3072 KB Execution killed with signal 11 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 1100 ms 132328 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 1101 ms 132264 KB Time limit exceeded
2 Halted 0 ms 0 KB -