QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#19905 | #2289. Jail or Joyride | Realityang# | WA | 3ms | 3772kb | C++14 | 1.8kb | 2022-02-12 21:37:47 | 2022-05-08 01:33:44 |
Judging History
answer
#include<bits/stdc++.h>
#define int long long
using namespace std;
int num,vi[310],lj[310][310],an,di[310],d[310][310],d1[310][310],n,m,s,t,dd[310],ru[310],dis[310],vis[310];
queue<int>q;
void ed(){printf("impossible");exit(0);}
void bfs(int ss,int bd){
q.push(ss);vi[ss]=1;
while(!q.empty()){
int x=q.front();q.pop();
for(int i=1;i<=n;i++){
if(!vi[i]&&d[x][i]==di[x])vi[i]=1,q.push(i);
}
}
}
signed main(){
scanf("%lld%lld%lld%lld",&n,&m,&s,&t);
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++)d[i][j]=d1[i][j]=1e9;
d[i][i]=d1[i][i]=0;
}
for(int i=1,u,v,w;i<=m;i++){
scanf("%lld%lld%lld",&u,&v,&w);
d[u][v]=d[v][u]=w;ru[u]++;ru[v]++;
lj[u][v]=lj[v][u]=1;
}
for(int k=1;k<=n;k++){
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
d[i][j]=min(d[i][j],d[i][k]+d[k][j]);
}
}
}
if(d[s][t]==1e9)ed();
for(int i=1;i<=n;i++)for(int j=1;j<=n;j++)if(i!=j)di[i]=max(di[i],d[i][j]);
do{
num=0;
for(int i=1;i<=n;i++){
if(vis[i])continue;
int fl=1;
if(ru[i]==1){vis[i]=1;num++;continue;}
for(int j=1;j<=n;j++)if(i!=j&&di[i]==d[i][j]){
if(!vis[j])fl=0;
}num+=fl;
if(fl){vis[i]=1;
for(int j=1;j<=n;j++)if(i!=j&&di[i]==d[i][j]&&vis[j])dis[i]=max(dis[i],dis[j]+d[i][j]);
}
}
}while(num);
for(int k=1;k<=n;k++){
if(k==t)continue;
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
d1[i][j]=min(d1[i][j],d1[i][k]+d1[k][j]);
}
}
}int fl=1;
for(int i=1;i<=n;i++){
if(lj[i][t]&&d1[s][i]!=1e9){
for(int j=1;j<=n;j++)if(i!=j)vi[j]=0;
bfs(t,i);
for(int j=1;j<=n;j++){
if(i==j)continue;
if(d[t][j]==di[t]&&vi[j]){
if(!vis[j])fl=0;
else an=max(an,d[s][t]+d[t][j]+dis[j]);
}
}
}
}
if(fl)printf("%lld",an);
else ed();
}
详细
Test #1:
score: 100
Accepted
time: 3ms
memory: 3772kb
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: 1ms
memory: 3692kb
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:
impossible
result:
wrong answer 1st lines differ - expected: '227641', found: 'impossible'