QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#787258#9802. Light Up the Gridticking_away#TL 0ms3872kbC++203.8kb2024-11-27 10:37:502024-11-27 10:37:51

Judging History

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

  • [2024-11-27 10:37:51]
  • 评测
  • 测评结果:TL
  • 用时:0ms
  • 内存:3872kb
  • [2024-11-27 10:37:50]
  • 提交

answer

#include <bits/stdc++.h>
#define con const
using namespace std;

using i32 = int32_t;
using i64 = int64_t;
#define i128 __int128_t
using u32 = uint32_t;
using u64 = uint64_t;
using f64 = long double;

template<class T>
using lque = priority_queue<T, vector<T>, greater<T>>;
template<class T>
using gque = priority_queue<T>;

template<class T>
void mes(T * arr, int sz=1, int val=0) {
    memset(arr, val, sz*sizeof(T));
}

// constexpr u32 IBUF_LEN = 1 << 16;
// char ibuf[1<<16], *ifr = ibuf, *ied = ibuf;
// #define fread() (ied=(ifr=ibuf)+fread(ibuf,1,IBUF_LEN,stdin),ifr==ied) // 单次fread不会等待输入
// #define ischar(c) (!isspace(c)&&c!=EOF)
// char gc() { return (ifr==ied && fread()) ? EOF : *(ifr++); } // 读取并弹出字符(类似getchar)
// char gr() { return (ifr==ied && fread()) ? EOF : *ifr; } // 读取字符
// char fit() { while(isspace(gr())) ++ifr; return gr(); } // 将空字符丢弃,并返回第一个非空字符或EOF
// #undef fread
 
// template<class T> T &fin(T &v) {
//     i32 c = v = 0; bool f = fit()=='-'; ifr += f;
//     while(isdigit(c=gc())) v = v*10 + (c ^ 48);
//     return f ? (v=-v) : v;
// }
 
// template<class T> void fin(T arr[], u32 n) {
//     for(u32 i=0; i<n; ++i) fin(arr[i]);
// }
 
// // 读取非空字符(建议手动gc(),这个只是简写)
// template<> char &fin(char &v) { return fit(), v = gc(); }
// -------------------
con u32 MX = 2e6 + 7;
con u64 MOD = 998244353;
// con u64 MOD = 1e9+7;

tuple<u32, u32> get_st(u32 st) { return {st>>4, st&15}; }
// ms: map status, gs: grid status
u32 build_st(u32 ms, u32 gs) { return (ms<<4)|gs; }

map<u32, u32> dp;

u32 n, cst[4], gip[16];
u32 w[16][16];
u32 ds[1<<16][1<<4];

bool vis[1<<20];

void solve() {
    memset(ds, -1, (1<<n)*sizeof(ds[0]));
    memset(vis, 0, (1<<n)*(1<<4)*sizeof(bool));
    lque<pair<u32, u32>> que;
    for(u32 i=1; i<16; ++i)
        que.emplace(ds[0][i]=w[0][i], i);
    while(!que.empty()) {
        auto [d, st] = que.top(); que.pop();
        if(vis[st]) continue; vis[st] = 1;
        auto [ums, ugs] = get_st(st);
        for(u32 vm=0; vm<16; ++vm) {
            if((ums>>vm)&1) continue;
            u32 vms = ums | (1<<vm);
            u32 vgs = 15^gip[vm];
            if(ds[vms][vgs]>d+w[ugs][vgs]) {
                u32 vst = build_st(vms, vgs);
                que.emplace(ds[vms][vgs]=d+w[ugs][vgs], vst);
            }
        }
    }
    cout << *min_element(ds[(1<<n)-1], ds[1<<n]) << '\n';
}

void pre() {
    cin >> n;
    string ip;
    for(u32 i=0; i<n; ++i) {
        gip[i] = 0;
        cin >> ip;
        for(char ch:ip)
            gip[i] = (gip[i]<<1)|(ch=='1');
        cin >> ip;
        for(char ch:ip)
            gip[i] = (gip[i]<<1)|(ch=='1');
    }
}

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);

    u32 _t = 1;
    cin >> _t;
    for(u32 i=0; i<4; ++i) cin >> cst[i];

    memset(w, -1, sizeof(w));
    for(u32 u=0; u<16; ++u) {
        for(u32 i=0; i<4; ++i) {
            u32 v = u^(1<<i);
            w[u][v] = min(w[u][v], cst[0]);
        }
        for(u32 i=0; i<2; ++i) {
            u32 v = u^(3<<(i*2));
            w[u][v] = min(w[u][v], cst[1]);
        }
        for(u32 i=0; i<2; ++i) {
            u32 v = u^(5<<i);
            w[u][v] = min(w[u][v], cst[2]);
        }
        w[u][u^15] = min(w[u][u^15], cst[3]);
    }

    for(u32 k=0; k<16; ++k) {
        for(u32 u=0; u<16; ++u) {
            if(!~w[u][k]) continue;
            for(u32 v=0; v<16; ++v) {
                if(!~w[k][v]) continue;
                w[u][v] = min(w[u][v], w[u][k]+w[k][v]);
            }
        }
    }

    for(u32 i=0; i<16; ++i)
        w[i][i] = 0;

    for(;_t--;) {
        pre();
        solve();
    }
}

详细

Test #1:

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

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

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

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: