其他分享
首页 > 其他分享> > POJ2 3498

POJ2 3498

作者:互联网

把冰块拆点来保证企鹅起跳的限制,对于所有点为汇点跑一边最大流看看是不是和企鹅人数相等

#include<bits/stdc++.h>
#define FT(a,b) memset(a,b,sizeof(a))
using namespace std;

const int N = 300 + 10 ,M = N * N + N * 2 << 1, INF = 0x3f3f3f3f;
const double esp = 1e-6;
int h[N],e[M],ne[M],w[M],idx;
int n,m,s,t;
int gap[N],cur[N],deepth[N];
double d;
struct node
{
    int x, y;
}p[N];
void add(int a,int b,int c)
{
    e[idx] = b,ne[idx] = h[a],w[idx] = c,h[a] = idx ++ ;
    swap(a,b),c = 0;
    e[idx] = b,ne[idx] = h[a],w[idx] = c,h[a] = idx ++ ;
}
void bfs()
{
    queue<int>q;
    FT(deepth,-1),FT(gap,0);
    q.push(t);
    deepth[t] = 0,gap[0] = 1;
    while(q.size())
    {
        int a = q.front();
        q.pop();
        for(int i = h[a];~i; i = ne[i])
        {
            int j = e[i];
            if(deepth[j] == -1)
            {
                deepth[j] = deepth[a] + 1;
                q.push(j);
                gap[deepth[j]]++;
            }
        }
    }
}
int dfs(int now,int flow)
{
    if(now == t)
        return flow;
    int nowflow = 0;
    for(int i = h[now];~i;i = ne[i])
    {
        int j = e[i];
        if(w[i]&&deepth[j] + 1 == deepth[now])
        {
            int k = dfs(j,min(w[i],flow - nowflow));
            w[i]-=k,w[i^1]+=k,nowflow +=k;
            if(flow == nowflow)
                return flow;
        }
    }
    gap[deepth[now]]--;
    if(!gap[deepth[now]])
        gap[s] = n + 2;
    ++deepth[now];
    ++gap[deepth[now]];
    return nowflow;
}
int ISAP()
{
    int ans = 0;
    bfs();
    while(gap[s]<n)
    {
        memcpy(cur,h,sizeof(h));
        ans += dfs(s,INF);
    }
    // cout << ans << endl;
    return ans;
}
void solve()
{
    FT(h,-1),idx = 0;
    cin >> n >> d;
    s = 2 * n,t =2 *  n + 1;
    int tot = 0;
    for(int i = 0;i<n;i++)
    {
        int a,b;
        cin >> p[i].x >> p[i].y>> a >> b;
        add(s,i,a);
        add(i,i+n,b);
        tot += a;
    }
    for(int i = 0;i<n;i++)
    {
        for(int j = 0;j < n;j++)
        {
            int dis = ((p[i].x - p[j].x) * (p[i].x - p[j].x) + (p[i].y - p[j].y) * (p[i].y - p[j].y));
            if(dis <= d * d + esp)
            {
                add(i+n,j,INF);
                // cout << i << " " << j << endl;
            }
        }
    }
    int res = 0;
    for(int i = 0;i<n;i++)
    {
        t = i;
        for(int j = 0;j<idx;j+=2)
        {
            w[j]+=w[j^1];
            w[j^1] = 0;
        }
        if(ISAP() >= tot)
            cout << i << " ", res ++;
    }
    if(!res) 
        cout << -1;
        cout << endl;
}

int main()
{
    int t = 0;
    cin >> t;
    while(t--)
    {
        solve();
    }
    return 0;
}

 

标签:3498,nowflow,int,flow,gap,deepth,POJ2,now
来源: https://www.cnblogs.com/ignorance/p/14122131.html