建分层图,每一层表示一天的情况

从S向第0层的1号点连边,每层的n向T连INF的边

枚举天数,每多一天就多建一层然后跑最大流,如果当前流量大于人数则输出答案

由于路径长度不会超过n,因此tot个人走这条路径总天数不会超过tot + n,故只需要建tot + n层即可

 /**************************************************************
Problem: 1570
User: rausen
Language: C++
Result: Accepted
Time:24 ms
Memory:15936 kb
****************************************************************/ #include <cstdio>
#include <cstring>
#include <algorithm> using namespace std;
const int N = ;
const int M = ;
const int inf = 1e8; inline int read() {
int x = ;
char ch = getchar();
while (ch < '' || '' < ch)
ch = getchar();
while ('' <= ch && ch <= '') {
x = x * + ch - '';
ch = getchar();
}
return x;
} struct Edge {
int x, y, z; inline void read_in() {
x = read(), y = read(), z = read();
}
} E[M]; struct edge {
int next, to, f;
edge() {}
edge(int _n, int _t, int _f) : next(_n), to(_t), f(_f) {}
} e[M << ]; int n, m, t;
int S, T, ans;
int first[M], tot = ;
int d[M], q[M]; inline void Add_Edges(int x, int y, int f) {
e[++tot] = edge(first[x], y, f), first[x] = tot;
e[++tot] = edge(first[y], x, ), first[y] = tot;
} #define y e[x].to
#define p q[l]
bool bfs() {
int l, r, x;
memset(d, -, sizeof(d));
d[q[] = S] = ;
for (l = r = ; l != r + ; ++l)
for (x = first[p]; x; x = e[x].next)
if (!~d[y] && e[x].f) {
d[q[++r] = y] = d[p] + ;
if (y == T) return ;
}
return ;
}
#undef p int dfs(int p, int lim) {
if (p == T || !lim) return lim;
int x, tmp, rest = lim;
for (x = first[p]; x && rest; x = e[x].next)
if (d[y] == d[p] + && ((tmp = min(e[x].f, rest)) > )) {
rest -= (tmp = dfs(y, tmp));
e[x].f -= tmp, e[x ^ ].f += tmp;
if (!rest) return lim;
}
if (rest) d[p] = -;
return lim - rest;
}
#undef y int Dinic() {
int res = ;
while (bfs())
res += dfs(S, inf);
return res;
} int main() {
int i, j;
n = read(), m = read(), t = read();
for (i = ; i <= m; ++i) E[i].read_in();
S = , T = ;
Add_Edges(S, , t);
for (i = ; i <= n + t; ++i) {
for (j = ; j <= m; ++j)
Add_Edges(i * n - n + E[j].x + , i * n + E[j].y + , E[j].z);
for (j = ; j <= n; ++j)
Add_Edges(i * n - n + j + , i * n + j + , inf);
Add_Edges(i * n + n + , T, inf);
ans += Dinic();
if (ans == t) {
printf("%d\n", i);
return ;
}
}
}

最新文章

  1. SPOJ GSS1 Can you answer these queries I[线段树]
  2. StringBuffer and StringBuilder
  3. VMware虚拟机打开不了操作系统的解决方案
  4. HDU 5710 Digit-Sum (构造)
  5. 转:postgresql:pg_restore: [archiver] input file does not appear to be a valid archive的解决方法
  6. QPointer很大程度上避免了野指针(使用if语句判断即可,类似于dynamic_cast),而且使用非常方便 good
  7. 【转】OpenCV中使用神经网络 CvANN_MLP
  8. HDU1698_Just a Hook(线段树/成段更新)
  9. python网络爬虫进入(一)——简单的博客爬行动物
  10. 一、SOAP简单对象访问协议讲解
  11. switch 在什么时候可以不写default
  12. Emmet 快速编写html代码
  13. Hibernate之ORM与Hibernate
  14. SSM-SpringMVC-14:SpringMVC中大话注解式开发基础--呕心沥血版
  15. Django的ORM操作
  16. 一次node-sass安装记录
  17. hikey960编译记录
  18. P1169 [ZJOI2007]棋盘制作 DP悬线法
  19. The server principal &quot;sa&quot; is not able to access the database &quot;xxxx&quot; under the current security context
  20. 正点原子stm32f103mini版串口下载BOOT0引脚与与CH340G芯片引脚RTS、DTR、的关系原理

热门文章

  1. python_way day10 python和其他语言的作用域 、 python2.7多继承和3.5多继承的区别 、 socket 和 socketserver源码(支持并发处理socket,多进程,多线程)
  2. jQuery:show()方法
  3. 如何设置table中&lt;tr&gt;和&lt;td&gt;的高度
  4. js自定义弹窗
  5. linux GO语言配置安装
  6. openfire消息通知推送
  7. git tag推送小分析
  8. jquery: 一些常见的获取
  9. phalcon: 上下文转义
  10. 树 - 从零开始实现by C++