QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#847513#9802. Light Up the Gridzhouyf828TL 1ms3832kbC++172.0kb2025-01-08 03:04:272025-01-08 03:04:28

Judging History

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

  • [2025-01-08 03:04:28]
  • 评测
  • 测评结果:TL
  • 用时:1ms
  • 内存:3832kb
  • [2025-01-08 03:04:27]
  • 提交

answer

#include<iostream>
#include<cstring>
#include<map>
using namespace std;
const int inf=1e9+7;
int w[20][20];
int dp[(1<<20)][20];
int a0,a1,a2,a3,dj,b3;
map<int,int> mp;
map<string,int> mp2;
void init(){
    int zx=min(min(min(a0,a1),a2),a3);
    mp[0]=zx*2;mp[1]=a0;mp[2]=a0;mp[3]=a1;
    mp[4]=a0;mp[5]=a2;mp[6]=dj;mp[7]=b3;
    mp[8]=a0;mp[9]=dj;mp[10]=a2;mp[11]=b3;
    mp[12]=a1;mp[13]=b3;mp[14]=b3;mp[15]=a3;
    mp2["0000"]=0;mp2["0001"]=1;mp2["0010"]=2;mp2["0011"]=3;
    mp2["0100"]=4;mp2["0101"]=5;mp2["0110"]=6;mp2["0111"]=7;
    mp2["1000"]=8;mp2["1001"]=9;mp2["1010"]=10;mp2["1011"]=11;
    mp2["1100"]=12;mp2["1101"]=13;mp2["1110"]=14;mp2["1111"]=15;
    for(int i=0;i<16;i++){
        for(int j=0;j<16;j++){
            int s=mp[(i^j)];
            w[i][j]=s;
        }
    }
}
void solve(){
    int m;cin>>m;
    for(int i=0;i<=1<<(m+2)+1;i++){
        for(int j=0;j<=m+2;j++){
            dp[i][j]=inf;
        }
    }
    map<int,int> mp3;
    mp3[0]=15;
    for(int i=1;i<=m;i++){
        string s="";
        string x="";
        cin>>x;s+=x;
        cin>>x;s+=x;
        mp3[i]=mp2[s];
    }
    dp[1][0]=0;
    for(int i=0;i< 1<<(m+1);i++){//i表示所有的情况
        for(int j=0;j<=m;j++){//j表示走到哪一个点
            if(i>>j&1){
            for(int k=0;k<=m;k++)//k表示走到j这个点之前,以k为终点的最短距离
                if(i>>k&1){
                    dp[i][j]=min(dp[i][j],dp[i-(1<<j)][k]+w[mp3[k]][mp3[j]]);//更新最短距离
                }
            }
        }
    }
    int ans=inf;
    for(int i=0;i<=m;i++){
        ans=min(ans,dp[(1<<(m+1))-1][i]);
    }
    cout<<ans<<'\n';
}
int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);
    int t;cin>>t;
    cin>>a0>>a1>>a2>>a3;
    if(a1>a0*2) a1=a0*2;
    if(a2>a0*2) a2=a0*2;
    if(a3>a0*4) a3=a0*4;
    if(a3>a1*2) a3=a1*2;
    if(a3>a2*2) a3=a2*2;
    dj=min(2*a0,a1+a2);
    b3=min(min(a0+a3,a1+a0),a2+a0);
    init();
    while(t--){
        solve();
    }
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 3832kb

input:

2 1000 100 10 1
4
10
00

01
00

00
10

00
01
1
11
11

output:

1121
2

result:

ok 2 number(s): "1121 2"

Test #2:

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

input:

2 1 1 1 1
4
10
00

01
00

00
10

00
01
1
11
11

output:

5
2

result:

ok 2 number(s): "5 2"

Test #3:

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

input:

1 1000000 1000000 1000000 1000000
1
11
11

output:

2000000

result:

ok 1 number(s): "2000000"

Test #4:

score: -100
Time Limit Exceeded

input:

10000 8 2 7 8
8
00
01

00
11

00
10

11
11

10
10

01
10

01
00

10
11
8
11
01

11
00

01
10

11
11

00
01

01
01

01
00

11
10
9
00
00

01
01

10
11

00
01

11
10

11
00

11
11

00
11

01
10
9
11
11

10
00

11
00

11
01

00
10

01
11

00
01

01
01

10
01
11
00
01

01
01

10
10

00
11

11
11

11
10
...

output:


result: