zoukankan      html  css  js  c++  java
  • P7368 [USACO05NOV]Asteroids G

    根据konig定理可知

    最大匹配数=最大流=最小割=最小点集覆盖

    那么这个题就是一个最大流问题了

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    #include<queue>
    using namespace std;
    int n,k;
    const int maxn=506;
    int head[maxn*2];
    int p=1;
    struct b{
    	int to;
    	int ne;
    	int v;
    } ed[maxn*maxn*2];
    void add(int f,int t,int v){
    	ed[++p].ne=head[f];ed[p].to=t;ed[p].v=v;head[f]=p;
    	ed[++p].ne=head[t];ed[p].to=f;ed[p].v=0;head[t]=p;
    } 
    int znx;
    queue<int> q;
    int inf=(1<<25);
    int vis[maxn*2];
    int exf[maxn*2];
    int pre[maxn*2];
    int s,t,m;
    int x,y;
    int Aimee;
    bool bfs(){
    	while(!q.empty()){
    		q.pop();
    	}
    	memset(vis,0,sizeof(vis));
    	vis[s]=1;
    	q.push(s);
    	exf[s]=inf;
    	while(!q.empty()){
    		int x=q.front();
    		q.pop();
    		for(int i=head[x];i;i=ed[i].ne){
    			int v=ed[i].to;
    			if(ed[i].v){
    				if(vis[v]) continue;
    				q.push(v);
    				exf[v]=min(exf[x],ed[i].v);
    				pre[v]=i;
    				vis[v]=1;
    				if(v==t) return 1;
    			}
    		}
    	}
    	return 0;
    }
    int fl[2][maxn];
    void up(){
    	int now=t;
    	while(now!=s){
    		int x=pre[now];
    		ed[x].v-=exf[t];
    		ed[x^1].v+=exf[t];
    		now=ed[x^1].to;
    	}
    	Aimee+=exf[t];
    }
    int main(){
    	scanf("%d%d",&n,&k);
    	s=n+n+1;	 
    	t=n+n+2;
    	for(int i=1;i<=k;++i){
    		scanf("%d%d",&x,&y);
    		add(x,y+n,1);
    		add(y+n,x,0);
    		if(!fl[1][x]){
    			add(s,x,1);
    			add(x,s,0);
    			fl[1][x]=1;
    		} 
    		if(!fl[2][y]){
    			add(y+n,t,1);
    			add(t,y+n,0);
    			fl[2][y]=1; 
    		} 
    	}
    	while(bfs()){
    		up();
    	} 
    	cout<<Aimee; 
    	return 0;
    }
    
  • 相关阅读:
    扫描线算法
    [Baltic 2001]Mars Maps
    Lost Cow
    李超线段树
    多种方法求解Pku3468 A Simple Problem with Integers
    陈老师的福利
    leetcode 673. 最长递增子序列的个数
    #10043.「一本通 2.2 例 1」剪花布条
    PTA7-1
    6-1 实验三哈夫曼树 (15分)
  • 原文地址:https://www.cnblogs.com/For-Miku/p/14425342.html
Copyright © 2011-2022 走看看