QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#788510 | #9802. Light Up the Grid | Cai_Guang# | TL | 0ms | 3852kb | C++20 | 2.1kb | 2024-11-27 17:14:53 | 2024-11-27 17:14:54 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
const int inf=1e9;
signed main() {
std::ios::sync_with_stdio(false);
std::cin.tie(0);
int t = 1;
std::cin >> t;
int a[4];
for(int i=0;i<4;++i) cin>>a[i];
vector<int>r(16,inf);
for(int i=0;i<16;++i){
int x=__builtin_popcount(i);
if(x==0){
r[i]=min({a[3],2*min(a[1],a[2]),4*a[0],min(a[1],a[2])+2*a[0]});
}
else if(x==1){
r[i]=a[0]+min({a[1],a[2],a[3]});
}
else if(x==3){
r[i]=a[0];
}
else if(x==4){
r[i]=2*min({a[0],a[1],a[2],a[3]});
}
else{
if(i==6 || i==9){
r[i]=min({a[1]+a[2],2*a[0]});
}
else if(i==5 || i==10) r[i]=min(2*a[0],a[2]);
else r[i]=min(2*a[0],a[1]);
}
}
// for(int i=0;i<16;++i){
// cerr<<i<<" : "<<(bitset<4>(i))<<" "<<r[i]<<endl;
// }
while(t--){
int m;
cin>>m;
vector<int>a;
for(int i=0;i<m;++i){
string s,t;
cin>>s>>t;
s+=t;
int x=0;
for(int j=0;j<4;++j){
if((s[j]-'0')) x|=(1<<j);
}
a.push_back(x);
}
// for(auto i:a) cerr<<"? "<<i<<endl;
vector f(1<<m,vector(16,inf));
f[0][0]=0;
for(int i=0;i<1<<m;++i){
for(int j=0;j<16;++j){
for(int k=0;k<m;++k){
if(i>>k&1) continue;
int sta=a[k];
for(int x=0;x<4;++x){
if(j>>x&1) sta^=1<<x;
}
int nj=0;
for(int x=0;x<4;++x){
if(a[k]>>x&1) continue;
nj|=1<<x;
}
f[i|(1<<k)][nj]=min(f[i|(1<<k)][nj],f[i][j]+r[sta]);
}
}
}
int res=inf;
for(int i=0;i<16;++i) res=min(res,f[(1<<m)-1][i]);
cout<<res<<"\n";
}
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3816kb
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: 3852kb
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: 3852kb
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 ...