码迷,mamicode.com
首页 > 其他好文 > 详细

BZOJ2929 [Poi1999]洞穴攀行

时间:2014-12-13 23:18:35      阅读:256      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   io   ar   color   os   sp   for   

我说这题怎么看着觉得怪怪的。。。

PoPoQQQ给出了解答:"给定一个有向图,与起点和终点相连的边只能走一次,剩下的边可以走无数次,问起点到终点可以走多少个人"

题目翻译错了233

好了,那不就是裸的网络流了吗?

 

bubuko.com,布布扣
 1 /**************************************************************
 2     Problem: 2929
 3     User: rausen
 4     Language: C++
 5     Result: Accepted
 6     Time:4 ms
 7     Memory:1792 kb
 8 ****************************************************************/
 9  
10 #include <cstdio>
11 #include <cstring>
12 #include <algorithm>
13  
14 using namespace std;
15 const int N = 205;
16 const int M = N * N;
17 const int inf = (int) 1e9;
18    
19 struct edges{
20     int next, to, f;
21     edges() {}
22     edges(int _n, int _t, int _f) : next(_n), to(_t), f(_f) {}
23 } e[M << 1];
24  
25 int n, m, S, T;
26 int tot = 1, first[N];
27 int d[N], q[N];
28  
29 inline int read() {
30     int x = 0;
31     char ch = getchar();
32     while (ch < 0 || 9 < ch)
33         ch = getchar();
34     while (0 <= ch && ch <= 9) {
35         x = x * 10 + ch - 0;
36         ch = getchar();
37     }
38     return x;
39 }
40  
41 inline void Add_Edges(int x, int y, int z) {
42     e[++tot] = edges(first[x], y, z), first[x] = tot;
43     e[++tot] = edges(first[y], x, 0), first[y] = tot;
44 }
45    
46 bool bfs() {
47     memset(d, 0, sizeof(d));
48     q[1] = S, d[S] = 1;
49     int l = 1, r = 1, x, y;
50     while (l != r + 1) {
51         for (x = first[q[l]]; x; x = e[x].next){
52             y = e[x].to;
53             if (!d[y] && e[x].f)
54                 q[(++r) %= N] = y, d[y] = d[q[l]] + 1;
55         }
56         (++l) %= N;
57     }
58     return d[T];
59 }
60    
61 int dinic(int p, int limit) {
62     if (p == T || !limit) return limit;
63     int x, y, tmp, rest = limit;
64     for (x = first[p]; x; x = e[x].next) {
65         y = e[x].to;
66         if (d[y] == d[p] + 1 && e[x].f && rest) { 
67             tmp = dinic(y, min(rest, e[x].f));
68             rest -= tmp;
69             e[x].f -= tmp, e[x ^ 1].f += tmp;
70             if (!rest) return limit;
71         }
72     }
73     if (limit == rest) d[p] = 0;
74     return limit - rest;
75 }
76    
77 int Dinic() {
78     int res = 0, x;
79     while (bfs())
80         res += dinic(S, inf);
81     return res;
82 }
83  
84 int main() {
85     int i, j, x;
86     n = read();
87     for (i = 1; i < n; ++i) {
88         for (j = 1, m = read(); j <= m; ++j) {
89             x = read();
90             Add_Edges(i, x, i == 1 || x == n ? 1 : inf);
91         }
92     }
93     S = 1, T = n;
94     printf("%d\n", Dinic());
95     return 0;
96 }
View Code

 

BZOJ2929 [Poi1999]洞穴攀行

标签:style   blog   http   io   ar   color   os   sp   for   

原文地址:http://www.cnblogs.com/rausen/p/4161906.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!