最大流模版

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<string>
#include<cmath>
#include<vector>
#include<queue>
using namespace std;
int n,m,t,v;
struct point
{
    double x;
    double y;
}rat[105],hole[105];
const double eps=1e-6;
bool ok(point p1,point p2)
{
    double tmp1=sqrt((p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y));
    double tmp2=(double)v*(double)t;
    if(tmp2-tmp1>eps)return true;
    return false;
}

const int INF=0x3f3f3f3f;
const int maxn=1e4+7;
int pre[maxn],visited[maxn],G[maxn][maxn];
int S,T;
//int n,np,nc,m;
int np,nc;
struct Edge{
    int from,to,cap,flow;
    Edge(int u,int v,int c,int f):from(u),to(v),cap(c),flow(f){}
};
struct EdmondsKarp{
    int n,m;
    vector<Edge>edges;//边数的两倍
    vector<int>G[maxn];//邻接表,G[i][j]表示结点i的第j条边在e数组中的序号
    int a[maxn];//当起点到i的可改进量
    int p[maxn];//最短路树上p的入弧编号
 
    void init(int n){
        for(int i=0;i<n;i++)G[i].clear();
        edges.clear();
    }
 
    void AddEdge(int from,int to,int cap){
        edges.push_back(Edge(from,to,cap,0));
        edges.push_back(Edge(to,from,0,0));
        m=edges.size();
        G[from].push_back(m-2);
        G[to].push_back(m-1);
    }
 
    int Maxflow(int s,int t){
        int flow=0;
        for(;;){
            memset(a,0,sizeof(a));
            queue<int>Q;
            Q.push(s);
            a[s]=INF;
            while(!Q.empty()){
                int x=Q.front();Q.pop();
                for(int i=0;i<G[x].size();i++){
                    Edge &e=edges[G[x][i]];
                    if(!a[e.to]&&e.cap>e.flow){
                        p[e.to]=G[x][i];
                        a[e.to]=min(a[x],e.cap-e.flow);
                        Q.push(e.to);
                    }
                }
                if(a[t])break;
            }
            if(!a[t])break;
            for(int u=t;u!=s;u=edges[p[u]].from){
                edges[p[u]].flow+=a[t];
                edges[p[u]^1].flow-=a[t];
            }
            flow+=a[t];
        }
        return flow;
    }
}EK;



int main()
{
    while(cin>>n)
    {
        if(n==0)break;
        cin>>m>>t>>v;
        for(int i=1;i<=n;i++)
        {
            cin>>rat[i].x>>rat[i].y;
        }
        for(int i=1;i<=m;i++)
        {
            cin>>hole[i].x>>hole[i].y;
        }
        
        EK.init(n+2+m);
        
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=m;j++)
            {
                if(ok(rat[i],hole[j]))
                {
                    EK.AddEdge(1+i,n+1+j,1);
                }
            }
        }
        for(int i=1;i<=n;i++)EK.AddEdge(1,1+i,1);
        for(int i=1;i<=m;i++)EK.AddEdge(n+1+i,n+2+m,1);
        cout<<EK.Maxflow(1,n+2+m)<<endl;
    }
    return 0;
}

http://acm.whu.edu.cn/weblearn/problem/292

posted @ 2021-01-01 19:24  TheDa  阅读(97)  评论(0编辑  收藏  举报