码迷,mamicode.com
首页 > 其他好文 > 详细

3972

时间:2017-11-28 20:41:34      阅读:186      评论:0      收藏:0      [点我收藏+]

标签:class   flow   name   i++   back   ace   ack   emc   efi   

#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e6+11;
const int oo = 0x7fffffff;
const double eps = 1e-8;
int to[maxn<<1],nxt[maxn<<1],cap[maxn<<1],flow[maxn<<1];
int head[maxn],tot;
void init(){
    memset(head,-1,sizeof head);
    tot=0;
}
void add(int u,int v,int w){
    to[tot]=v;
    nxt[tot]=head[u];
    cap[tot]=w;
    flow[tot]=0;
    head[u]=tot++;
    swap(u,v);
    to[tot]=v;
    nxt[tot]=head[u];
    cap[tot]=0;
    flow[tot]=0;
    head[u]=tot++;
}
int n,m,s,t;
int dis[maxn],pre[maxn],cur[maxn],gap[maxn];
bool vis[maxn];
struct QUEUE{
    int que[maxn];
    int front,rear;
    void init(){front=rear=0;}
    void push(int u){que[rear++]=u;}
    int pop(){return que[front++];}
    bool empty(){return front==rear;}
}que;
void bfs(){
    memset(vis,0,sizeof vis);
    que.init();
    que.push(t);
    vis[t]=1;dis[t]=0;
    while(que.empty()^1){
        int u = que.pop();
        for(int i = head[u]; ~i; i = nxt[i]){
            register int v=to[i],c=cap[i^1],f=flow[i^1];
            if(!vis[v]&&c>f){
                vis[v]=1;
                dis[v]=dis[u]+1;
                que.push(v);
            }
        }
    }
}
int aug(){
    int u=t,ans=oo;
    while(u!=s){
        ans=min(ans,cap[pre[u]]-flow[pre[u]]);
        u=to[pre[u]^1];
    }
    u=t;
    while(u!=s){
        flow[pre[u]]+=ans;
        flow[pre[u]^1]-=ans;
        u=to[pre[u]^1];
    }
    return ans;
}
int isap(){
    int ans=0;
    bfs();
    memset(gap,0,sizeof gap);
    memcpy(cur,head,sizeof head);
    for(int i = 1; i <= n; i++) gap[dis[i]]++;
    int u = s;
    while(dis[s]<n){
        if(u==t){
            ans+=aug();
            u=s;
        }
        bool ok=0;
        for(int i = cur[u]; ~i; i = nxt[i]){
            int v=to[i],c=cap[i],f=flow[i];
            if(c>f&&dis[u]==dis[v]+1){
                ok=1;
                pre[v]=i;
                cur[u]=i;
                u=v;
                break;
            }
        }
        if(!ok){
            int mn=n-1;
            for(int i = head[u]; ~i; i = nxt[i]){
                int v=to[i],c=cap[i],f=flow[i];
                if(c>f) mn=min(mn,dis[v]);
            }
            if(--gap[dis[u]]==0) break;
            dis[u]=mn+1;gap[dis[u]]++;cur[u]=head[u];
            if(u!=s) u=to[pre[u]^1];
        }
    }
    return ans;
}
#define rep(i,j,k) for(int i = j; i <= k; i++)
#define repp(i,j,k) for(int i = j; i < k; i++)
#define repe(i,u) for(int i = head[u]; ~i; i = nxt[i])
#define scan(a) scanf("%d",&a)
#define scann(a,b) scanf("%d%d",&a,&b)
#define scannn(a,b,c) scanf("%d%d%d",&a,&b,&c)
#define println(a) printf("%d\n",a)
#define printbk(a) printf("%d ",a)
#define print(a) printf("%d",a)
double ddis(double a,double b,double c,double d){
    // cout<<(a-c)*(a-c)+(b-d)*(b-d)<<endl;
    return (a-c)*(a-c)+(b-d)*(b-d);
}
#define bk(a,b) memcpy(a,b,sizeof b)
int _to[maxn<<1],_nxt[maxn<<1],_cap[maxn<<1],_flow[maxn<<1];
int _head[maxn<<1],_tot;
void backup(){
    bk(_to,to);
    bk(_nxt,nxt);
    bk(_cap,cap);
    bk(_flow,flow);
    bk(_head,head);
    _tot=tot;
}
void rec(){
    bk(to,_to);
    bk(nxt,_nxt);
    bk(cap,_cap);
    bk(flow,_flow);
    bk(head,_head);
    tot=_tot;
}
int main(){
    int n1,ni[maxn],mi[maxn],sum;
    double D,xi[maxn],yi[maxn];
    int T; scan(T);
    while(T--){
        scan(n1);
        scanf("%lf",&D);
        init();sum=0;
        rep(i,1,n1){
            scanf("%lf%lf%d%d",&xi[i],&yi[i],&ni[i],&mi[i]);
            sum+=ni[i];
        }
        s=4*n1+1;t=s+1;n=t;
        #define rg(x) ((x)+n1)
        #define chai(x) ((x)+2*n1)
        #define rgchai(x) ((x)+3*n1)
        rep(i,1,n1) add(s,i,ni[i]);
        rep(i,1,n1){
            add(i,rg(i),oo);
            add(i,chai(i),mi[i]);
            add(rg(i),rgchai(i),mi[i]);
            add(rg(i),i,oo);
            rep(j,1,n1){
                if(i==j) {continue;}
                bool flag=0;
                if(fabs(D*D-ddis(xi[i],yi[i],xi[j],yi[j]))>=0) flag=1;
                if(flag){
                    add(chai(i),rg(j),oo);
                    add(rgchai(j),i,oo);
                }
            }
        }
        backup();
        // memcpy(_head,head,sizeof head);
        vector<int> vec;
        rep(i,1,n1){
            rec();
            // if(i>1) tot-=2;
            // memcpy(head,_head,sizeof head);
            add(rg(i),t,oo);
            // if(isap()==sum) vec.push_back(i);
            println(isap());
        }
        // if(vec.size()) for(int i = 0; i < vec.size(); i++){
        //     if(i==vec.size()-1) println(vec[i]);
        //     else printbk(vec[i]);
        // }
        // else println(-1);
    }
    return 0;
}

3972

标签:class   flow   name   i++   back   ace   ack   emc   efi   

原文地址:http://www.cnblogs.com/caturra/p/7911823.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!