首页 > 其他分享 >AcWing 2280. 最优标号

AcWing 2280. 最优标号

时间:2022-12-16 16:13:08浏览次数:53  
标签:标号 dep 2280 ne tot int ans now AcWing

AcWing 2280. 最优标号
很神奇的建图,但是确实很有道理

//多个进行匹配的问题
#include <bits/stdc++.h>
using namespace std;
using ll=long long;
const int N=1005,M=1e6+5;
const int inf=1e9;
 
int h[N],ne[M],e[M],w[M],tot=1;
void add(int from,int to,int w1,int w2) {
    e[++tot]=to;  w[tot]=w1;  ne[tot]=h[from];  h[from]=tot;
    e[++tot]=from;w[tot]=w2;   ne[tot]=h[to];    h[to]=tot;
}
 
int S=N-2,T=N-1;
int cur[N],dep[N];
bool bfs() {
    memcpy(cur,h,sizeof(h));
    memset(dep,0,sizeof(dep));
    queue<int>q;
    q.push(S);
    dep[S]=1;
    while(!q.empty()) {
        int now=q.front();
        q.pop();
        for(int i=h[now];i;i=ne[i]) {
            int to=e[i];
            if(dep[to]==0&&w[i]>0)
                dep[to]=dep[now]+1,q.push(to);
        }
    }
    return dep[T];
}
 
int dfs(int now,int sum) {
    if(now==T)return sum;
    int ans=0;
    for(int i=cur[now];i&&sum;i=ne[i]) {
        cur[now]=i;
        int to=e[i];
        if(dep[to]==dep[now]+1&&w[i]>0) {
            int k=dfs(to,min(sum,w[i]));
            if(k==0)dep[to]=0;
            w[i]-=k;
            w[i^1]+=k;
            sum-=k;
            ans+=k;
        }
    }
    return ans;
}

int n,m,a[N];
int x[3005],y[3005];
 
void build(int flag) {
    memset(h,0,sizeof(h));
    tot=1;
    for(int i=1;i<=m;i++)add(x[i],y[i],1,1);
    for(int i=1;i<=n;i++)
        if(a[i]!=-1) {
            if(a[i]>>flag&1)add(i,T,inf,0);
            else add(S,i,inf,0);
        }
}

ll dinic(int x) {
    build(x);
    int ans=0;
    while(bfs())ans+=dfs(S,inf);
    return ans;
}

int main() {
    cin>>n>>m;
    for(int i=1;i<=m;i++)cin>>x[i]>>y[i];
    int k;cin>>k;
    memset(a,-1,sizeof(a));
    while(k--) {
        int x,y;
        cin>>x>>y;
        a[x]=y;
    }
    ll ans=0;
    for(int i=0;i<31;i++)
        ans+=dinic(i)<<i;
    cout<<ans<<'\n';
    return 0;
}

标签:标号,dep,2280,ne,tot,int,ans,now,AcWing
From: https://www.cnblogs.com/basicecho/p/16987626.html

相关文章

  • Acwing 第 78 场周赛 ABC
    明天考完就可以放假咯,水一篇博客练练手https://www.acwing.com/activity/content/2622/4719.商品种类题目大意:货架中摆放着n件商品,每件商品都有两个属性:名称和产地......
  • acwing 2237. 猪
    2237.猪分层图的简化,思路tql,大大的减少了点的基数如果这个猪房已经被用过了,那就直接从用过的那个人哪里流过来就可以了如果这个猪房没有用过,那就从超级起点流出来。这......
  • AcWing 261. 旅馆 【线段树】
    [NOIP2015普及组]推销员题目背景NOIP2015普及组T4题目描述阿明是一名推销员,他奉命到螺丝街推销他们公司的产品。螺丝街是一条死胡同,出口与入口是同一个,街道的一侧......
  • AcWing 205. 斐波那契
    \(AcWing\)\(205\).斐波那契​​题目传送门​​一、题目描述在斐波那契数列中,\(F_ib_0=0,F_ib_1=1,F_ib_n=F_ib_{n−1}+F_ib_{n−2}(n>1)\)。给定整数\(n\),求\(F_ib_n~......
  • AcWing2437. Splay 题解
    题目大意:splay执行区间翻转示例程序:#include<bits/stdc++.h>usingnamespacestd;constintmaxn=1e5+5;intn,m;structNode{ints[2],p,v;intsz,flag......
  • acwing第80场周赛
    比赛地址核心思路:这是一眼暴力搜索题,但是我们怎么取构造他们的参数呢。首先我们肯定需要4和7的个数,所以这两个参数是肯定需要的,还有就是我们需要他们两个个数加起来不能够......
  • acwing 273. 分级
     #include"bits/stdc++.h"usingnamespacestd;constintN=2e3+3;intn,a[N],b[N],f[N][N];intA=1e9;voidsov(){inti,j,k;for(i=1;i<=n;i++)......
  • acwing 152. 城市游戏
     #include"bits/stdc++.h"usingnamespacestd;constintN=1e3+3;intn,m,a[N][N],s[N][N];intA;intw[N],h[N],pp;voidsov(intx){inti,ans=0......
  • 时间 Acwing每日一题
    本系列所有题目均为Acwing课的内容,发表博客既是为了学习总结,加深自己的印象,同时也是为了以后回过头来看时,不会感叹虚度光阴罢了,因此如果出现错误,欢迎大家能够指出错误,我......
  • acwing 140. 后缀数组
    把字符串S的所有后缀按照字典序排列,排名为i的后缀记为SA[i] 额外地,我们考虑排名为i的后缀与排名为 i-1的后缀,把二者的最长公共前缀的长度记为hgt[i]使用快排......