首页 > 其他分享 >POJ 3498 March of the Penguins(枚举+最大流)

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

时间:2023-06-12 14:34:14浏览次数:68  
标签:penguins March 3498 int flow number ice Penguins include


题意:在X,Y坐标系中有N(N<=100)个冰块...有些冰块上有若干只企鹅..每只企鹅一次最多跳M距离..一个冰块在有Mi个企鹅离开..就会消失..问有哪些冰块可以作为集合点..就是所有企鹅都能成功到这个冰块上来.

思路:枚举每一块冰块,看看最大流能否等于企鹅总数即可


           建图:把每块冰分成两个点i和i+n. i表示进入i冰块的点(可以有无数企鹅过来,所以从别的冰到i有边,容量为INF) i+n表示从i冰块出去的点(最多只能有Mi企鹅从这跳出去,所以从i到i+n有边,且容量为Mi)

                     从源点S到i有边(S, i, i点初始企鹅数).

                     从i到i+n有边(i, i+n, Mi). 表示第i块冰最多只有Mi个企鹅能跳走.

                     因为i+n表示的是第i个跳走的点,所以如果冰块i和j之间的距离<=企鹅能跳跃的距离M,有边(i+n, j, INF)

                     假设我们当前枚举第x块冰块作为集合点,那么(x分成x和x+n两个点)x点就是汇点(不是x+n点哦),我们只要计算到x点的流量是否==企鹅总数即可.


Trick:如果有拆点的话在初始化的时候记得修改初始化函数..这个傻逼错误我调了一个多小时...



#include <cstdio>
#include <queue>
#include <cstring>
#include <iostream>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <map>
#include <string>
#include <set>
#include <ctime>
#include <cmath>
#include <cctype>
using namespace std;
#define maxn 550
#define INF 1e9
#define LLINF 1LL<<60
#define LL long long
int cas=1,T;
struct Edge
{
	int from,to,cap,flow;
	Edge(int u,int v,int c,int f):from(u),to(v),cap(c),flow(f){}
};
int n,m;
struct Dinic
{
//	int n,m;
    int s,t;
	vector<Edge>edges;        //边数的两倍
	vector<int> G[maxn];      //邻接表,G[i][j]表示结点i的第j条边在e数组中的序号
	bool vis[maxn];           //BFS使用
	int d[maxn];              //从起点到i的距离
	int cur[maxn];            //当前弧下标
	void init()
	{
	   for (int i=0;i<=2*n+2;i++)                   //初始化!!!如果有拆点记得这里要改
		   G[i].clear();
	   edges.clear();
	  // memset(d,0,sizeof(d));
	}
	void AddEdge(int from,int to,int cap)
	{
		edges.push_back(Edge(from,to,cap,0));
		edges.push_back(Edge(to,from,0,0));        //反向弧
		int mm=edges.size();
		G[from].push_back(mm-2);
		G[to].push_back(mm-1);
	}
	bool BFS()
	{
		memset(vis,0,sizeof(vis));
		queue<int>q;
		q.push(s);
		d[s]=0;
		vis[s]=1;
		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 (!vis[e.to] && e.cap > e.flow)
				{
					vis[e.to]=1;
					d[e.to] = d[x]+1;
					q.push(e.to);
				}
			}
		}
		return vis[t];
	}

	int DFS(int x,int a)
	{
		if (x==t || a==0)
			return a;
		int flow = 0,f;
		for(int &i=cur[x];i<G[x].size();i++)
		{
			Edge &e = edges[G[x][i]];
			if (d[x]+1 == d[e.to] && (f=DFS(e.to,min(a,e.cap-e.flow)))>0)
			{
				e.flow+=f;
				edges[G[x][i]^1].flow-=f;
				flow+=f;
				a-=f;
				if (a==0)
					break;
			}
		}
		return flow;
	}

	int Maxflow(int s,int t)
	{
		this->s=s;
		this->t=t;
		int flow = 0;
		while (BFS())
		{
			memset(cur,0,sizeof(cur));
			flow+=DFS(s,INF);
		}
		return flow;
	}
}dc;
int sum;
double limit;
struct Node
{
	double x,y;
	int now;      //有多少只企鹅在冰上
	int num;      //一块冰可以让多少只企鹅跳
}pie[maxn];
double getdist(Node a,Node b)
{
	return ((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
bool solve(int ice)
{
	dc.init();
    for (int i = 1;i<=n;i++)
	{
		if (pie[i].now)
		  dc.AddEdge(0,i,pie[i].now);
	}
	for (int i = 1;i<=n;i++)
	{
		dc.AddEdge(i,i+n,pie[i].num);
	}

	for (int i = 1;i<=n;i++)
		for (int j = i+1;j<=n;j++)
			if (getdist(pie[i],pie[j]) <= limit*limit)
			{
			   dc.AddEdge(i+n,j,INF);
               dc.AddEdge(j+n,i,INF);
			}
	return dc.Maxflow(0,ice) == sum;
}
int main()
{
	//freopen("in","r",stdin);
	scanf("%d",&T);
	while (T--)
	{		
		scanf("%d%lf",&n,&limit);
		sum=0;
		for (int i = 1;i<=n;i++)
		{
			scanf("%lf%lf%d%d",&pie[i].x,&pie[i].y,&pie[i].now,&pie[i].num);
			sum+=pie[i].now;                 //统计企鹅的数量
		}
	
		vector <int> ans;
		ans.clear();
        for (int i = 1;i<=n;i++)
		{
			if (solve(i))
				ans.push_back(i);
		}
		if (ans.size()==0)
		{
			printf("-1\n");
		}
		else
	    {
			for (int i = 0;i<ans.size()-1;i++)
                printf("%d ",ans[i]-1);
			printf("%d\n",ans[ans.size()-1]-1);
		}
	}
	//printf("time=%.3lf",(double)clock()/CLOCKS_PER_SEC);
	return 0;
}




Description



Somewhere near the south pole, a number of penguins are standing on a number of ice floes. Being social animals, the penguins would like to get together, all on the same floe. The penguins do not want to get wet, so they have use their limited jump distance to get together by jumping from piece to piece. However, temperatures have been high lately, and the floes are showing cracks, and they get damaged further by the force needed to jump to another floe. Fortunately the penguins are real experts on cracking ice floes, and know exactly how many times a penguin can jump off each floe before it disintegrates and disappears. Landing on an ice floe does not damage it. You have to help the penguins find all floes where they can meet.





A sample layout of ice floes with 3 penguins on them.


Input


On the first line one positive number: the number of testcases, at most 100. After that per testcase:

  • One line with the integer N (1 ≤ N ≤ 100) and a floating-point number D (0 ≤ D ≤  100 000  ), denoting the number of ice pieces and the maximum distance a penguin can jump.
  • N lines, each line containing xiyini and mi, denoting for each ice piece its X and Y coordinate, the number of penguins on it and the maximum number of times a penguin can jump off this piece before it disappears (  −10 000  ≤ xiyi ≤  10 000  , 0 ≤ ni ≤ 10, 1 ≤ mi ≤ 200).


Output


Per testcase:

  • One line containing a space-separated list of 0-based indices of the pieces on which all penguins can meet. If no such piece exists, output a line with the single number −1.


Sample Input


25 3.5 1 1 1 1 2 3 0 1 3 5 1 1 5 1 1 1 5 4 0 1 3 1.1 -1 0 5 10 0 0 3 9 2 0 1 1


Sample Output


1 2 4-1





标签:penguins,March,3498,int,flow,number,ice,Penguins,include
From: https://blog.51cto.com/u_16156555/6462551

相关文章

  • P3498 [POI2010]KOR-Beads 题解
    前言:最近在做哈希的题,发现了这道好题,看题解里很多大佬的方法都很巧妙,自己就发一个较为朴素的方法吧。题意:题目传送门给你一个序列,需要求出数k,使划分的子串长度为k时,不同的子串数量最多。还要注意几件事:子串可以反转,比如(1,2,3)看做与(3,2,1)相同。如果不能正好划......
  • 界面重建——Marching cubes算法
    一、引子对于一个标量场数据,我们可以描绘轮廓(Contouring),包括2D和3D。2D的情况称为轮廓线(contourlines),3D的情况称为表面(surface)。他们都是等值线或等值面。以下是一个2D例子: 为了生成轮廓,必须使用某种形式的插值。这是因为我们只在数据集中的一个有限点集上有标量值,而我们......
  • DTOJ 3498 无限剑制 题解
    题面题目链接题解想了好久,其实很水tt想写题解主要是因为这题题面是Fate很有意思我们注意到“所有\(v_i\)值域在\([1,5]\)”这个部分分,这种情况下,初始的不同情......
  • 什么是体元?什么是体素?Marching Cubes算法理解
    Marchingcubesusesadivide-and-conquerapproachtolocatethesurfaceinalogicalcubecreatedfromeightpixels;foureachfromtwoadjacentslicesMarchin......
  • 编译指令 -mcpu -march
    这俩指令都会根据当前系统使用的微架构对程序进行优化,优点是针对计算密集型任务会有较大程度的优化,但是可移植性不好,因为是针对特定架构的优化一。确定选项使用如下命令......