QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#143169 | #6406. Stage Clear | Schi2oid | WA | 1137ms | 135472kb | C++20 | 2.8kb | 2023-08-20 20:14:54 | 2023-08-20 20:14:57 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
#define int long long
const int INF=1e18;
int n,m;
vector<int>edge[105];
vector<int>fedge[105];
int x[105],y[105];
int bx[105],by[105];
int cnt[105];
int dp[70000005];
int ret=INF;
int solve1(){
dp[1]=0;
for(int i=2;i<(1ll<<n);i++) dp[i]=INF;
int sum=0;
for(int i=1;i<(1ll<<n);i++){
for(int j=0;j<n;j++){
if(((i>>j)&1)&&(!(((i-1)>>j)&1))){
sum+=y[j+1]-x[j+1];
for(int k=0;k<edge[j+1].size();k++) cnt[edge[j+1][k]]++;
}
else if((((i-1)>>j)&1)&&(!((i>>j)&1))){
sum-=y[j+1]-x[j+1];
for(int k=0;k<edge[j+1].size();k++) cnt[edge[j+1][k]]--;
}
else break;
}
for(int j=1;j<=n;j++) if(cnt[j]&&(!((i>>(j-1))&1))) dp[i^(1<<(j-1))]=min(dp[i^(1<<(j-1))],dp[i]+max(0ll,x[j]-dp[i]-sum));
}
return dp[(1ll<<n)-1];
}
struct node{
int x,y,id;
bool operator < (const node& ano) const {
if(y>=0&&ano.y<0) return 1;
else if(y<0&&ano.y>=0) return 0;
else if(y>=0){
if(x!=ano.x) return x<ano.x;
else if(y!=ano.y) return y<ano.y;
else return id<ano.id;
}
else{
if(x+y!=ano.x+ano.y) return x+y>ano.x+ano.y;
else if(x!=ano.x) return x<ano.x;
else if(y!=ano.y) return y<ano.y;
else return id<ano.id;
}
}
};
int fa[105],f[105];
int find(int x){
if(fa[x]==x) return x;
fa[x]=find(fa[x]);
return fa[x];
}
void merge(int a,int b){
x[a]=max(x[a],x[b]-y[a]);
y[a]+=y[b];
}
bool vis[105];
vector<int>tmp[105];
bool check(int x){
vis[x]=1;
int ret=1;
for(int i=0;i<tmp[x].size();i++){
int v=tmp[x][i];
if(vis[v]) return 0;
ret&=check(v);
}
return ret;
}
int calc(){
for(int i=1;i<=n;i++) vis[i]=0;
for(int i=1;i<=n;i++) tmp[i].clear();
for(int i=2;i<=n;i++) tmp[f[i]].push_back(i);
bool ttt=check(1);
if(!ttt) return INF;
for(int i=1;i<=n;i++) if(!vis[i]) return INF;
memcpy(x,bx,sizeof x);
memcpy(y,by,sizeof y);
set<node>s;
for(int i=2;i<=n;i++) s.insert((node){x[i],y[i],i});
for(int i=1;i<=n;i++) fa[i]=i;
while(s.size()){
int u=(*s.begin()).id;
s.erase(*s.begin());
int ffu=find(f[u]);
fa[u]=ffu;
if(ffu!=1) s.erase((node){x[ffu],y[ffu],ffu});
merge(ffu,u);
if(ffu!=1) s.insert((node){x[ffu],y[ffu],ffu});
}
return x[1];
}
void dfs(int x){
if(x==n+1){
ret=min(ret,calc());
return;
}
for(int i=0;i<fedge[x].size();i++){
f[x]=fedge[x][i];
dfs(x+1);
}
}
int solve2(){
for(int i=1;i<=n;i++) y[i]-=x[i],x[i]=max(x[i],y[i]);
memcpy(bx,x,sizeof bx);
memcpy(by,y,sizeof by);
dfs(2);
return ret;
}
signed main(){
int u,v;
cin>>n>>m;
for(int i=2;i<=n;i++) scanf("%lld%lld",&x[i],&y[i]);
for(int i=1;i<=m;i++){
scanf("%lld%lld",&u,&v);
edge[u].push_back(v);
fedge[v].push_back(u);
}
if(n<=26) cout<<solve1()<<endl;
else cout<<solve2()<<endl;
return 0;
}
詳細信息
Test #1:
score: 100
Accepted
time: 1ms
memory: 3592kb
input:
4 4 4 2 5 3 2 6 1 2 1 3 2 4 3 4
output:
4
result:
ok 1 number(s): "4"
Test #2:
score: 0
Accepted
time: 1ms
memory: 5800kb
input:
15 14 254040392438309 117083115436273 500005748229691 557255157630172 821034233718230 865199673774998 659892147898798 987564141425694 81172575487567 811635577877255 751768357864605 341103322647288 454926350150218 140191090713900 921608121471585 659295670987251 223751724062143 505619245326640 8907765...
output:
1665396301509143
result:
ok 1 number(s): "1665396301509143"
Test #3:
score: 0
Accepted
time: 9ms
memory: 7296kb
input:
18 17 636830992776530 847574431876821 330869946457865 78274534165482 450581372553540 11565219334965 8736347226844 17186323694285 870805093198860 559070167736042 674369178493171 930151818400874 641605209598997 222521062460239 450936030349531 469197172169023 831295459816974 626096008793091 53095460351...
output:
2375957544280218
result:
ok 1 number(s): "2375957544280218"
Test #4:
score: 0
Accepted
time: 58ms
memory: 12556kb
input:
20 19 539893468691183 767805205447882 240338186903141 960937349402327 942645580569365 896509929612645 542601575005817 191461109090531 540992546866047 765080044816119 904535155855114 858111921213175 452499200048240 115895143306864 983856946412026 838504718536099 586421298181479 265212699386882 677124...
output:
800919806038419
result:
ok 1 number(s): "800919806038419"
Test #5:
score: 0
Accepted
time: 1137ms
memory: 135472kb
input:
24 23 114281007218527 308690671179962 145951034437731 718976086594208 709172151907814 926071954787084 224496444610281 498657753059525 874422017133378 857676356343078 532175866197017 818525693672607 303837639402605 374469705563954 512244364294540 952911486867703 748959419417502 249992707230361 512696...
output:
114281007218527
result:
ok 1 number(s): "114281007218527"
Test #6:
score: -100
Wrong Answer
time: 1ms
memory: 3600kb
input:
36 35 389328367777319 678636570542258 32216944647452 612585362150577 891592845704885 596030605892036 688825276167602 461516360471825 916552899998310 106733202183953 400050408958777 670724326933521 995792861502757 894514508573875 14511185222713 612305257166443 175168368096281 508263855969282 85578802...
output:
358932768607979
result:
wrong answer 1st numbers differ - expected: '171942144116875', found: '358932768607979'