一个dinic超时的疑问
查看原帖
一个dinic超时的疑问
915727
hao_zi6366楼主2025/8/31 22:03

如下代码提交本题时会在 #1 和 #13 T掉,但这种构图方法确实可以通过本题,猜测是 dinic 出锅了,求大佬解答

#include<bits/stdc++.h>
#define mid ((l+r)>>1)
#define inf 0x7fffffff
typedef long long LL;
const int N = 5e4+10;
const int bs=131;
const int Mod=1e9+7;
namespace hao_zi{

int n,nn,st,ed,s,t,idx=1,ind;
struct edge{
    int nxt,v;
    LL w;
}e[(int)1e6<<1];
int head[N],dx[]={0,0,1,1,-1,-1},dy[]={1,-1,0,1,0,-1};
void addedge(int u,int v,int w){
    e[++idx]={head[u],v,w};
    head[u]=idx;
}
void add(int u,int v,int w){
    //if(w==0)return ;
    addedge(u,v,w);
    addedge(v,u,0);
}
int now[N],dep[N];
bool bfs(){
    for(int i=1;i<=ind;i++){
        dep[i]=0;
        now[i]=head[i];
    }
    std::queue<int> q;
    q.push(st);
    dep[st]=1;
    while(!q.empty()){
        int u=q.front();q.pop();
        for(int i=head[u];i;i=e[i].nxt){
            int v=e[i].v;
            LL w=e[i].w;
            if(dep[v]==0 && w>0){
                q.push(v);
                dep[v]=dep[u]+1;
            }
        }
    }
    return dep[ed]!=0;
}
LL dfs(int u,LL flow){
    if(u==ed){
        return flow;
    }
    LL tmp=flow;
    for(int& i=now[u];i && tmp;i=e[i].nxt){
        int v=e[i].v;
        LL w=e[i].w;
        if(dep[v]==dep[u]+1 && w>0){
            LL cs=dfs(v,std::min(w,tmp));
            if(cs==0)dep[v]=0;
            e[i].w-=cs;
            e[i^1].w+=cs;
            tmp-=cs;
        }

    }
    return flow-tmp;
}
LL res=0,sum=0;
void dinic(){
    while(bfs()){
        res+=dfs(st,inf);
    }
}
std::map<std::pair<int,int>,LL> c;
std::map<std::pair<int,int>,int> id;
int mod3(int x){
    return x%3<0?x%3+3:x%3;
}
void solve(){
    std::cin>>nn;
    for(int i=1;i<=nn;i++){
        int x,y,z,cs;
        std::cin>>x>>y>>z>>cs;
        if(mod3(x+y+z)==0){
            cs*=11;
        }else{
            cs*=10;
        }
        sum+=cs;
        x-=z,y-=z;
        c[{x,y}]+=cs;
    }
    st=++ind;s=st;
    ed=++ind;t=ed;
    for(auto cc:c){
        id[cc.first]=++ind;
        ++ind;
        add(id[cc.first],ind,cc.second);
    }
    for(auto cc:c){
        int x=cc.first.first,y=cc.first.second;
        if(mod3(x+y)==0){
            for(int i=0;i<6;i++){
                int nx=x+dx[i],ny=y+dy[i];
                auto it=c.find({nx,ny});
                if(it!=c.end()){
                    if(mod3(nx+ny)==1){
                        add(id[{nx,ny}]+1,id[{x,y}],inf);
                    }else if(mod3(nx+ny)==2){
                        add(id[{x,y}]+1,id[{nx,ny}],inf);
                    }
                }
            }
        }else if(mod3(x+y)==1){
            add(st,id[{x,y}],inf);
        }else if(mod3(x+y)==2){
            add(id[{x,y}]+1,ed,inf);
        }
    }
    dinic();
    std::cout<<std::fixed<<std::setprecision(1)<<0.1L*(sum-res)<<"\n";
}

}

int main(){
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
    std::cout.tie(nullptr);

    hao_zi::solve();
    return 0;
}

2025/8/31 22:03
加载中...