QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#781690 | #9802. Light Up the Grid | Yurily | RE | 17ms | 14072kb | C++14 | 4.1kb | 2024-11-25 16:57:39 | 2024-11-25 16:57:48 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
#define int long long
int a,b,c,d;
int dis[20];
int f[1<<16][20];
int m;
int string_to_int(string &s){
int sum=0;
if(s[0]=='1')
sum+=1;
if(s[1]=='1')
sum+=2;
if(s[2]=='1')
sum+=4;
if(s[3]=='1')
sum+=8;
return sum;
}
string int_to_string(int x){
string ss;
if(x&1)
ss.push_back('1');
else
ss.push_back('0');
x>>=1;
if(x&1)
ss.push_back('1');
else
ss.push_back('0');
x>>=1;
if(x&1)
ss.push_back('1');
else
ss.push_back('0');
x>>=1;
if(x&1)
ss.push_back('1');
else
ss.push_back('0');
x>>=1;
return ss;
}
string get_s(string s,int op){
if(op==1)
return int_to_string(string_to_int(s)^(1));
if(op==2)
return int_to_string(string_to_int(s)^(2));
if(op==3)
return int_to_string(string_to_int(s)^(4));
if(op==4)
return int_to_string(string_to_int(s)^(8));
if(op==5)
return int_to_string(string_to_int(s)^(3));
if(op==6)
return int_to_string(string_to_int(s)^(12));
if(op==7)
return int_to_string(string_to_int(s)^(5));
if(op==8)
return int_to_string(string_to_int(s)^(10));
if(op==9)
return int_to_string(string_to_int(s)^(15));
}
void dij(){
string ss0111="0111";
string ss1011="1011";
string ss1101="1101";
string ss1110="1110";
string ss1100="1100";
string ss0011="0011";
string ss1010="1010";
string ss0101="0101";
string ss0000="0000";
memset(dis,0x3f,sizeof(dis));
dis[string_to_int(ss0111)]=dis[string_to_int(ss1011)]=dis[string_to_int(ss1101)]=dis[string_to_int(ss1110)]=a;
dis[string_to_int(ss1100)]=dis[string_to_int(ss0011)]=b;
dis[string_to_int(ss1010)]=dis[string_to_int(ss0101)]=c;
dis[string_to_int(ss0000)]=d;
priority_queue<pair<int,string>,vector<pair<int,string> >,greater<pair<int,string> > > q;
q.push({a,ss0111});
q.push({a,ss1011});
q.push({a,ss1101});
q.push({a,ss1110});
q.push({b,ss1100});
q.push({b,ss0011});
q.push({c,ss1010});
q.push({c,ss0101});
q.push({d,ss0000});
while(!q.empty()){
auto tmp=q.top();
q.pop();
if(dis[string_to_int(tmp.second)]<tmp.first)
continue;
auto x=tmp.second;
int zhi=0;
for(int i=1;i<=9;i++){
auto tx=get_s(x,i);
if(i<=4)
zhi=a;
if(5<=i && i<=6)
zhi=b;
if(7<=i && i<=8)
zhi=c;
if(i==9)
zhi=d;
if(dis[string_to_int(tx)]>dis[string_to_int(x)]+zhi){
dis[string_to_int(tx)]=dis[string_to_int(x)]+zhi;
q.push({dis[string_to_int(tx)],tx});
}
}
}
}
int bet(int x,int y){
return dis[15^(x^y)];
}
void dp(){
memset(f,0x3f,sizeof(f));
for(int i=1;i<(1<<16);i++){
int x=i;
if(__builtin_popcount(x)==1){
f[x][__builtin_ffs(x)]=dis[__builtin_ffs(x)];
continue;
}
for(int j=1;j<=16;j++){
if(x&(1<<(j-1))==0)
continue;
int y=x^(1<<(j-1));
for(int k=1;k<=16;k++){
if(y&(1<<(k-1))==0)
continue;
f[x][j]=min(f[x][j],f[y][k]+bet(k,j));
}
}
}
}
void Solve(){
cin>>m;
int tmp=0;
for(int i=1;i<=m;i++){
string s;
string ss;
cin>>s;
cin>>ss;
s=s+ss;
tmp+=(1<<(string_to_int(s)-1));
}
int ans=0x3f3f3f3f3f;
for(int i=1;i<=16;i++){
if(tmp&(1<<(i-1))==0)
continue;
ans=min(ans,f[tmp][i]);
}
cout<<ans<<'\n';
}
signed main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
int T;
cin>>T;
cin>>a>>b>>c>>d;
dij();
dp();
while(T--)
Solve();
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 17ms
memory: 13808kb
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: 13ms
memory: 13816kb
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: 12ms
memory: 14072kb
input:
1 1000000 1000000 1000000 1000000 1 11 11
output:
2000000
result:
ok 1 number(s): "2000000"
Test #4:
score: -100
Runtime Error
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 ...