首页 > 代码库 > hust 1164 4 Rain on your Parade
hust 1164 4 Rain on your Parade
题目描述
You’re giving a party in the garden of your villa by the sea. The party is a huge success, and everyone is here. It’s a warm, sunny evening, and a soothing wind sends fresh, salty air from the sea. The evening is progressing just as you had imagined. It could be the perfect end of a beautiful day. But nothing ever is perfect. One of your guests works in weather forecasting. He suddenly yells, “I know that breeze! It means its going to rain heavily in just a few minutes!” Your guests all wear their best dresses and really would not like to get wet, hence they stand terrified when hearing the bad news. You have prepared a few umbrellas which can protect a few of your guests. The umbrellas are small, and since your guests are all slightly snobbish, no guest will share an umbrella with other guests. The umbrellas are spread across your (gigantic) garden, just like your guests. To complicate matters even more, some of your guests can’t run as fast as the others. Can you help your guests so that as many as possible find an umbrella before it starts to pour? Given the positions and speeds of all your guests, the positions of the umbrellas, and the time until it starts to rain, find out how many of your guests can at most reach an umbrella. Two guests do not want to share an umbrella, however.
输入
The input starts with a line containing a single integer, the number of test cases. Each test case starts with a line containing the time t in minutes until it will start to rain (1 ≤ t ≤ 5). The next line contains the number of guests m (1 ≤ m ≤ 3000), followed by m lines containing x- and y-coordinates as well as the speed si in units per minute (1 ≤ si ≤ 3000) of the guest as integers, separated by spaces. After the guests, a single line contains n (1 ≤ n ≤ 3000), the number of umbrellas, followed by n lines containing the integer coordinates of each umbrella, separated by a space. The absolute value of all coordinates is less than 10000.
输出
For each test case, write a line containing “Scenario #i:”, where i is the number of the test case starting at 1. Then, write a single line that contains the number of guests that can at most reach an umbrella before it starts to rain. Terminate every test case with a blank line.
样例输入
2 1 2 1 0 3 3 0 3 2 4 0 6 0 1 2 1 1 2 3 3 2 2 2 2 4 4
样例输出
Scenario #1: 2 Scenario #2: 2
这个题就是一个二分图最大匹配,不过数据实在太大,简单的匈牙利算法过不了,我就用最大流来做,不过算一算,最坏情况下,内存的消耗为3000*3000*4*2==48000000;这样内存肯定超了,怎么办,只有Hopcroft-Karp算法了,具体的我就不说了,给出一个内存超限的程序,希望过路者帮忙看看能不能再优化
#include<map> #include<set> #include<stack> #include<queue> #include<cmath> #include<vector> #include<cstdio> #include<string> #include<cstring> #include<cstdlib> #include<iostream> #include<algorithm> #define inf 0x0f0f0f0f using namespace std; const double pi=acos(-1.0); const double eps=1e-8; typedef pair<int,int>pii; const int maxn=6000+10; struct Edge { int from,to,cap,flow; }; int n,m,s,t; vector<Edge>edges; vector<int>G[maxn]; int d[6010],cur[6010]; bool vis[6010]; void AddEdge(int from,int to,int cap) { Edge temp; temp.cap=cap; temp.flow=0; temp.from=from; temp.to=to; edges.push_back(temp); temp.cap=0; temp.flow=0; temp.from=to; temp.to=from; edges.push_back(temp); m=edges.size(); G[from].push_back(m-2); G[to].push_back(m-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 Dinic() { int flow=0; while (BFS()) { memset(cur,0,sizeof(cur)); flow+=DFS(s,inf); } return flow; } void init() { for (int i=0;i<=maxn;i++) G[i].clear(); edges.clear(); } float MX[3001],MY[3001],UX[3001],UY[3001],speed[3001],ttime; void build(int M,int N) { for (int i=1;i<=M;i++) for (int j=1;j<=N;j++) { if ((ttime*speed[i]-sqrt((MX[i]-UX[j])*(MX[i]-UX[j])+(MY[i]-UY[j])*(MY[i]-UY[j])))>=0) AddEdge(i,j+M,1); } for (int i=1;i<=M;i++) AddEdge(s,i,1); for (int i=1;i<=N;i++) AddEdge(i+M,t,1); } int main() { //freopen("in.txt","r",stdin); int M,N,T; scanf("%d",&T); for (int k=1;k<=T;k++) { init(); scanf("%f",&ttime); scanf("%d",&M); for (int i=1;i<=M;i++) scanf("%f%f%f",&MX[i],&MY[i],&speed[i]); scanf("%d",&N); for (int i=1;i<=N;i++) scanf("%f%f",&UX[i],&UY[i]); s=0;t=N+M+1; build(M,N); printf("Scenario #%d:\n%d\n\n",k,Dinic()); init(); } //fclose(stdin); return 0; }