QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#615055#4235. TransparencyForever_YoungWA 0ms4104kbC++232.9kb2024-10-05 17:31:152024-10-05 17:31:15

Judging History

你现在查看的是最新测评结果

  • [2024-10-05 17:31:15]
  • 评测
  • 测评结果:WA
  • 用时:0ms
  • 内存:4104kb
  • [2024-10-05 17:31:15]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=1;i<=n;i++)
#define pb push_back
#define mp make_pair
#define data dataa
using LL=long long;
using ULL=unsigned long long;
using LD=long double;
const int N=55;
priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>>q;
queue<int>Q;
vector<pair<int,int>>g[N*N];
int id[N][N],s,p,t,terminal[N],dis[N*N],len[N],go[N][55],tot,len2[N],cir[N];
bool vis[N*N];
void add(int x,int y,int z){g[x].pb(mp(y,z));}
void bfs(int S,int len[],bool can_cap)
{
    rep(i,s)len[i]=-1;
    Q.push(S);len[S]=0;
    for(;!Q.empty();)
    {
        int u=Q.front();Q.pop();
        for(int i=0;i<52;i++)if(go[u][i])
        {
            if(!can_cap&&i<26)continue;
            int x=go[u][i];
            if(len[x]!=-1)continue;
            len[x]=len[u]+1;
            Q.push(x);
        }
    }
}
int main()
{
    scanf("%d%d%d",&s,&p,&t);
    rep(i,p)scanf("%d",&terminal[i]);
    rep(i,t)
    {
        int x,y;char s[2];
        scanf("%d%s%d",&x,s,&y);
        char c=s[0];
        int typ;
        if('A'<=c&&c<='Z')typ=c-'A';else typ=c-'a'+26;
        go[x][typ]=y;
    }
    rep(i,s)rep(j,s)id[i][j]=++tot;
    rep(i,s)rep(j,s)
    {
        for(int k=26;k<52;k++)if(go[i][k])add(id[go[i][k]][j],id[i][j],1);
        for(int k=26;k<52;k++)if(go[j][k])add(id[i][go[j][k]],id[i][j],1);
        for(int k=0;k<26;k++)if(go[i][k]&&go[j][k])add(id[go[i][k]][go[j][k]],id[i][j],2);
    }
    rep(i,tot)dis[i]=-1;
    rep(i,p)rep(j,p)
    {
        int x=id[terminal[i]][terminal[j]];
        dis[x]=0;
        q.push(mp(0,x));
    }
    for(;!q.empty();)
    {
        int u=q.top().second;q.pop();
        if(vis[u])continue;
        vis[u]=1;
        for(auto [x,y]:g[u])if(dis[x]==-1||dis[u]+y<dis[x])dis[x]=dis[u]+y,q.push(mp(dis[x],x));
    }
    rep(i,s)
    {
        bfs(i,len,0);
        cir[i]=-1;
        rep(j,s)for(int k=26;k<52;k++)if(go[j][k]==i&&j!=s&&len[j]!=-1&&(cir[i]==-1||cir[i]>len[j]+1))cir[i]=len[j]+1;
    }
    bfs(1,len,1);
    int ans=-1;
    rep(i,s)
    {
        if(len[i]==-1)continue;
        for(int j=26;j<52;j++)for(int k=26;k<52;k++)
        {
            if(j==k)continue;
            if(!go[i][j]||!go[i][k])continue;
            int x=go[i][j],y=go[i][k],now=id[x][y];
            if(dis[now]==-1)continue;
            int tans=len[i]*2+dis[now]+2;
            if(ans==-1||tans<ans)ans=tans;
        }
        if(cir[i]!=-1&&dis[id[i][i]]!=-1&&(ans==-1||ans>len[i]*2+cir[i]+dis[id[i][i]]))ans=len[i]*2+cir[i]+dis[id[i][i]];
        bfs(i,len2,0);
        for(int j=0;j<26;j++)if(go[i][j])rep(k,s)if(go[k][j])
        {
            if(k==i)continue;
            if(len2[k]==-1)continue;
            int x=id[go[i][j]][go[k][j]];
            if(dis[x]==-1)continue;
            int tans=len[i]*2+len2[k]+2+dis[x];
            if(ans==-1||ans>tans)ans=tans;
        }
    }
    printf("%d\n",ans);
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 3880kb

input:

4 1 8
3
1 F 1
1 A 2
2 b 1
2 F 3
2 d 3
3 B 3
3 y 4
4 d 1

output:

10

result:

ok single line: '10'

Test #2:

score: 0
Accepted
time: 0ms
memory: 4084kb

input:

4 1 8
3
1 F 1
1 A 2
2 b 1
2 F 3
2 d 3
3 B 3
3 y 4
4 d 4

output:

-1

result:

ok single line: '-1'

Test #3:

score: 0
Accepted
time: 0ms
memory: 3832kb

input:

5 2 5
1
5
1 i 2
2 c 3
3 p 4
4 c 1
1 I 5

output:

4

result:

ok single line: '4'

Test #4:

score: 0
Accepted
time: 0ms
memory: 3764kb

input:

8 2 96
4
8
1 x 2
1 y 5
2 A 3
3 A 4
4 A 2
5 A 6
6 A 7
7 A 8
8 A 5
5 B 2
6 B 2
7 B 2
8 B 2
5 C 2
6 C 2
7 C 2
8 C 2
5 D 2
6 D 2
7 D 2
8 D 2
5 E 2
6 E 2
7 E 2
8 E 2
5 F 2
6 F 2
7 F 2
8 F 2
5 G 2
6 G 2
7 G 2
8 G 2
5 H 2
6 H 2
7 H 2
8 H 2
5 I 2
6 I 2
7 I 2
8 I 2
5 J 2
6 J 2
7 J 2
8 J 2
5 K 2
6 K 2
7 K 2
8...

output:

24

result:

ok single line: '24'

Test #5:

score: 0
Accepted
time: 0ms
memory: 3824kb

input:

4 1 4
1
1 d 2
2 A 3
3 A 4
4 d 1

output:

-1

result:

ok single line: '-1'

Test #6:

score: 0
Accepted
time: 0ms
memory: 4104kb

input:

7 1 8
1
1 d 2
2 A 3
3 A 4
4 d 1
1 A 5
5 d 6
6 d 7
7 A 1

output:

8

result:

ok single line: '8'

Test #7:

score: -100
Wrong Answer
time: 0ms
memory: 3876kb

input:

1 1 1
1
1 a 1

output:

-1

result:

wrong answer 1st lines differ - expected: '1', found: '-1'