(beginer) 网络流(拆点)UVA 12125 - March of the Penguins

Problem B - March of the Penguins

Time limit: 4 seconds

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.

(beginer) 网络流(拆点)UVA 12125 - March of the Penguins - 恶魔仁 - 恶魔仁
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 ≤ 100000), denoting the number of ice pieces and the maximum distance a penguin can jump.
  • N lines, each line containing xi, yi, ni 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 (-10000 ≤ xi, yi ≤ 10000, 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

2
5 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
The 2007 ACM Northwestern European Programming Contest 

题意:在南极有一些冰块(当做是吧),上面会有一企鹅,这些企鹅想要通过一定的跳跃,跳到同一块冰块上面见面,但是由于温度在上升,冰块变得不那么结实,你现在知道每个冰块上面最多能跳多少次,也知道企鹅一次最多能跳多远,也知道冰块的位置,问最后企鹅能在那些冰块见面。

思路:我们把每个冰块当做一个点,能互相到达就连一条边,容量为无穷大,然后我们把每个点都拆成两个点,它们连一条容量为跳跃限制的数目。然后加一个源点,连向所有有企鹅的点,容量为那个冰块的企鹅的数量。最后我们枚举每个冰块作为汇点,套网络流的模板,最大流是不是所有企鹅的数目就行了。

代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string.h>
#include<vector>
#include<queue>
#include<math.h>
using namespace std;
const int maxn = 200+5;
const int inf = 1e8;
#define LL long long 
#define eps 1e-8
int n , all_peng;
double D;
struct Edge
{
Edge(int uu,int vv,int cap,int flow)
: u(uu) , v(vv) , cap(cap) , flow(flow) { }
int u , v;
int cap , flow;
};

vector<Edge> edge;
vector<int> G[maxn];

void add(int s,int t,int cap)
{
edge.push_back(Edge(s,t,cap,0));
edge.push_back(Edge(t,s,0,0));
int x = edge.size();
G[s].push_back(x-2);
G[t].push_back(x-1);
}

struct ISAP
{
int num[maxn] , d[maxn] , p[maxn];
int cur[maxn];
int s , t;
vector<Edge> edge;
// vector<int> G[maxn];
void init(int x) {
edge = ::edge;
// for (int i =  0 ; i <= 2*n+1 ; ++i) G[i] = ::G[i];
}

int Augment() {
int a = inf , x = t;
while (x!=s) {
Edge & e = edge[p[x]];
a = min(a,e.cap-e.flow);
x = e.u;
}
x = t;
while (x!=s) {
edge[p[x]].flow += a;
edge[p[x]^1].flow -= a;
x = edge[p[x]].u;
}
return a;
}

void bfs() {
queue<int> q; q.push(t);
for (int i = 0 ; i <= 2*n ; ++i) d[i] = inf;
d[t] = 0;
while (q.size()) {
int u = q.front(); q.pop();
for (int i = 0 ; i < G[u].size() ; ++i) {
Edge & e = edge[G[u][i]];
if (e.cap==0 && d[e.v]==inf) {
d[e.v] = d[u]+1;
q.push(e.v);
}
}
}
}

int maxflow(int s,int t) {
this->s = s , this->t = t;
memset(num,0,sizeof(num));
memset(cur,0,sizeof(cur));
bfs();
int flow = 0 , x = s;
for (int i = 0 ; i <= 2*n+1 ; ++i) 
if (d[i]!=inf) ++num[d[i]];
while (d[s] < 2*n+1) {
if (x==t) {
flow += Augment();
x = s;
}
bool ok = false;
for (int i = cur[x] ; i < G[x].size() ; ++i) {
Edge & e = edge[G[x][i]];
if (e.cap > e.flow && d[x]==d[e.v]+1) {
p[e.v] = G[x][i];
cur[x] = i;
ok = true;
x = e.v;
break;
}
}
if (!ok) {
int k = 2*n;
for (int i = 0 ; i < G[x].size() ; ++i) {
Edge & e = edge[G[x][i]];
if (e.cap > e.flow) k = min(k,d[e.v]);
}
if (--num[d[x]]==0) break;
++num[d[x]=k+1];
cur[x] = 0;
if (x!=s) x = edge[p[x]].u;
}
}
return flow;
}
}solver;

struct Point
{
int x , y;
int peng;
int jump;
}p[maxn];

inline double sqr(LL x) { return x * x; }
double dist(const Point & p1 , const Point & p2) 
{
return sqrt(sqr(p1.x-p2.x)+sqr(p1.y-p2.y));
}

void input()
{
scanf("%d%lf",&n,&D);
all_peng = 0;
for (int i = 0 ; i < maxn ; ++i) G[i].clear();
for (int i = 1 ; i <= n ; ++i) {
Point& pt = p[i];
scanf("%d%d%d%d",&pt.x,&pt.y,&pt.peng,&pt.jump);
all_peng += pt.peng;
}
}

void BuildGraph()
{
for (int i = 1 ; i <= n ; ++i) {
Point& pt = p[i];
add(i,i+n,pt.jump);
if (pt.peng!=0) add(0,i,pt.peng);
for (int j = 1 ; j <= n ; ++j) if (i!=j) {
if (dist(p[i],p[j]) < D+eps) {
add(i+n,j,inf);
// printf("%d %d\n",i+n,j);
}
}
}
}

void solve()
{
vector<int> ans;
for (int i = 1 ; i <= n ; ++i) {
solver.init(i);
int flow = solver.maxflow(0,i);
if (flow==all_peng) ans.push_back(i);
}
if (ans.size()==0) { printf("-1\n"); return; }
printf("%d",ans[0]-1);
for (int i = 1 ; i < ans.size() ; ++i)
printf(" %d",ans[i]-1);
printf("\n");
}

int main()
{
int T; cin>>T;
while (T--) {
input();
BuildGraph();
solve();
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值