QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#447558 | #7404. Back and Forth | Crying | TL | 145ms | 3932kb | C++14 | 2.6kb | 2024-06-18 16:22:01 | 2024-06-18 16:22:01 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
typedef long ll;
typedef array<int,2> pr;
const int MAXN = 210,INF = 1e8;
void tomin(int& x,int y){x = min(x,y);}
void tomax(int& x,int y){x = max(x,y);}
int T,n,m,s,t,p[MAXN];
vector<int> e[MAXN];
int dis[MAXN][MAXN],tag[MAXN];
int dp[MAXN][MAXN],tmp[MAXN][MAXN],vis[MAXN][MAXN],visg[MAXN][MAXN],ans;
int chk(int x,int y){return x!=s && x!=t && y!=s && y!=t;}
void solve(){
cin>>n>>m>>s>>t; ans = INF;
for(int i=1;i<=n;i++)cin>>p[i],e[i].clear();
for(int i=1,u,v;i<=m;i++)cin>>u>>v,e[u].push_back(v);
assert(s != t);
for(int i=1;i<=n;i++){
int* f = dis[i]; fill(f+1,f+1+n,INF); fill(tag+1,tag+1+n,0);
priority_queue<pr> pq;
f[i] = p[i],pq.push((pr){-f[i],i});
while(pq.size()){
int u = pq.top()[1]; pq.pop(); if(tag[u]++)continue;
for(auto v : e[u])if(f[v] > f[u]+p[v])f[v] = f[u]+p[v],pq.push((pr){-f[v],v});
}
}
if(dis[s][t] == INF || dis[t][s] == INF)return void(cout<<"-1\n");
//
for(int x=1;x<=n;x++)for(int y=1;y<=n;y++)dp[x][y] = INF,vis[x][y] = 0;
for(int x=1;x<=n;x++)for(int y=1;y<=n;y++)tmp[x][y] = INF,visg[x][y] = 0;
//初始化
tomin(ans,dis[s][t] + dis[t][s] - (p[s] + p[t]));
for(int x=1;x<=n;x++){
for(int y=1;y<=n;y++)if(chk(x,y) && x!=y){
tomin(dp[x][y],dis[x][y]+dis[y][t]+dis[t][x]-(p[x]+p[y]+p[t]));
}
if(chk(x,x))dp[x][x] = dis[x][t] + dis[t][x] - (p[x] + p[t]);
}
//dijkstra
while(1){
int vx = -1,vy = -1,wx = -1,wy = -1;
for(int x=1;x<=n;x++)for(int y=1;y<=n;y++)if(!vis[x][y] && dp[x][y] != INF){
if(vx==-1 || dp[vx][vy] > dp[x][y])vx = x,vy = y;
}
for(int x=1;x<=n;x++)for(int y=1;y<=n;y++)if(!visg[x][y] && tmp[x][y] != INF){
if(wx==-1 || tmp[wx][wy] > tmp[x][y])wx = x,wy = y;
}
if(vx==-1 && wx==-1)break;
if(wx==-1 || (vx!=-1 && dp[vx][vy] < tmp[wx][wy])){
int x = vx,y = vy,val = dp[x][y];
if(vis[x][y]++)continue;
tomin(ans,val+dis[s][x]+dis[y][s]-(p[x]+p[y]+p[s]));
//
for(int cx=1;cx<=n;cx++)if(chk(cx,y) && y!=cx){
tomin(tmp[x][cx],val+dis[y][cx]-p[y]-p[cx]);
}
}else{
int x = wx,cx = wy,val = tmp[x][cx];
if(visg[x][cx]++)continue;
for(int cy=1;cy<=n;cy++)if(chk(cx,cy) && x!=cy){
tomin(dp[cx][cy],val+dis[cy][x]-p[x]-p[cy]+dis[cx][cy]);
}
}
}
cout<<ans<<endl;
}
int main(){
cin>>T;
while(T--)solve();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3636kb
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: 0
Accepted
time: 0ms
memory: 3612kb
input:
1 2 0 1 2 1 1
output:
-1
result:
ok 1 number(s): "-1"
Test #3:
score: 0
Accepted
time: 8ms
memory: 3628kb
input:
10 20 40 5 16 2 1 1 1 2 1 1 2 1 1 1 2 1 1 1 2 1 2 1 2 7 2 8 5 15 4 17 13 11 6 9 2 12 13 12 1 1 8 2 20 6 9 18 3 2 15 10 12 4 17 11 5 19 15 15 9 14 7 11 2 1 15 16 1 13 3 11 16 5 10 19 6 1 3 13 14 20 11 6 19 7 9 20 12 17 18 13 9 4 18 3 19 13 17 19 9 19 10 8 6 20 40 10 1 2 2 2 2 1 2 1 2 2 2 2 2 1 2 2 1 ...
output:
15 14 11 12 15 8 9 13 12 12
result:
ok 10 numbers
Test #4:
score: 0
Accepted
time: 8ms
memory: 3588kb
input:
10 20 40 17 11 8 3 9 8 9 2 9 3 8 10 4 7 5 2 5 2 7 5 8 2 14 1 6 5 9 13 13 20 15 7 16 12 9 16 9 11 1 2 8 13 3 17 18 2 2 15 4 15 5 18 11 18 15 1 18 11 8 9 20 1 7 4 4 14 1 8 5 19 13 12 5 3 12 8 7 14 3 18 2 19 11 13 12 6 20 10 20 6 19 7 10 20 17 16 10 19 19 3 15 14 20 40 20 2 6 2 3 1 8 3 9 8 5 2 7 8 2 5 ...
output:
50 32 30 52 42 45 34 55 40 55
result:
ok 10 numbers
Test #5:
score: 0
Accepted
time: 8ms
memory: 3932kb
input:
10 20 40 18 6 66 8 72 32 88 52 22 28 50 62 1 73 83 81 14 82 50 90 90 87 13 10 11 18 16 12 20 8 11 15 3 12 15 2 14 13 17 7 2 5 18 3 4 2 15 4 2 15 7 9 2 1 17 14 14 18 15 8 16 1 4 17 14 19 5 8 20 11 7 5 19 16 6 10 1 4 14 20 5 6 12 13 8 13 2 18 10 17 9 20 12 15 1 5 7 6 16 18 9 3 20 40 10 20 46 66 84 50 ...
output:
525 239 429 400 328 517 229 391 664 376
result:
ok 10 numbers
Test #6:
score: 0
Accepted
time: 145ms
memory: 3760kb
input:
4 50 100 16 46 2 3 3 1 2 1 2 5 2 5 5 5 5 1 3 1 1 5 2 1 3 5 5 3 3 1 4 5 1 3 1 5 3 4 1 2 1 4 4 1 1 3 5 1 2 5 3 3 3 3 28 1 7 18 3 17 48 5 27 42 18 49 6 5 26 27 48 25 15 21 41 15 32 31 39 30 30 22 23 2 35 9 19 37 44 22 50 43 14 46 26 3 24 2 37 23 25 30 39 19 36 41 45 17 31 50 26 38 14 38 30 3 3 10 13 7 ...
output:
36 38 23 36
result:
ok 4 number(s): "36 38 23 36"
Test #7:
score: -100
Time Limit Exceeded
input:
1 200 300 169 170 2 1 2 1 1 2 1 1 2 2 2 2 2 1 2 2 2 1 1 2 2 2 2 2 2 1 2 1 2 1 2 2 2 2 2 1 2 2 2 1 1 2 2 2 1 2 2 2 2 2 1 1 2 2 1 2 2 2 1 2 2 2 1 2 1 1 1 2 2 1 2 2 2 2 2 1 2 2 2 2 1 1 2 2 1 2 1 1 2 2 1 1 1 2 1 2 2 1 2 1 1 1 1 2 2 1 2 2 2 1 1 1 2 1 2 1 1 1 1 1 1 1 2 2 1 2 2 1 1 2 1 2 2 2 1 1 1 2 1 2 1 ...