QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#447519 | #7404. Back and Forth | Crying | WA | 0ms | 3640kb | C++14 | 2.2kb | 2024-06-18 15:37:13 | 2024-06-18 15:37:13 |
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],vis[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;
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});
}
}
//
for(int x=1;x<=n;x++)for(int y=1;y<=n;y++)dp[x][y] = INF,vis[x][y] = 0;
//初始化
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 x = -1,y = -1;
for(int cx=1;cx<=n;cx++)for(int cy=1;cy<=n;cy++)if(!vis[cx][cy] && dp[cx][cy] != INF){
if(x==-1)x = cx,y = cy; else if(dp[cx][cy] < dp[x][y])x = cx,y = cy;
}
if(x==-1)break;
if(vis[x][y]++)continue;
int val = dp[x][y];
//printf("get dp[%d,%d] : %d\n",x,y,val);
//更新答案
tomin(ans,val+dis[s][x]+dis[y][s]-(p[x]+p[y]+p[s]));
//printf("now ans : %d\n",ans); if(ans<4)exit(0);
//更新其他的dp
for(int cx=1;cx<=n;cx++)for(int cy=1;cy<=n;cy++)if(chk(cx,cy)){
if(x==cx || x==cy || y==cx || y==cy)continue;
tomin(dp[cx][cy],val+dis[x][y]+dis[y][cx]+dis[cy][x]-(p[x]+p[y]+p[cx]+p[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: 3640kb
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: 0ms
memory: 3632kb
input:
1 2 0 1 2 1 1
output:
199999998
result:
wrong answer 1st numbers differ - expected: '-1', found: '199999998'