扣血回血机制 n+m<=72

· · 个人记录

QOJ6406. Stage Clear

#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int N=605;
struct node{ll cst,val;};
int cmp1(node x,node y){
    //  没有严谨单调性 
    return max(x.cst,x.cst-x.val+y.cst)<max(y.cst,y.cst-y.val+x.cst);}

int cmp2(node x,node y){
    if((x.cst<x.val)!=(y.cst<y.val)) return x.cst<x.val;
    if(x.cst<x.val) return x.cst<y.cst;
    return x.val>y.val;}



int main(){
    for(ll c1=1;c1<=10;c1++)
    for(ll v1=1;v1<=10;v1++)
        for(ll c2=1;c2<=10;c2++)
        for(ll v2=1;v2<=10;v2++)
            if(cmp1(node{c1,v1},node{c2,v2})!=cmp2(node{c1,v1},node{c2,v2})){
                printf("%lld %lld ; %lld %lld\n",c1,v1,c2,v2);
                printf("%lld %lld\n",cmp1(node{c1,v1},node{c2,v2}),cmp2(node{c1,v1},node{c2,v2}));
                printf("%lld %lld\n",max(c1,c1-v1+c2),max(c2,c2-v2+c1));
                return 0;
            }
    return 0;
}


正确代码:

#include<bits/stdc++.h>
using namespace std;
#define ll long long
const ll N=1e6+5,inf=1e18;
int n,m;
int B[N],par[N],deg[N],now[N],vis[N];
vector<int>v[N],g[N];
ll v1[N],v2[N],ans=inf;
struct node{ll cst,val;int id;}b[N];
inline bool operator < (node y,node x){
    if((x.cst<x.val)!=(y.cst<y.val)) return x.cst<x.val;
    if(x.cst<x.val) return x.cst<y.cst;
    return x.val>y.val;}
inline node merge(node x,node y){
    ll u=max(x.cst,x.cst-x.val+y.cst);
    return node{u,x.val+y.val-x.cst-y.cst+u,x.id};}
priority_queue<node> que;
inline int find(int x){if(B[x]==x) return x; return B[x]=find(B[x]);}
void work1(){
    for(int i=1,x,y;i<=m;i++){
        scanf("%d%d",&x,&y),deg[y]++;
        v[x].push_back(y);
        g[y].push_back(x);}
    while(now[1]==0){
        for(int i=1;i<=n;i++) B[i]=i,vis[i]=0,b[i]=node{v1[i],v2[i],i};
        for(int i=2;i<=n;i++) par[i]=g[i][now[i]]; while(!que.empty()) que.pop();
        for(int i=2;i<=n;i++) que.push(b[i]);
        while(!que.empty()){
            node e=que.top(); que.pop();
            if(vis[e.id]) continue;
            if((e.cst!=b[e.id].cst)||(e.val!=b[e.id].val)) continue;
            int u=find(par[e.id]); B[e.id]=u; vis[e.id]=1;
            b[u]=merge(b[u],b[e.id]);
            if(u>=2) que.push(b[u]);
        }
        ans=min(ans,b[1].cst);
        now[n]++; for(int i=n;i>=2;i--) if(now[i]>=deg[i]) now[i]=0,now[i-1]++;}
    printf("%lld\n",ans);
}
int w[N],b2[N],K;ll f[70000000];
void work2(){
    w[1]=1; for(int i=2;i<=n;i++) w[i]=(w[i-1]<<1ll);
    for(int i=1,x,y;i<=m;i++) scanf("%d%d",&x,&y),b2[y]|=w[x];
    memset(f,0x3f3f3f3f,sizeof f); K=(1<<n)-1;
    f[K]=-inf;
    for(int i=K;i>=3;i-=2)
        for(int j=1;j<=n;j++)
            if(i&w[j])
                if(i&b2[j])
                    f[i^w[j]]=min(f[i^w[j]],max(v1[j],f[i]-v2[j]+v1[j]));
    printf("%lld\n",f[1]);
}
int main(){
    scanf("%d%d",&n,&m);
    for(int i=2;i<=n;i++) scanf("%lld%lld",&v1[i],&v2[i]);
    if(n>=27) return work1(),0;
    return work2(),0;
}


/*

4 4
4 2
5 3
2 6
1 2
1 3
2 4
3 4


1->2->4
1->3




*/