QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#19850 | #2289. Jail or Joyride | IAKIOI# | WA | 4ms | 5824kb | C++14 | 2.8kb | 2022-02-12 14:39:18 | 2022-05-06 07:21:35 |
Judging History
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 ){
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;
}
详细
Test #1:
score: 100
Accepted
time: 4ms
memory: 5676kb
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: -100
Wrong Answer
time: 4ms
memory: 5824kb
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:
1772
result:
wrong answer 1st lines differ - expected: '227641', found: '1772'