Submission #211724

# Submission time Handle Problem Language Result Execution time Memory
211724 2020-03-21T05:16:58 Z kshitij_sodani Olympic Bus (JOI20_ho_t4) C++17
0 / 100
897 ms 262148 KB
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
typedef  long long llo;
#define mp make_pair
#define pb push_back
#define a first
#define b second
vector<pair<int,llo>> adj[201];
vector<llo> cost[201];
llo dis[201][201];
int yes[201][201];

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;
	llo cc,dd;
	int ac,bb;
	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]);
					}
				}
			}
		}
	}
	
	llo 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<llo,int>> cd;
	llo dist3[n];
	pair<int,llo> par[n];
	for(int i=0;i<n;i++){
		dist3[i]=-1;
	}
	dist3[0]=0;
	cd.push({0,0});

	while(cd.size()){
		pair<llo,int> bb=cd.top();
		cd.pop();
		bb.a=-bb.a;
		for(int k=0;k<adj[bb.b].size();k++){
			pair<int,int> jj=adj[bb.b][k];
			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};
			}
		}
	}
	if(dist3[n-1]>-1){
		llo nn=n-1;
		while(nn!=0){
			yes[par[nn].a][par[nn].b]=1;
			nn=par[nn].a;
		}
	}
	pair<int,llo> par2[n];

	llo dist4[n];

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

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

		bb.a=-bb.a;
		for(int k=0;k<adj[bb.b].size();k++){
			pair<int,int> jj=adj[bb.b][k];
			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});
				par2[jj.a]={bb.b,k};
			}
		}
	}
	if(dist4[0]>-1){
		int nn=0;
		while(nn!=n-1){
			yes[par2[nn].a][par2[nn].b]=1;
			nn=par2[nn].a;
		}
	}
	int io=0;
	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{
				io+=1;
				if(io>2*n){
					while(true){
						continue;
					}
				}
				priority_queue<pair<llo,int>> cc;
				llo dist[n];
				for(int i=0;i<n;i++){
					dist[i]=-1;
				}
				dist[0]=0;
				cc.push({0,0});
				while(cc.size()){
					pair<llo,int> bb=cc.top();
					cc.pop();
					bb.a=-bb.a;
					for(int jjj=0;jjj<adj[bb.b].size();jjj++){
						pair<int,llo> jj=adj[bb.b][jjj];
				//	for(auto jj:adj[bb.b]){
						if(bb.b==i and jj.a==nn){
							continue;
						}
						if(dist[jj.a]==-1 or dist[jj.a]>bb.a+jj.b ){
								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});
						}
					}
				}
				llo dist2[n];
				for(int i=0;i<n;i++){
					dist2[i]=-1;
				}
				dist2[n-1]=0;
				cc.push({0,n-1});
				while(cc.size()){
					pair<llo,int> bb=cc.top();
					cc.pop();
					bb.a=-bb.a;
					for(int jjj=0;jjj<adj[bb.b].size();jjj++){
						pair<int,llo> jj=adj[bb.b][jjj];
				//	for(auto jj:adj[bb.b]){
						if(bb.b==i and jj.a==nn){
							continue;
						}
						if(dist2[jj.a]==-1 or dist2[jj.a]>bb.a+jj.b ){
							
								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:68:16: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for(int k=0;k<adj[bb.b].size();k++){
               ~^~~~~~~~~~~~~~~~~
ho_t4.cpp:99:16: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for(int k=0;k<adj[bb.b].size();k++){
               ~^~~~~~~~~~~~~~~~~
ho_t4.cpp:117:16: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for(int j=0;j<adj[i].size();j++){
               ~^~~~~~~~~~~~~~
ho_t4.cpp:159:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
      for(int jjj=0;jjj<adj[bb.b].size();jjj++){
                    ~~~^~~~~~~~~~~~~~~~~
ho_t4.cpp:188:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
      for(int jjj=0;jjj<adj[bb.b].size();jjj++){
                    ~~~^~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Runtime error 817 ms 262148 KB Execution killed with signal 9 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 45 ms 5368 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 Runtime error 897 ms 262148 KB Execution killed with signal 9 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 817 ms 262148 KB Execution killed with signal 9 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -