QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#781209 | #9802. Light Up the Grid | IGVA | TL | 2ms | 9976kb | C++14 | 1.8kb | 2024-11-25 15:14:26 | 2024-11-29 22:51:47 |
Judging History
answer
#include<bits/stdc++.h>
#define ll long long
#define inf 0x3f3f3f3f
#define rep(i,a,b) for(int i=(a),i##_end=(b);i<=i##_end;++i)
#define dep(i,a,b) for(int i=(a),i##_end=(b);i>=i##_end;--i)
using namespace std;
template<class T>inline T mymin(T x,T y){return x<y?x:y;}
template<class T>inline T mymax(T x,T y){return x>y?x:y;}
template <typename T>
inline void read(T &X){
X=0;int w=0; char ch=0;
while(!isdigit(ch)) {w|=ch=='-';ch=getchar();}
while(isdigit(ch)) X=X*10+(ch^48),ch=getchar();
if(w) X=-X;
}
const int maxn = 4e5 + 5;
//const double pi = acos(-1.0);
//const double eps = 1e-9;
//const int mo = 1e9 + 7;
int n,m,k;
int a[maxn],c[maxn];
int ans,tmp,cnt;
int flag;
char s[10][10];
bool ok[maxn][16];
int dp[maxn][16];
int jh[] = {1,2,4,8,3,12,5,10,15};
int w[] = {0,0,0,0,1,1,2,2,3};
queue<pair<int,int>>q;
void solve(){
read(n);
m = (1 << n) - 1;
rep(i,0,n - 1){
rep(j,0,1){
scanf("%s",s[j]);
}
c[i] = (s[0][0] & 1) | ((s[0][1] & 1) << 1) | ((s[1][0] & 1) << 2) | ((s[1][1] & 1) << 3);
c[i] ^= 15;
}
rep(i,0,m) rep(j,0,15) {
dp[i][j] = inf;
ok[i][j] = 0;
}
dp[m][0] = 0;
ok[n][0] = 1;
q.push({m,0});
while(!q.empty()){
auto nd = q.front();q.pop();
int i = nd.first;
int j = nd.second;
rep(j,0,15){
rep(l,0,8){
int nj = j ^ jh[l];
int ni = i;
rep(k,0,n - 1)if(i >> k & 1){
if(c[k] == nj) ni ^= (1 << k);
}
if(dp[i][j] + a[w[l]] < dp[ni][nj]){
dp[ni][nj] = dp[i][j] + a[w[l]];
if(!ok[ni][nj]) {
q.push({ni,nj});
ok[ni][nj] = 1;
}
}
}
}
}
int ans = inf;
rep(i,0,15) ans = min(ans,dp[0][i]);
printf("%d\n",ans);
}
int main(){
//cin.tie(0)->sync_with_stdio(false);
int T=1,cas=1;
read(T);
rep(i,0,3) read(a[i]);
while(T--) solve();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 2ms
memory: 9976kb
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: 1ms
memory: 7976kb
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: 9944kb
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 ...