POJ 3498 March of the Penguins point flow with limited maximum flow

Source: Internet
Author: User

Test instructions

Give the coordinates of the n block of ice, the number of penguins on each ice pack and the number of times they can withstand the jump, and ask what ice floe will bring the penguins together.

Analysis:

The limit of the point traffic is converted to the limit of the edge traffic, and the maximum flow is calculated.

Code:

POJ 3498//sep9 #include <iostream> #include <queue> #include <algorithm> #include <cmath> usin
G namespace Std;
const int maxn=128;
const int MAXM=40;
const int maxv=260;
const int maxe=26000; struct Edge {int u,v,f,nxt;}
E[MAXE*2+10];
Queue<int> que;
int src,sink;
int g[maxv+10];
int nume;
BOOL VIS[MAXV+10];

int dist[maxv+10];
Double X[MAXN],Y[MAXN];

int NUM[MAXN],M[MAXN];
	void Addedge (int u,int v,int c) {e[nume].u=u,e[nume].v=v;e[nume].f=c;e[nume].nxt=g[u];g[u]=nume++;
e[nume].u=v;e[nume].v=u;e[nume].f=0;e[nume].nxt=g[v];g[v]=nume++;	
	} void Init () {memset (g,0,sizeof (g));
nume=2;
	} int BFs () {while (!que.empty ()) Que.pop ();
	memset (dist,0,sizeof (Dist));
	memset (vis,0,sizeof (VIS));
	Vis[src]=true;	
	Que.push (SRC);
		while (!que.empty ()) {int U=que.front (); Que.pop ();
				for (int i=g[u];i;i=e[i].nxt) if (e[i].f&&!vis[e[i].v]) {Que.push (E[I].V);
				dist[e[i].v]=dist[u]+1; 
				Vis[e[i].v]=true;
	if (E[i].v==sink) return 1;		}} return 0;
	} int dfs (int u,int delta) {if (u==sink) return delta;
	int ret=0; for (int i=g[u];ret<delta&&i;i=e[i].nxt) if (e[i].f&&dist[e[i].v]==dist[u]+1) {int Dd=dfs (E[I].V,
			Min (E[i].f,delta-ret));
				if (dd>0) {e[i].f-=dd;
				E[I^1].F+=DD;
			RET+=DD;
		} else Dist[e[i].v]=-1;
} return ret;
	} int dinic () {int ret=0;
	while (BFS () ==1) Ret+=dfs (Src,int_max);	
return ret;
	} void Solve () {int i,j,t,n,sum;
	Double D;
	scanf ("%d%lf", &n,&d);
	Sum=0,src=2*n;
	int ok=0;
		for (i=0;i<n;++i) {scanf ("%lf%lf%d%d", &x[i],&y[i],&num[i],&m[i]);
	Sum+=num[i];		
		} for (t=0;t<n;++t) {init ();
			for (i=0;i<n;++i) {Addedge (src,i,num[i]);
		Addedge (I,i+n,m[i]); } for (I=0;i<n;++i) for (j=0;j<n;++j) if (i!=j) {double l=sqrt ((X[i]-x[j]) * (X[i]-x[j]) + (Y[i]-y[j]) * (y[i]-
					Y[J])); if (l<d| |
					Fabs (l-d) <1e-6) {Addedge (I+n,j,int_max);
		}} sink=t; if (Dinic () ==sum) {printf ("%d", t);
		ok=1;
	}} if (!ok) printf ("-1");
printf ("\ n");
	} int main () {int cases;
	scanf ("%d", &cases);
	while (cases--) solve ();	
return 0;   }


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.