hdoj 3605 Escape 【中等最大流 | 二分图多重匹配】

题目:hdoj 3605 Escape 


分类:中等最大流 | 二分图多重匹配


题意:给出n个人和m个星球,每个人有想去的兴趣,然后每个星球有容量,问能不能让所有人都住在自己想去的星球?


分析:最大流的话卡的非常严,这个题目写了之后手写MTL,超内存,然后加入状态压缩之后TEL,后面没办法了看别人说C++提交能过,改C++Compilation Error,不容易呀,原来C++用的vc编译器,果然改了之后600ms过了。

首先题意很明显,建图方法也很明显,要设一个超级远点s和汇点t,其他的不说了。

首先第一步优化,状态压缩,因为有100000个人,而星球只有10个,所以每个人想去的星球的状态最多1<<10,大约1000过点,所以先统计状态,然后在压缩之后建图是个很大的优化,这个题目也是对网络流数据卡的很严,要用dinci或者sap,而且要假如优化。


通过这个题目知道了C++用的是vc的编译环境,以后就不用Compilation Error了。

网络流AC代码:

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;
#define Del(a,b) memset(a,b,sizeof(a))
const int N = 1210;
const int inf = 0x3f3f3f3f;
const double esp = 1e-9;
int n,m;
struct Node
{
    int from,to,cap,flow;
};
vector v[N];
int sum[N];
vector e;
int vis[N];  //构建层次图
int cur[N];
int MIN(int x,int y)
{
	return x>y?y:x;
}
void add_Node(int from,int to,int cap)
{
	Node tmp1,tmp2;
	tmp1.from=from,tmp1.to=to,tmp1.cap=cap,tmp1.flow=0;
    e.push_back(tmp1);
	tmp2.from=to,tmp2.to=from,tmp2.cap=0,tmp2.flow=0;
    e.push_back(tmp2);
    int tmp=e.size();
    v[from].push_back(tmp-2);
    v[to].push_back(tmp-1);
}
bool bfs(int s,int t)
{
    Del(vis,-1);
    queue q;
    q.push(s);
    vis[s] = 0;
    while(!q.empty())
    {
        int x=q.front();
        q.pop();
        int i;
        for(i=0;itmp.flow)  //第二个条件保证
            {
                vis[tmp.to]=vis[x]+1;
                q.push(tmp.to);
            }
        }
    }
    if(vis[t]>0)
        return true;
    return false;
}
int dfs(int o,int f,int t)
{
    if(o==t || f==0)  //优化
        return f;
    int a = 0,ans=0;
    int &i = cur[o];
    for(;i0)
        {
            tmp.flow+=a;
            e[v[o][i]^1].flow-=a; //存图方式
            ans+=a;
            f-=a;
            if(f==0)  //注意优化
                break;
        }
    }
    return ans;  //优化
}
int dinci(int s,int t)
{
    int ans=0;
    while(bfs(s,t))
    {
        Del(cur,0);
        int tm=dfs(s,inf,t);
        ans+=tm;
    }
    return ans;
}
int main()
{
    int n,m;
    while(~scanf("%d%d",&n,&m))
    {
        int i,j;
        memset(sum,0,sizeof(sum));
        int s=0,x,t=m+(1<


你可能感兴趣的:(图论~~网络流)