QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#211530 | #7404. Back and Forth | SolitaryDream# | WA | 1ms | 3704kb | C++17 | 2.1kb | 2023-10-12 17:55:16 | 2023-10-12 17:55:17 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
using tii=tuple<int,int,int>;
using pti=pair<int,tuple<int,int,int> >;
const int N=211;
int n,T,m,s,t;
int f[N][N],p[N];
int d[N][N][2];
priority_queue<pti,vector<pti>,greater<pti> >q;
int main()
{
scanf("%d",&T);
while(T--)
{
scanf("%d%d%d%d",&n,&m,&s,&t);
for(int i=1;i<=n;i++)
scanf("%d",&p[i]);
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
f[i][j]=(i==j?0:1e9);
for(int i=1;i<=m;i++)
{
int u,v;
scanf("%d%d",&u,&v);
f[u][v]=0;
}
for(int k=1;k<=n;k++)
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
f[i][j]=min(f[i][j],f[i][k]+f[k][j]+p[k]);
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
for(int k=0;k<2;k++)
d[i][j][k]=1e8;
d[s][s][0]=p[s];
q.push({p[s],{s,s,0}});
while(!q.empty())
{
auto [dis,Z]=q.top();
q.pop();
auto [x,y,z]=Z;
if(dis!=d[x][y][z])
continue;
for(int u=1;u<=n;u++)
{
if(z==0)
{
int nx=x;
int ny=u;
int nz=z^1;
if(d[nx][ny][nz]>d[x][y][z]+f[y][u]+p[u]*(u!=y))
d[nx][ny][nz]=d[x][y][z]+f[y][u]+p[u]*(u!=y),q.push({d[nx][ny][nz],{nx,ny,nz}});
}
else
{
int nx=y;
int ny=u;
int nz=z^1;
if(d[nx][ny][nz]>d[x][y][z]+f[u][x]+f[y][u]+p[u]*(u!=y))
{
d[nx][ny][nz]=d[x][y][z]+f[u][x]+f[y][u]+p[u]*(u!=y),q.push({d[nx][ny][nz],{nx,ny,nz}});
}
}
}
}
int ans=d[t][t][0];
if(ans>1e8)
puts("-1");
else
printf("%d\n",ans);
}
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 3704kb
input:
3 4 5 1 4 1 1 1 1 1 2 2 3 3 1 4 2 3 4 4 4 1 2 1 1 1 1 1 2 2 3 3 4 4 1 4 8 1 3 1 100 1 1 1 2 2 1 2 3 3 2 1 4 4 1 3 4 4 3
output:
4 4 3
result:
ok 3 number(s): "4 4 3"
Test #2:
score: -100
Wrong Answer
time: 1ms
memory: 3624kb
input:
1 2 0 1 2 1 1
output:
100000000
result:
wrong answer 1st numbers differ - expected: '-1', found: '100000000'