zoukankan      html  css  js  c++  java
  • poj 3041 Asteroids

    http://poj.org/problem?id=3041

    X所在的行向列建边,跑二分图

    #include<iostream>
    #include<cstdio>
    #include<queue>
    #include<algorithm>
    #include<cmath>
    #include<ctime>
    #include<set>
    #include<map>
    #include<stack>
    #include<cstring>
    #define inf 2147483647
    #define ls rt<<1
    #define rs rt<<1|1
    #define lson ls,nl,mid,l,r
    #define rson rs,mid+1,nr,l,r
    #define N 1000010
    #define For(i,a,b) for(long long i=a;i<=b;i++)
    #define p(a) putchar(a)
    #define g() getchar()
    
    using namespace std;
    long long T,n,m,x,y,v,maxflow,tot,len;
    long long head[N],deep[N],cur[N];
    struct node{
        long long n;
        long long v;
        long long next;
    }e[N];
    queue<long long>q;
    
    void in(long long &x){
        long long y=1;char c=getchar();x=0;
        while(c<'0'||c>'9'){if(c=='-')y=-1;c=getchar();}
        while(c<='9'&&c>='0'){ x=(x<<1)+(x<<3)+c-'0';c=getchar();}
        x*=y;
    }
    void o(long long x){
        if(x<0){p('-');x=-x;}
        if(x>9)o(x/10);
        p(x%10+'0');
    }
    
    void push(long long x,long long y,long long v){
        e[tot].n=y;
        e[tot].v=v;
        e[tot].next=head[x];
        head[x]=tot++;
    }
    
    bool bfs(long long s,long long t){
        memset(deep,-1,sizeof(deep));
        For(i,0,2*n+1)
            cur[i]=head[i];
        deep[s]=0;
        q.push(s);
        while(!q.empty()){
            long long now=q.front();
            q.pop();
            for(long long i=head[now];i!=-1;i=e[i].next)
                if(deep[e[i].n]==-1 && e[i].v>0){
                    deep[e[i].n]=deep[now]+1;
                    q.push(e[i].n);
                }
        }
        return deep[t]!=-1;
    }
    
    long long dfs(long long now,long long t,long long lim){
        if(now==t||lim==0)
            return lim;
        long long flow=0,f;
        for(long long i=cur[now];i!=-1;i=e[i].next){
            cur[now]=i;
            if(deep[e[i].n]==deep[now]+1 && (f=dfs(e[i].n,t, min(lim,e[i].v) ))){
                flow+=f;
                lim-=f;
                e[i].v-=f;
                e[i^1].v+=f;
                if(!lim) break;
            }
        }
        return flow;
    }
    
    void dinic(long long s,long long t){
        while(bfs(s,t)){
            maxflow+=dfs(s,t,inf);
        }
    }
    
    void init(){
        memset(head,-1,sizeof(head));
        tot=maxflow=0;
    }
    
    signed main(){
        init();
        in(n);in(m);
        For(i,1,m){
            in(x);in(y);
            push(x,n+y,1);
            push(n+y,x,0);
        }
        For(i,1,n){
            push(0,i,1);
            push(i,0,0);
        }
        For(i,n+1,2*n){
            push(i,2*n+1,1);
            push(2*n+1,i,0);
        }
        dinic(0,2*n+1);
        o(maxflow);
        return 0;
    }
  • 相关阅读:
    网页中输出漂亮格式的Php数组神器
    从对轮播图的封装中体会 面对接口 编程的思想
    UITabBarController 详解
    UINavigationController 详解
    UITabBarController 和 UINavigationController 的详解
    【iOS开发】canOpenURL: failed for URL
    SDK里报错[NSConcreteMutableData wbsdk_base64EncodedString]
    UIScrollView 几乎所有的属性和方法
    oracle服务器和客户端字符集的查看和修改
    查看、修改oracle字符集,查看oracle版本
  • 原文地址:https://www.cnblogs.com/war1111/p/13435866.html
Copyright © 2011-2022 走看看