POJ 3498 —— March of the Penguins (枚举+最大流)

#include<cstdio>
#include<cstring>
#include<string>
#include<queue>
#include<vector>
#include<algorithm>
#define inf 0x3f3f3f3f
using namespace std;
const int maxn = 205;
int num_nodes;
int n;
double m;
struct Edge
{
	int from, to, flow, cap;
}edge[maxn*maxn*2];

struct Node
{
	double x, y;
	int n, m;
}node[maxn];

vector<int>G[maxn];
int edgenum;
void add(int u, int v, int c)
{
	edge[edgenum].from = u;
	edge[edgenum].to = v;
	edge[edgenum].flow = 0;
	edge[edgenum].cap = c;
	edgenum++;
	
	edge[edgenum].from = v;
	edge[edgenum].to = u;
	edge[edgenum].flow = 0;
	edge[edgenum].cap = 0;
	edgenum++;
	
	G[u].push_back(edgenum-2);
	G[v].push_back(edgenum-1);
}

int deep[maxn];
bool vis[maxn];
void BFS(int s, int t)
{
	queue<int>Q;
	memset(vis, false, sizeof vis);
	Q.push(t);
	vis[t] = true;
	deep[t] = 0;
	while(!Q.empty())
	{
		int now = Q.front();
		Q.pop();
		for(int i = 0;i<(int)G[now].size();i++)
		{
			int v = edge[G[now][i]].to;
			if(!vis[v])
			{
				deep[v] = deep[now] + 1;
				vis[v] = true;
				Q.push(v);
			}
		}
	}
}

int gap[maxn], front[maxn], cur[maxn];
int Augment(int s, int t)
{
	int minflow = inf;
	int begin = t;
	while(begin != s)
	{
		Edge& e = edge[front[begin]];
		minflow = min(minflow, e.cap - e.flow);
		begin = e.from;
	}
	
	begin = t;
	while(begin != s)
	{
		edge[front[begin]].flow += minflow;
		edge[front[begin]^1].flow -= minflow;
		begin = edge[front[begin]].from;
	}
	return minflow;
}

int Maxflow(int s, int t)
{
	int flow = 0;
	BFS(s, t);
	memset(gap, 0, sizeof gap);
	memset(cur, 0, sizeof cur);
	for(int i = 0;i<num_nodes;i++)
		gap[deep[i]]++;
	int begin = s;
	while(deep[s] < num_nodes)
	{
		if(begin == t)
		{
			flow += Augment(s, t);
			begin = s;
		}
		bool flag = false;
		for(int i = cur[begin];i<(int)G[begin].size();i++)
		{
			Edge& e = edge[G[begin][i]];
			if(e.cap > e.flow && deep[begin] == deep[e.to] + 1)
			{
				front[e.to] = G[begin][i];
				cur[begin] = i;
				flag = true;
				begin = e.to;
				break;
			}
		}
		if(!flag)
		{
			int k = num_nodes-1;
			for(int i = 0;i<(int)G[begin].size();i++)
			{
				Edge& e = edge[G[begin][i]];
				if(e.cap > e.flow)
					k = min(k, deep[e.to]);
			}
			if(--gap[deep[begin]] == 0)	break;
			gap[deep[begin] = k+1]++;
			cur[begin] = 0;
			if(begin != s)
			{
				begin = edge[front[begin]].from;
			}
		}
	}
	return flow;
}

void init()
{
	for(int i = 0;i<num_nodes;i++)	G[i].clear();
	edgenum = 0;
	memset(deep, 0, sizeof deep);
}

double Cal_dis(int a, int b)
{
	double x1 = node[a].x, y1 = node[a].y;
	double x2 = node[b].x, y2 = node[b].y;
	return (x1-x2)*(x1-x2)+(y1-y2)*(y1-y2);
}

int ans[maxn];
int main()
{	
	int cas;
	scanf("%d", &cas);
	while(cas--)
	{
		scanf("%d%lf", &n, &m);
		int sum = 0;
		for(int i = 1;i<=n;i++)
		{
			scanf("%lf%lf%d%d", &node[i].x, &node[i].y, &node[i].n, &node[i].m);
			sum += node[i].n;
		}
			
		int cnt = 0;
		for(int i = 1;i<=n;i++)
		{
			init();
			int s = 0, t = i;
			for(int k = 1;k<=n;k++)
			{
				add(k, k+n, node[k].m);
				add(s, k, node[k].n);
			}
			for(int j = 1;j<=n;j++)
			{
				for(int k = j+1;k<=n;k++)
				{
					if(Cal_dis(j, k) <= m*m)
					{
						add(j+n, k, inf);
						add(k+n, j, inf);
					}
				}
			}			
			num_nodes = 2*n+1;
			int flow = Maxflow(s, t);
			if(flow == sum)
			{
				printf("%d ", i-1);
				cnt++;
			}
		}
		if(cnt == 0)	printf("-1\n");
		else	printf("\n");
	}
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值