QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#815530 | #9802. Light Up the Grid | ucup-team1769# | TL | 0ms | 3832kb | C++14 | 2.8kb | 2024-12-15 15:18:22 | 2024-12-15 15:18:32 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define max_len ((1 << (4)) + 10)
#define status_map(i1, i2, i3, i4) (((i1) * 0x1) | ((i2) * 0x2) | ((i3) * 0x4) | ((i4) * 0x8))
int min_cost[max_len];
int a1, a2, a3, a4;
void init_cost()
{
memset(min_cost, 0x3f, sizeof(min_cost));
}
void bfs()
{
// cost, status
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> que;
que.push({0, status_map(1, 1, 1, 1)});
while (que.size() != 0)
{
auto [co, st] = que.top();
que.pop();
if(min_cost[st] != 0x3f3f3f3f)
{
continue;
}
min_cost[st] = co;
auto mask_status = [&] (int mask_s, int cost_use){
int new_st = st ^ mask_s;
que.push({co + cost_use, new_st});
};
mask_status(status_map(1, 1, 1, 1), a4);
mask_status(status_map(1, 0, 1, 0), a3);
mask_status(status_map(0, 1, 0, 1), a3);
mask_status(status_map(1, 1, 0, 0), a2);
mask_status(status_map(0, 0, 1, 1), a2);
mask_status(status_map(1, 0, 0, 0), a1);
mask_status(status_map(0, 1, 0, 0), a1);
mask_status(status_map(0, 0, 1, 0), a1);
mask_status(status_map(0, 0, 0, 1), a1);
}
}
int can_stauts[max_len];
int status_vis[max_len];
int N;
int min_ans = 0x3f3f3f3f;
void dfs(int now_i, int all_mask, int all_cost, int pas_st)
{
status_vis[now_i] = 1;
pas_st++;
all_cost += min_cost[all_mask ^ can_stauts[now_i]];
all_mask ^= ((all_mask ^ can_stauts[now_i]) ^ status_map(1, 1, 1, 1));
if(all_cost >= min_ans)
{
status_vis[now_i] = 0;
return;
}
if(pas_st == N)
{
min_ans = min(min_ans, all_cost);
}
else
{
for (int i = 0; i < N; i++)
{
if(status_vis[i] == 0)
{
dfs(i, all_mask, all_cost, pas_st);
}
}
}
status_vis[now_i] = 0;
}
int get_ans()
{
for (int i = 0; i < N; i++)
{
dfs(i, 0, 0, 0);
}
return min_ans;
}
#define ch2int(ch) ((ch) - '0')
void sol()
{
cin >> N;
for (int i = 0; i < N; i++)
{
string s1, s2;
cin >> s1 >> s2;
can_stauts[i] = status_map(ch2int(s1[0]), ch2int(s1[1]), ch2int(s2[0]), ch2int(s2[1]));
}
if(N == 1 && can_stauts[0] == 0x0f)
{
cout << min(2 * a4, min(2 * a3, min(2 * a2, 2 * a1))) << "\n";
return;
}
int ans = get_ans();
cout << ans << "\n";
}
int main()
{
// cout << status_map(1, 1, 1, 1) << endl;
int T;
cin >> T >> a1 >> a2 >> a3 >> a4;
init_cost();
bfs();
while (T--)
{
sol();
}
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
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: 3600kb
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: 3608kb
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:
32 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 29 29 29 29 29 29 29 29 25 25 25 25 25 25 25 25 25 25 25 25 25 25 25 25 25 25 25 25 25 25 25 25 25 25 25 25 25 25 25 25 25 25 25 25 25 25 25 25 25 25 25 25 25 25 25 25 25 25 25 25 25 25 25 25 25 25 25 25 25 25 25 25 25 25 25 25 25 25 25 25 25 25 25 25 ...