QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#132790 | #6331. Jewel Game | Energy_is_not_over | RE | 0ms | 0kb | C++17 | 7.1kb | 2023-07-31 16:23:40 | 2023-07-31 16:23:43 |
Judging History
answer
//
// Created by Barichek on 21.07.2023 15:43:48
//
#include <bits/stdc++.h>
#define F first
#define S second
#define MP make_pair
#define PB push_back
#define all(a) a.begin(), a.end()
#define len(a) (int) (a.size())
#define mp make_pair
#define pb push_back
#define fir first
#define sec second
using namespace std;
typedef pair<int, int> pii;
typedef long long ll;
typedef long double ld;
#ifdef Energy_is_not_over
#define DEBUG for (bool ____DEBUG = true; ____DEBUG; ____DEBUG = false)
#define LOG(...) print(#__VA_ARGS__" ::", __VA_ARGS__) << endl
template<class ...Ts>
auto &print(Ts ...ts) { return ((cerr << ts << " "), ...); }
#else
#define DEBUG while (false)
#define LOG(...)
#endif
const int max_n = 30, inf = 1000111222;
const int max_k=10;
inline bool get_bit(ll mask,int bit)
{
return (mask>>bit)&1;
}
bool maximize(int& a,int b)
{
if (a<b){
a=b;
return 1;
}
return 0;
}
bool minimize(int& a,int b)
{
if (a>b){
a=b;
return 1;
}
return 0;
}
int n,m,a,b;
int k;
bool edge[max_n][max_n];
int vertex_cost[max_n];
int vertex_id_in_mask[max_k];
int pos_in_mask_for_vertex[max_n];
int global_mask;
int dp[(1ll<<max_k)][2][max_n][max_n];
int cur_outs_A[max_n][max_n];
int cur_outs_B[max_n][max_n];
priority_queue<pair<int,pii>> pqA;
priority_queue<pair<int,pii>> pqB;
void process_cur_A(int i,int j);
void process_cur_B(int i,int j);
void process_cur_A(int i,int j)
{
cur_outs_A[i][j]=inf;
if (abs(dp[global_mask][0][i][j])==+inf){
dp[global_mask][0][i][j]=0;
}
else{
if (pos_in_mask_for_vertex[j]==-1 || get_bit(global_mask,pos_in_mask_for_vertex[j])){
for (int k=0;k<n;k++){
if (edge[k][j] && cur_outs_B[i][k]<inf/2){
if (minimize(dp[global_mask][1][i][k],dp[global_mask][0][i][j])){
pqB.push(mp(-dp[global_mask][1][i][k],mp(i,k)));
}
if ((--cur_outs_B[i][k])==0){
process_cur_B(i,k);
}
}
}
}
}
LOG("final value of dp[global_mask][0][i][j]",i,j,dp[global_mask][0][i][j]);
}
void process_cur_B(int i,int j)
{
cur_outs_B[i][j]=inf;
if (abs(dp[global_mask][1][i][j])==+inf){
dp[global_mask][1][i][j]=0;
}
else{
if (pos_in_mask_for_vertex[j]==-1 || get_bit(global_mask,pos_in_mask_for_vertex[j])){
for (int k=0;k<n;k++){
if (edge[k][i] && cur_outs_A[k][j]<inf/2){
if (maximize(dp[global_mask][0][k][j],dp[global_mask][1][i][j])){
pqA.push(mp(dp[global_mask][0][k][j],mp(k,j)));
}
if ((--cur_outs_A[k][j])==0){
process_cur_A(k,j);
}
}
}
}
}
LOG("final value of dp[global_mask][1][i][j]",i,j,dp[global_mask][1][i][j]);
}
int main() {
// freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stderr);
ios_base::sync_with_stdio(0);
cin.tie(0);
cin>>n>>m>>a>>b;
a--;
b--;
for (int i=0;i<n;i++){
pos_in_mask_for_vertex[i]=-1;
}
for (int i=0;i<m;i++){
int u,v;
cin>>u>>v;
u--;
v--;
edge[u][v]=1;
}
cin>>k;
LOG(k);
for (int i=0;i<k;i++){
int x,w;
cin>>x>>w;
x--;
vertex_id_in_mask[i]=x;
vertex_cost[x]=w;
pos_in_mask_for_vertex[x]=i;
}
for (int i=0;i<(1ll<<k);i++){
for (int j=0;j<n;j++){
for (int k=0;k<n;k++){
dp[i][0][j][k]=-inf;
dp[i][1][j][k]=+inf;
}
}
}
for (global_mask=(1ll<<k)-1;global_mask>=0;global_mask--){
LOG(bitset<4>(global_mask));
{
for (int i=0;i<n;i++){
for (int j=0;j<n;j++){
for (int k=0;k<n;k++){
if (edge[i][k] && (pos_in_mask_for_vertex[k]!=-1 && !get_bit(global_mask,pos_in_mask_for_vertex[k]))){
maximize(dp[global_mask][0][i][j],dp[global_mask+(1ll<<pos_in_mask_for_vertex[k])][1][k][j]+vertex_cost[k]);
}
}
pqA.push(mp(dp[global_mask][0][i][j],mp(i,j)));
}
}
for (int i=0;i<n;i++){
for (int j=0;j<n;j++){
for (int k=0;k<n;k++){
if (edge[j][k] && (pos_in_mask_for_vertex[k]!=-1 && !get_bit(global_mask,pos_in_mask_for_vertex[k]))){
minimize(dp[global_mask][1][i][j],dp[global_mask+(1ll<<pos_in_mask_for_vertex[k])][0][i][k]-vertex_cost[k]);
}
}
pqB.push(mp(-dp[global_mask][1][i][j],mp(i,j)));
}
}
}
{
memset(cur_outs_A,0,sizeof(cur_outs_A));
memset(cur_outs_B,0,sizeof(cur_outs_B));
for (int i=0;i<n;i++){
for (int j=0;j<n;j++){
for (int k=0;k<n;k++){
if (edge[i][k] && (pos_in_mask_for_vertex[k]==-1 || get_bit(global_mask,pos_in_mask_for_vertex[k]))){
cur_outs_A[i][j]++;
}
}
for (int k=0;k<n;k++){
if (edge[j][k] && (pos_in_mask_for_vertex[k]==-1 || get_bit(global_mask,pos_in_mask_for_vertex[k]))){
cur_outs_B[i][j]++;
}
}
}
}
for (int i=0;i<n;i++){
for (int j=0;j<n;j++){
if (cur_outs_A[i][j]==0){
process_cur_A(i,j);
}
}
}
for (int i=0;i<n;i++){
for (int j=0;j<n;j++){
if (cur_outs_B[i][j]==0){
process_cur_B(i,j);
}
}
}
while (1){
while (!pqA.empty() && cur_outs_A[pqA.top().sec.fir][pqA.top().sec.sec]>inf/2){
pqA.pop();
}
while (!pqB.empty() && cur_outs_B[pqB.top().sec.fir][pqB.top().sec.sec]>inf/2){
pqB.pop();
}
if (pqA.empty() && pqB.empty()){
break;
}
if (!pqA.empty() && (pqA.top().fir!=-inf || pqB.empty())){
auto now=pqA.top().sec;
pqA.pop();
process_cur_A(now.fir,now.sec);
}
else{
auto now=pqB.top().sec;
pqB.pop();
process_cur_B(now.fir,now.sec);
}
}
}
}
cout<<dp[0][0][a][b]<<"\n";
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Dangerous Syscalls
input:
5 16 1 1 1 2 1 3 1 4 1 5 2 3 2 4 2 5 3 2 3 4 3 5 4 2 4 3 4 5 5 2 5 3 5 4 4 2 4 3 84 4 38 5 96