标签:
题目大意:有n个冰块,每块冰块能承受mi只企鹅从上面跳走
初始时每个冰块上有ai只企鹅,每只企鹅跳跃的最远距离为d,要求所有的企鹅在同一片冰块上集合,问哪些冰块满足要求
解题思路:这题和HDU - 2732 Leapin’ Lizards
类似,具体的就不讲了,枚举+最大流就可以了
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <cmath>
using namespace std;
#define M 1000010
#define N 10010
#define INF 0x3f3f3f3f
struct Edge{
int u, v, cap, flow, next;
}E[M];
struct Dinic{
int head[N], d[N];
int tot, sink, source;
void init() {
memset(head, -1, sizeof(head));
tot = 0;
}
inline void AddEdge(int u, int v, int cap) {
E[tot].u = u; E[tot].v = v; E[tot].cap = cap; E[tot].flow = 0; E[tot].next = head[u]; head[u] = tot++;
u = u ^ v; v = u ^ v; u = u ^ v;
E[tot].u = u; E[tot].v = v; E[tot].cap = 0; E[tot].flow = 0; E[tot].next = head[u]; head[u] = tot++;
}
inline bool bfs(int s) {
int u, v;
memset(d, 0, sizeof(d));
queue<int> Q;
Q.push(s);
d[s] = 1;
while (!Q.empty()) {
u = Q.front(); Q.pop();
if (u == sink) return true;
for (int i = head[u]; ~i; i = E[i].next) {
v = E[i].v;
if (!d[v] && E[i].cap - E[i].flow > 0) {
d[v] = d[u] + 1;
Q.push(v);
}
}
}
return false;
}
int dfs(int x, int a) {
if (x == sink || a == 0)
return a;
int f, flow = 0;
for (int i = head[x]; ~i; i = E[i].next) {
int v = E[i].v;
if (d[v] == d[x] + 1 && E[i].cap - E[i].flow > 0) {
f = dfs(v, min(a, E[i].cap - E[i].flow));
E[i].flow += f;
E[i^1].flow -= f;
flow += f;
a -= f;
if (!a) break;
}
}
if (flow == 0) d[x] = 0;
return flow;
}
int Maxflow(int source, int sink) {
int flow = 0;
this->sink = sink;
while (bfs(source)) flow += dfs(source, INF);
return flow;
}
};
Dinic dinic;
#define maxn 110
#define esp 1e-5
struct Ice{
int x, y, n, m;
}ice[maxn];
double dis[maxn][maxn];
double Max;
int n, Sum;
double distance(int i, int j) {
int x = ice[i].x - ice[j].x;
int y = ice[i].y - ice[j].y;
return sqrt(1.0 * x * x + 1.0 * y * y);
}
void init() {
scanf("%d%lf", &n, &Max);
Sum = 0;
for (int i = 1; i <= n; i++) {
scanf("%d%d%d%d", &ice[i].x, &ice[i].y, &ice[i].n, &ice[i].m);
Sum += ice[i].n;
}
for (int i = 1; i <= n; i++)
for (int j = i + 1; j <= n; j++)
dis[i][j] = dis[j][i] = distance(i, j);
}
void solve() {
bool flag = false;
int source = 0, sink = N - 1;
for (int i = 1; i <= n; i++) {
dinic.init();
for (int j = 1; j <= n; j++) {
if (i == j)
dinic.AddEdge(j * 2, sink, INF);
dinic.AddEdge(j * 2, (j * 2) + 1, ice[j].m);
dinic.AddEdge(source, j * 2, ice[j].n);
for (int k = j + 1; k <= n; k++)
if (Max - dis[j][k] > esp) {
dinic.AddEdge((j * 2) + 1, k * 2, INF);
dinic.AddEdge((k * 2) + 1, j * 2, INF);
}
}
int maxflow = dinic.Maxflow(source, sink);
if ( maxflow == Sum) {
if (flag)
printf(" ");
flag = true;
printf("%d", i - 1);
}
}
if (!flag)
printf("-1");
printf("\n");
}
int main() {
int test;
scanf("%d", &test);
while (test--) {
init();
solve();
}
return 0;
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
UVALive - 3972 March of the Penguins(最大流+枚举)
标签:
原文地址:http://blog.csdn.net/l123012013048/article/details/48120361