SGU 438 The Glorious Karlutka River =) 动态网络流
来源:互联网 发布:pdf.js 获取总页数 编辑:程序博客网 时间:2024/06/03 14:52
题意:给你一条宽为w的河 河中央有一些石头 有一些人在河的一面 它们想到河的另一面 每个人的弹跳距离为d 每个石头每一时刻有一个最大载人数量 问你所有人是否能跳到河对面 如果能 输出最小时间
思路:这道题并不是我们常做的静态网络流 因为每一秒中网络中的流量可能不相同 所以我们要用动态的思想
常见的动态网络流的题型为固定时间求最大通过流量 和 固定流量求最小通过时间 显然这道题属于第二种
建图方面 我们按时间拆点 因为如果可以到达河对岸 那么最长时间一定小于等于n+m 所以我们把原图拆成n+m个点 对于第i秒的石头 我们把第i-1秒的该石头连一条边指向它 代表在第i-1秒的人在这个石头上待了一秒没动 然后上一秒的石头a与它相连的下一秒的石头b相连 代表上一秒在a石头上的人花了一秒钟跳到了b石头上 然后源点与这一秒的石头相连 代表在岸边的人花了一秒钟的时间跳到第一个石头上 上一秒的石头与对岸相连 为什么是上一秒的石头呢? 因为是那个人在第i-1秒花了一秒 在第i秒才跳到了对岸 然后每一秒中跑最大流 记录最大流流量即可
这道题我与到一个问题 就是在windows上运行这个程序 输入 0 5 2 1 这组数据 它会蹦掉 但是我如果加上中间输出 它就莫名的不蹦了... 哪位大神知道其中的原因一定要帮我解答 感激不尽 T T
#include <cstdio>#include <cstring>#include <algorithm>#include <cmath>#include <cstdlib>#include <vector>using namespace std;#define REP( i, a, b ) for( int i = a; i < b; i++ )#define FOR( i, a, b ) for( int i = a; i <= b; i++ )#define CLR( a, x ) memset( a, x, sizeof a )#define CPY( a, x ) memcpy( a, x, sizeof a )const int maxn = 10000 + 1000;const int maxe = 600000 + 10;const int INF = 1e9;struct Edge{ int v, c, f; int next; Edge() {} Edge(int v, int c, int f, int next) : v(v), c(c), f(f), next(next) {}};struct ISAP{ int n, s, t; int num[maxn], cur[maxn], d[maxn], p[maxn]; int Head[maxn], cntE; int Q[maxn], head, tail; Edge edge[maxe]; void Init(int n){ this -> n = n; cntE = 0; CLR(Head, -1); } void Add(int u, int v, int c){ edge[cntE] = Edge(v, c, 0, Head[u]); Head[u] = cntE++; edge[cntE] = Edge(u, 0, 0, Head[v]); Head[v] = cntE++; } void Bfs(){ CLR(d, -1); CLR(num, 0); d[t] = 0; head = tail = 0; Q[tail++] = t; num[0] = 1; while(head != tail){ int u = Q[head++]; for(int i = Head[u]; ~i; i = edge[i].next){ Edge &e = edge[i]; if(~d[e.v]) continue; d[e.v] = d[u] + 1; Q[tail++] = e.v; num[d[e.v]] ++; } } } int Maxflow(int s, int t){ this -> s = s; this -> t = t; CPY(cur, Head); Bfs(); int flow = 0, u = p[s] = s; while(d[s] < n){ if(u == t){ int f = INF, neck; for(int i = s; i != t; i = edge[cur[i]].v){ if(f > edge[cur[i]].c - edge[cur[i]].f){ f = edge[cur[i]].c - edge[cur[i]].f; neck = i; } } for(int i = s; i != t; i = edge[cur[i]].v){ edge[cur[i]].f += f; edge[cur[i]^1].f -= f; } flow += f; u = neck; } int ok = 0; for(int i = cur[u]; ~i; i = edge[i].next){ Edge &e = edge[i]; if(e.c > e.f && d[e.v] + 1 == d[u]){ ok = 1; cur[u] = i; p[e.v] = u; u = e.v; break; } } if(!ok){ int m = n - 1; if(--num[d[u]] == 0) break; for(int i = Head[u]; ~i; i = edge[i].next){ Edge &e = edge[i]; if(e.c - e.f > 0 && m > d[e.v]){ cur[u] = i; m = d[e.v]; } } ++num[d[u] = m + 1]; u = p[u]; } } return flow; }}solver;const double eps = 1e-8;int dcmp(double x){ if(fabs(x) < eps) return 0; return x < 0 ? -1 : 1;}struct Point{ double x, y; int c; void read(){ scanf("%lf%lf%d", &x, &y, &c); } bool can(Point a, double d){ return dcmp((x - a.x) * (x - a.x) + (y - a.y) * (y - a.y) - d * d) <= 0; }}p[60];int n, m;int st, ed;double d, w;vector<int> G[60];int in(int T, int u){ return 2 * T * n + u; }int out(int T, int u){ return (2 * T + 1) * n + u; }void build_relationship(){ FOR(i, 1, n) FOR(j, i+1, n) if(p[i].can(p[j], d)){ G[i].push_back(j); G[j].push_back(i); } FOR(i, 1, n) if(p[i].y <= d) G[st].push_back(i);}void build_netflow(int t){ FOR(i, 1, n) solver.Add(in(t, i), out(t, i), p[i].c); REP(i, 0, (int)G[st].size()){ int tar = G[st][i]; solver.Add(st, in(t, tar), INF); } if(w <= d) solver.Add(st, ed, INF); FOR(i, 1, n) REP(j, 0, (int)G[i].size()){ int tar = G[i][j]; solver.Add(out(t-1, i), in(t, tar), INF); } FOR(i, 1, n) if(p[i].y + d >= w) solver.Add(out(t-1, i), ed, INF);}void solve(){ scanf("%d%d%lf%lf", &n, &m, &d, &w); st = 0, ed = (n + m + 1) * n * 2 + 1; solver.Init(ed + 1); //printf("....\n"); FOR(i, 1, n) p[i].read(); build_relationship(); if(m == 0){ printf("0\n"); return; } int sum = 0, ans = 0; FOR(i, 1, m + n){ build_netflow(i); sum += solver.Maxflow(st, ed); if(sum >= m){ ans = i; break; } } if(sum >= m) printf("%d\n", ans); else printf("IMPOSSIBLE\n");}int main(){ solve(); return 0;}/*0 5 2 1*/
0 0
- SGU 438 The Glorious Karlutka River =) 动态网络流
- sgu 438 The Glorious Karlutka River =) 网络流 动态流
- sgu 438 The Glorious Karlutka River =)(动态网络流)
- Sgu 438 The Glorious Karlutka River(动态流)
- sgu 438 The Glorious Karlutka River 【网络流 分层】
- SGU 438 -- The Glorious Karlutka River =)
- SGU 438. The Glorious Karlutka River =) (网络流,动态流问题)
- SGU 438 The Glorious Karlutka River =) 动态流 最大流
- SGU 438 - The Glorious Karlutka River =)(网络流‘最大流)
- sug438 The Glorious Karlutka River =)(枚举+最大流)
- 【SGU438】The Glorious Karlutka River =) 分层图最大流
- 438. The Glorious Karlutka River =)
- 解题报告 之 SGU438 The Glorious Karlutka River 时间流(动态流)
- 解题报告 之 SGU438 The Glorious Karlutka River
- 动态网络流 SGU 438
- SGU 438 动态流
- SGU 438 动态网络流,SGU挂了,我也不知道A没A
- SGU 326 Perspective ( 网络流 )
- Android实战技巧之四十:Android5.1.1源代码编译与烧写
- 【socket】有关socket的编程<一>
- SDUTOJ------3306
- IP地址分类
- Assetbundle的原理
- SGU 438 The Glorious Karlutka River =) 动态网络流
- Servlet注册功能的实现
- u-boot-2009.11移植(适用于TQ2440和MINI2440)第一篇:建立开发板文件,测试编译环境
- HDU 1569 方格取数(2)(最大点权独立集)
- 类再生
- 31 Next Permutation
- 指针之指针数组
- 笔记本无线WiFi生成代码
- makefile