lightoj 1154 - Penguins 【拆点建图后枚举汇点 满流判可行解】
来源:互联网 发布:百电通打电话要网络 编辑:程序博客网 时间:2024/05/16 09:12
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
Input starts with an integer T (≤ 25), denoting the number of test cases.
Each case starts with the integer N (1 ≤ N ≤ 100) and a floating-point number D (0 ≤ D ≤ 105), denoting the number of ice pieces and the maximum distance a penguin can jump. After that there will be 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
For each case of input, print the case number and a space-separated list of 0-based indices of the pieces on which all penguins can meet. If no such piece exists, output -1.
Sample Input
Output for 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
Case 1: 1 2 4
Case 2: -1
#include <cstdio>#include <cstring>#include <algorithm>#include <cmath>#include <queue>#define MAXN 200+10#define MAXM 100000+10#define INF 0x3f3f3f3fusing namespace std;struct MAXFLOW{ struct Edge{ int from, to, cap, flow, next; }; Edge edge[MAXM], Redge[MAXM]; int head[MAXN], edgenum; int Rhead[MAXN]; int dist[MAXN], cur[MAXN]; bool vis[MAXN]; void init(){ edgenum = 0; memset(head, -1, sizeof(head)); } void addEdge(int u, int v, int w) { Edge E1 = {u, v, w, 0, head[u]}; edge[edgenum] = E1; head[u] = edgenum++; Edge E2 = {v, u, 0, 0, head[v]}; edge[edgenum] = E2; head[v] = edgenum++; } bool BFS(int s, int t) { queue<int> Q; memset(dist, -1, sizeof(dist)); memset(vis, false, sizeof(vis)); dist[s] = 0, vis[s] = true; Q.push(s); while(!Q.empty()) { int u = Q.front(); Q.pop(); for(int i = head[u]; i != -1; i = edge[i].next) { Edge E = edge[i]; if(!vis[E.to] && E.cap > E.flow) { dist[E.to] = dist[u] + 1; if(E.to == t) return true; vis[E.to] = true; Q.push(E.to); } } } return false; } int DFS(int x, int a, int t) { if(x == t || a == 0) return a; int flow = 0, f; for(int &i = cur[x]; i != -1; i = edge[i].next) { Edge &E = edge[i]; if(dist[E.to] == dist[x] + 1 && (f = DFS(E.to, min(a, E.cap-E.flow), t)) > 0) { edge[i].flow += f; edge[i^1].flow -= f; flow += f; a -= f; if(a == 0) break; } } return flow; } int Maxflow(int s, int t) { int flow = 0; while(BFS(s, t)) { memcpy(cur, head, sizeof(head)); flow += DFS(s, INF, t); } return flow; }};MAXFLOW dinic;int n;double limit;int S, T;double dis(double x1, double y1, double x2, double y2){ return sqrt((x1-x2) * (x1-x2) + (y1-y2) * (y1-y2));}double x[110], y[110];int num[110], can[110];void getMap(){ dinic.init(); for(int i = 1; i <= n; i++) { if(num[i]) dinic.addEdge(S, i, num[i]); dinic.addEdge(i, i+n, can[i]); for(int j = i+1; j <= n; j++) if(dis(x[i], y[i], x[j], y[j]) <= limit) dinic.addEdge(i+n, j, INF), dinic.addEdge(j+n, i, INF); }}int kcase = 1;void solve(){ scanf("%d%lf", &n, &limit); int sumflow = 0; for(int i = 1; i <= n; i++) scanf("%lf%lf%d%d", &x[i], &y[i], &num[i], &can[i]), sumflow += num[i]; //memcpy(dinic.Rhead, dinic.head, sizeof(dinic.head)); //memcpy(dinic.Redge, dinic.edge, sizeof(dinic.edge)); int have = 0; S = 0, T = 2*n+1; printf("Case %d: ", kcase++); for(int i = 1; i <= n; i++) { getMap(); dinic.addEdge(i, T, INF); if(dinic.Maxflow(S, T) == sumflow)//满流 { if(have) printf(" "); printf("%d", i-1); have++; } } if(have == 0) printf("-1\n"); else printf("\n");}int main(){ int t; scanf("%d", &t); while(t--){ solve(); } return 0;}
- lightoj 1154 - Penguins 【拆点建图后枚举汇点 满流判可行解】
- LightOJ 1154 - Penguins 最大流
- Ligthoj 1154--Penguins【最大流 && 拆点】
- loj 1407(2-sat + 枚举 + 输出一组可行解 )
- loj 1154(最大流+枚举汇点)
- POJ 3498 March of the Penguins(枚举+最大流)
- UVALive - 3972 March of the Penguins(最大流+枚举)
- POJ 3498 March of the Penguins(枚举+最大流)
- POJ 3498 March of the Penguins(枚举+最大流)
- LightOJ 1251 Forming the Council【2-Sat+逆向拓扑排序输出可行解】好题!
- lightoj-1014【枚举】
- 暴力枚举之八皇后问题(可行解个数,打印可行解纵坐标,棋盘有洞,n皇后)
- codeforces 366D 求1-n点最大区间长度 枚举区间左端+二分区间右端dfs判可行
- Light OJ 1407 - Explosion 【2-sat之 3布尔变量的处理 — 枚举所有状态判断是否存在可行解 + 反向输出可行解】【好题】
- POJ 3498 —— March of the Penguins (枚举+最大流)
- 2-sat 输出任意一组可行解&拓扑排序+缩点 poj3683
- poj 3498 (最大流,枚举汇点)
- La (NWERC2007 Problem B )- March of the Penguins(最大流 拆点)
- 如何禁止函数的传值调用
- 【Linux】RedHat9.0添加module和编译内核
- Hibernate3.6 入门(笔记二):映射_inverse_lazy
- tcpdump+wireshark分析数据笔记(2)
- 关于validate验证表单与js语法的一个bug
- lightoj 1154 - Penguins 【拆点建图后枚举汇点 满流判可行解】
- 函数指针与指针函数
- OpenCV之滑动条
- C#程序员经常用到的10个实用代码片段
- Android应用程序的消息处理机制
- hibernate二级缓存VS查询缓存
- 利用大白菜制作多系统启动U盘(ubuntu+windows)
- ViewStub
- Telnet使用