QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#839419#9802. Light Up the GridFalse0099TL 1ms3528kbC++203.4kb2025-01-01 18:49:242025-01-01 18:49:25

Judging History

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

  • [2025-01-01 18:49:25]
  • 评测
  • 测评结果:TL
  • 用时:1ms
  • 内存:3528kb
  • [2025-01-01 18:49:24]
  • 提交

answer

#include <bits/stdc++.h>
#define int long long
#define endl '\n'
int INF = 0x3f3f3f3f3f3f3f3f;
using namespace std;
typedef pair<int, int> PII;
int a0, a1, a2, a3;
const int N = 17;
int dist[N][N];
void calc()
{
    // swap(a1, a2);
    memset(dist, INF, sizeof dist);
    for (int i = 0; i < 16; i++)
    {
        int u = i;
        int v1 = (u ^ 1);
        int v2 = (u ^ 2);
        int v3 = (u ^ 4);
        int v4 = (u ^ 8);

        int v5 = (u ^ 3);
        int v6 = (u ^ 12);

        int v7 = u ^ 5;
        int v8 = (u ^ 10);

        int v9 = u ^ 15;
        //        dist[u][u] = 0;
        dist[i][v1] = a0;
        dist[i][v2] = a0;
        dist[i][v3] = a0;
        dist[i][v4] = a0;

        dist[i][v5] = a1;
        dist[i][v6] = a1;

        dist[i][v7] = a2;
        dist[i][v8] = a2;

        dist[i][v9] = a3;
    }
    // for (int i = 0; i < 16; i++)
    // {
    //     for (int j = 0; j < 16; j++)
    //     {
    //         // assert(dist[11][15] == 1000);
    //         //cerr << i << " " << j << " " << dist[i][j] << endl;
    //     }
    //     cerr << endl;
    // }
    for (int k = 0; k < 16; k++)
    {
        for (int i = 0; i < 16; i++)
        {
            for (int j = 0; j < 16; j++)
            {
                dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]);
            }
        }
    }
}
void init()
{
    calc();
}
void solve()
{
    int m;
    cin >> m;
    vector<int> Sgrid(m);
    vector<vector<int>> f((1ll << m) + 10, vector<int>(m + 10, INF));
    for (int i = 0; i < m; i++)
    {
        string s1;
        string s2;
        cin >> s1 >> s2;
        int res = 0;
        for (int j = 0; s1[j]; j++)
        {
            res = res * 2 + (s1[j] - '0');
        }
        for (int j = 0; s2[j]; j++)
        {
            res = res * 2 + (s2[j] - '0');
        }
        //cerr << res << endl;
        Sgrid[i] = res;
    }
    for (int i = 0; i < m; i++)
    {
        f[(1ll << i)][i] = dist[Sgrid[i]][15];
    }
    // cerr << "ok" << endl;
    for (int s = 0; s < (1ll << m); s++)
    {
        int prev_States = 0;
        int cnd_s = __popcount(s);
        if ((cnd_s >= 1))
        {
            prev_States ^= 15;
        }
        for (int i = 0; i < m; i++)
        {
            if (((s >> i) & 1) == 1)
            {
                for (int k = 0; k < m; k++)
                {
                    if (((s >> k) & 1) == 0)
                    {
                        // cerr << Sgrid[i] << " " << Sgrid[k] << " " << dist[Sgrid[i]][Sgrid[k]] << endl;
                        f[s | (1ll << k)][k] = min(f[s | (1ll << k)][k], f[s][i] + dist[(prev_States ^ Sgrid[k] ^ Sgrid[i])][15]);
                    }
                }
            }
        }
    }
    // cout << (1ll << m) - 1 << endl;
    // for (int s = 0; s < (1ll << m); s++)
    // {
    //     for (int i = 0; i < m; i++)
    //     {
    //         cout << dist[s][i] << " ";
    //     }
    //     cout << endl;
    // }
    // cout << endl;
    int res = INF;
    for (int i = 0; i < m; i++)
    {
        res = min(res, f[(1ll << m) - 1][i]);
    }
    cout << res << endl;
}
signed main()
{
    ios::sync_with_stdio(false), cin.tie(0);
    int t;
    t = 1;
    cin >> t;
    cin >> a0 >> a1 >> a2 >> a3;
    init();
    while (t--)
    {
        solve();
    }
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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: 3488kb

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: 1ms
memory: 3524kb

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:

34
32
36
36
40
36
42
38
40
41
36
44
34
37
37
32
29
39
39
40
38
39
44
37
29
37
37
38
34
34
32
41
34
36
41
40
44
34
37
34
29
36
39
40
42
35
39
37
38
38
41
29
40
41
36
41
43
40
41
34
42
39
37
33
34
38
38
37
42
40
34
36
28
34
32
38
36
39
38
37
36
38
34
34
34
34
42
40
38
38
40
40
37
40
38
29
36
40
36
34
...

result: