QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#784570 | #9802. Light Up the Grid | Corycle# | TL | 2ms | 12032kb | C++20 | 1.8kb | 2024-11-26 15:22:30 | 2024-11-26 15:22:31 |
Judging History
answer
#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int N=16;
const int M=1<<16;
int read(){
int s=0,f=1;char c=getchar();
while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
while(c>='0'&&c<='9'){s=s*10+c-'0';c=getchar();}
return s*f;
}
char s[4][4];
ll dis[N][M];
bool vis[N][M];
int n,m,Sta,a[9],sta[M];
int b[9]={1,2,4,8,5,10,3,12,15};
struct Node{
int x,t;
ll dis;
friend bool operator<(Node A,Node B){
return A.dis>B.dis;
}
};
void BFS(){
for(int i=0;i<N;i++){
for(int j=0;j<=Sta;j++){
dis[i][j]=1e18;
vis[i][j]=0;
}
}
priority_queue<Node>q;
q.push((Node){15,0,0});
dis[15][0]=0;
while(q.size()){
Node x=q.top();q.pop();
if(vis[x.x][x.t])continue;
vis[x.x][x.t]=1;
// cout<<"Solve: "<<x.x<<" "<<x.t<<" "<<x.dis<<endl;
for(int i=0;i<9;i++){
Node y=(Node){x.x^b[i],x.t|(sta[x.x^b[i]]),x.dis+a[i]};
// cout<<": "<<y.x<<" "<<y.t<<" "<<y.dis<<endl;
if(dis[y.x][y.t]>y.dis){
dis[y.x][y.t]=y.dis;
q.push(y);
}
}
}
}
int main(){
int T=read();
a[0]=a[1]=a[2]=a[3]=read();
a[4]=a[5]=read();
a[6]=a[7]=read();
a[8]=read();
while(T--){
n=read();
Sta=(1<<n)-1;
for(int i=0;i<=Sta;i++)sta[i]=0;
for(int i=0;i<n;i++){
scanf("%s%s",s[0],s[1]);
int x=(s[0][0]-'0')+(s[0][1]-'0')*2+(s[1][0]-'0')*4+(s[1][1]-'0')*8;
sta[x]|=(1<<i);
// cout<<i<<": "<<x<<endl;
}
BFS();
ll ans=1e18;
for(int i=0;i<16;i++)ans=min(ans,dis[i][Sta]);
printf("%lld\n",ans);
}
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 0ms
memory: 12016kb
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: 2ms
memory: 11956kb
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: 1ms
memory: 12032kb
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:
34 32 31 34 40 38 42 37 40 41 31 44 34 38 38 32 38 38 38 40 36 42 42 37 32 40 36 40 34 44 36 41 34 36 41 31 44 27 38 34 34 36 39 39 42 35 39 37 36 34 40 36 40 42 34 41 43 29 41 33 42 37 37 34 29 40 36 40 42 38 34 29 33 34 32 36 38 39 38 39 35 38 43 33 36 32 42 40 28 40 38 42 38 41 36 36 35 38 34 36 ...