QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#19851#2289. Jail or JoyrideIAKIOI#WA 4ms5836kbC++142.8kb2022-02-12 14:43:252022-05-06 07:21:38

Judging History

你现在查看的是最新测评结果

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2022-05-06 07:21:38]
  • 评测
  • 测评结果:WA
  • 用时:4ms
  • 内存:5836kb
  • [2022-02-12 14:43:25]
  • 提交

answer

#include<bits/stdc++.h>
#define rep(i,x,y) for(int i=x;i<=y;++i)
#define per(i,x,y) for(int i=x;i>=y;--i)
#define mar(o) for(int E=fst[o];E;E=e[E].nxt)
#define v e[E].to
#define lon long long
#define die(x,y) memset(x,y,sizeof x)
using namespace std;
const int n7=520,m7=501234;
const lon inf=1000000000000000;
struct dino{int to,nxt,w;}e[m7];
int n,m,me,mez,yo,ecnt,fst[n7],vis[n7],laf[n7],u[n7],ddis[n7];
lon dis[n7][n7],maxx,maxn,minx,minn,ans;

int rd(){
	int shu=0;bool fu=0;char ch=getchar();
	while( !isdigit(ch) ){if(ch=='-')fu=1;ch=getchar();}
	while( isdigit(ch) )shu=(shu<<1)+(shu<<3)+ch-'0',ch=getchar();
	return fu?-shu:shu;
}

void edge(int p,int q,int w){
	ecnt++;
	e[ecnt]=(dino){q,fst[p],w};
	fst[p]=ecnt;
}

void dfs0(int o){
	vis[o]=1;
	int flag=0;
	mar(o){
		flag++;
		if(vis[v])continue;
		dfs0(v);
	}
	if(flag<2)laf[o]=1;
}

int dfs1(int o,int w){
	vis[o]=1;
	int cnt=0;
	mar(o){
		if(vis[v])continue;
		if(v==yo){
			cnt++;
			if(w<minx)minx=w,mez=o;
		}
		else cnt+=dfs1(v,w+e[E].w);
		if(cnt>1)return cnt;
	}
	return cnt;
}

void dijk(){
	memset(ddis,0x3f,sizeof ddis),ddis[yo]=0;
	priority_queue < pair<lon,int> > que;
	que.push( make_pair(0,yo) );
	while( !que.empty() ){
		int o=que.top().second;que.pop();
		if(u[o])continue;
		u[o]=1;
		mar(o){
			if(v==me)continue;
			if(ddis[v]>ddis[o]+e[E].w){
				ddis[v]=ddis[o]+e[E].w;
				if(!u[v])que.push( make_pair(-ddis[v],v) );
			}
		}
	}
}	

bool work0(){
	mez=me;
	if( dfs1(me,0)>1 ){ans+=minx;return 1;}
	ans+=minx,me=mez;
	dijk();
	maxx=0;
	rep(i,1,n)if(ddis[i]^ddis[0]&&ddis[i]>maxx)maxx=ddis[i],maxn=i;
	yo=maxn;
	die(vis,0),minx=inf;
	if( dfs1(me,0)==1 ){
		mar(mez){
			if(v==yo)ans+=e[E].w;
		}
		printf("%lld\n",ans+minx);
		return 0;
	}
	return 1;
}

bool dfs3(int o,int fa){
	vis[o]=1;
	mar(o){
		if(v==fa)continue;
		if(vis[v])return 1;
		if( dfs3(v,o) )return 1;
	}
	return 0;
}

void dfs4(int o,int fa,int w){
	if(w>maxx)maxx=w,maxn=o;
	mar(o){
		if(v==fa)continue;
		dfs4(v,o,w+e[E].w);
	}
}

int main(){
	n=rd(),m=rd(),me=rd(),yo=rd();
	memset(dis,0x3f,sizeof dis);
	rep(i,1,n)dis[i][i]=0;
	rep(i,1,m){
		int p=rd(),q=rd();lon w=rd();
		edge(p,q,w),edge(q,p,w);
		dis[p][q]=dis[q][p]=min(dis[p][q],w);
	}
	die(vis,0),dfs0(1);
	die(vis,0),dfs0(2);
	die(vis,0);
	if( !work0() )return 0;
	die(fst,0),ecnt=0;
	rep(k,1,n)rep(i,1,n)rep(j,1,n){
		dis[i][j]=min(dis[i][j],dis[i][k]+dis[k][j]);
	}
	ans+=dis[me][yo];
	rep(i,1,n){
		if(laf[i])continue;
		int mxa=i;
		rep(j,1,n){
			if(dis[i][j]>dis[i][mxa])mxa=j;
			if(dis[i][j]==dis[i][mxa]&&!laf[j])mxa=j;
		}
		edge(i,mxa,dis[i][mxa]),edge(mxa,i,dis[i][mxa]);
	}
	die(vis,0);
	if( dfs3(yo,0) )return puts("impossible"),0;
	maxx=0,dfs4(yo,0,0);
	printf("%lld",ans+maxx);
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 4ms
memory: 5616kb

input:

9 10 1 2
1 2 225869
2 3 1772
3 4 314393
4 5 692250
5 6 684107
4 6 532792
3 7 441133
7 8 468183
8 9 186297
7 9 228792

output:

impossible

result:

ok single line: 'impossible'

Test #2:

score: 0
Accepted
time: 4ms
memory: 5808kb

input:

9 10 3 2
1 2 225869
2 3 1772
3 4 314393
4 5 692250
5 6 684107
4 6 532792
3 7 441133
7 8 468183
8 9 186297
7 9 228792

output:

227641

result:

ok single line: '227641'

Test #3:

score: -100
Wrong Answer
time: 2ms
memory: 5836kb

input:

8 22 8 1
1 2 11
1 3 11
1 4 11
1 5 11
1 6 11
1 7 10
2 3 12
2 4 12
2 5 12
2 6 12
2 7 11
3 4 13
3 5 13
3 6 13
3 7 12
4 5 14
4 6 14
4 7 13
5 6 15
5 7 14
6 7 15
7 8 1

output:

38

result:

wrong answer 1st lines differ - expected: '92', found: '38'