POJ 2391 Ombrophobic Bovines(Floyd+二分+最大流)

题目链接

题意:农场有F(1 <= F <= 200)片草地用于放牛,这些草地有P(1 <= P <= 1500)连接,农场的草地上有一些避雨点,奶牛们可以在避雨点避雨,但是避雨点的大小是有限的,所以有的时候需要经过道路去其他避雨点避雨,而经过道路是需要时间的,求出所有奶牛进入避雨点所需要的最小时间,如果不能是所有奶牛进入避雨点,输出-1;每块草地上的奶牛数量范围为(1—1000),草地避雨点的范围为(1--1000)经过道路的时间范围为(1--1000000000)

floyd:首先需要用floyd计算奶牛距离他最近的避雨点,也就是任意两块草地的距离;因为奶牛需要用最少的时间到达避雨点

二分:其次这是100000000,然后1500条边,最后floyd计算出的时间可能会达到1000000000000,所以需要用到long long,然后二分去计算中间时间能否让所有奶牛进入避雨点,一次次去靠近正确答案;

最大流:用二分的时间上限去建图,用最大流去计算该时间上限能否让所有奶牛进入避雨点,不行则在去二分;

再说几个坑点:

1)一定要用long long ,不然会wa;

2)数组也别太大,可能会tle

3)在用结构体封装的dinic模板的时候,一定要在清除旧的数据,别新开,会tle

其实下面的代码还可以在优化的,比如把vector换成链式前向星

AC代码+部分测试数据

#include<iostream>
#include<cstdio>
#include<cmath>
#include<vector>
#include<queue>
#include<cstring>
#define ll long long
using namespace std;
const int maxn = 405;
const ll INF = 1e16;
int N,M;
ll Map[maxn][maxn];
int cow[maxn],sh[maxn];

struct Edge
{
    int from,to,cap,flow;
    Edge(int u,int v,int c,int f):from(u),to(v),cap(c),flow(f){}
};

void init(){
    for(int i=1;i<=N;i++){
        for(int j=1;j<=N;j++){
            if(i==j)Map[i][j]=0;
            else Map[i][j]=INF;
        }
    }
}

struct Dinic
{
    int n,m,s,t;//结点数,边数(包括反向弧),源点编号,汇点编号
    vector<Edge>edges;//边表,dges[e]和dges[e^1]互为反向弧
    vector<int>G[maxn];//邻接表,G[i][j]表示结点i的第j条边在e数组中的编号
    bool vis[maxn]; //BFS的使用
    int d[maxn]; //从起点到i的距离
    int cur[maxn]; //当前弧下标

    void Init(int n){
        this->n=n;
        for(int i=0;i<n;i++) G[i].clear();
        edges.clear();
    }
    
    void addedge(int from,int to,int cap)
    {
        edges.push_back(Edge(from,to,cap,0));
        edges.push_back(Edge(to,from,0,0));
        int  m=edges.size();
        G[from].push_back(m-2);
        G[to].push_back(m-1);
    }

    bool bfs()
    {
        memset(vis,0,sizeof(vis));
        queue<int>Q;
        Q.push(s);
        d[s]=0;
        vis[s]=1;
        while(!Q.empty())
        {
            int x=Q.front();Q.pop();
            for(int i=0;i<G[x].size();i++)
            {
                Edge&e=edges[G[x][i]];
                if(!vis[e.to]&&e.cap>e.flow)//只考虑残量网络中的弧
                {
                    vis[e.to]=1;
                    d[e.to]=d[x]+1;
                    Q.push(e.to);
                }
            }

        }
        return vis[t];
    }

    int dfs(int x,int a)//x表示当前结点,a表示目前为止的最小残量
    {
        if(x==t||a==0)return a;//a等于0时及时退出,此时相当于断路了
        int flow=0,f;
        for(int&i=cur[x];i<G[x].size();i++)//从上次考虑的弧开始,注意要使用引用,同时修改cur[x]
        {
            Edge&e=edges[G[x][i]];//e是一条边
            if(d[x]+1==d[e.to]&&(f=dfs(e.to,min(a,e.cap-e.flow)))>0)
            {
                e.flow+=f;
                edges[G[x][i]^1].flow-=f;
                flow+=f;
                a-=f;
                if(!a)break;//a等于0及时退出,当a!=0,说明当前节点还存在另一个曾广路分支。

            }
        }
        return flow;
    }

    int Maxflow(int s,int t)//主过程
    {
        this->s=s,this->t=t;
        int flow=0;
        while(bfs())//不停地用bfs构造分层网络,然后用dfs沿着阻塞流增广
        {
            memset(cur,0,sizeof(cur));
            flow+=dfs(s,INF);
        }
        return flow;
    }
}din;

int main(){
    while(~scanf("%d%d",&N,&M)){
        init();
        int S=0,T=2*N+1;
        int nowc=0;
        for(int i=1;i<=N;i++){
            scanf("%d%d",&cow[i],&sh[i]);
            nowc+=cow[i];
        }
        int u,v;
        ll MIN=-1,w;
        while(M--){
            scanf("%d%d%lld",&u,&v,&w);
            if(w<Map[u][v]){
                Map[u][v]=Map[v][u]=w;
                MIN=max(MIN,w);
            }
        }
        for(int k=1;k<=N;k++){//floyd建立最短路
            for(int i=1;i<=N;i++){
                for(int j=1;j<=N;j++){
                    if(Map[i][k]!=INF&&Map[k][j]!=INF){
                        Map[i][j]=min(Map[i][j],Map[i][k]+Map[k][j]);
                        MIN=max(MIN,Map[i][j]);
                    }
                }
            }
        }
        ll high=MIN+1,low=0,mid,ans=-1;
        while(low<high){
            mid=(low+high)>>1;  
            din.Init(2*N+1);
            for(int i=1;i<=N;i++){
                din.addedge(S,i,cow[i]);
                din.addedge(i+N,T,sh[i]);
                din.addedge(i,i+N,0x3f3f3f3f);
                for(int j=i+1;j<=N;j++){
                    if(Map[i][j]<=mid){
                        din.addedge(i,j+N,0x3f3f3f3f);
                        din.addedge(j,i+N,0x3f3f3f3f);
                    }
                }
            }
            int sum=din.Maxflow(S,T);
            if(sum==nowc)
                ans=high=mid;
            else 
                low=mid+1;
        }
        printf("%lld\n",ans);
    }
    return 0;
}

测试数据:

input:
200 199
1000 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 1000
1 2 1000000000
2 3 1000000000
3 4 1000000000
4 5 1000000000
5 6 1000000000
6 7 1000000000
7 8 1000000000
8 9 1000000000
9 10 1000000000
10 11 1000000000
11 12 1000000000
12 13 1000000000
13 14 1000000000
14 15 1000000000
15 16 1000000000
16 17 1000000000
17 18 1000000000
18 19 1000000000
19 20 1000000000
20 21 1000000000
21 22 1000000000
22 23 1000000000
23 24 1000000000
24 25 1000000000
25 26 1000000000
26 27 1000000000
27 28 1000000000
28 29 1000000000
29 30 1000000000
30 31 1000000000
31 32 1000000000
32 33 1000000000
33 34 1000000000
34 35 1000000000
35 36 1000000000
36 37 1000000000
37 38 1000000000
38 39 1000000000
39 40 1000000000
40 41 1000000000
41 42 1000000000
42 43 1000000000
43 44 1000000000
44 45 1000000000
45 46 1000000000
46 47 1000000000
47 48 1000000000
48 49 1000000000
49 50 1000000000
50 51 1000000000
51 52 1000000000
52 53 1000000000
53 54 1000000000
54 55 1000000000
55 56 1000000000
56 57 1000000000
57 58 1000000000
58 59 1000000000
59 60 1000000000
60 61 1000000000
61 62 1000000000
62 63 1000000000
63 64 1000000000
64 65 1000000000
65 66 1000000000
66 67 1000000000
67 68 1000000000
68 69 1000000000
69 70 1000000000
70 71 1000000000
71 72 1000000000
72 73 1000000000
73 74 1000000000
74 75 1000000000
75 76 1000000000
76 77 1000000000
77 78 1000000000
78 79 1000000000
79 80 1000000000
80 81 1000000000
81 82 1000000000
82 83 1000000000
83 84 1000000000
84 85 1000000000
85 86 1000000000
86 87 1000000000
87 88 1000000000
88 89 1000000000
89 90 1000000000
90 91 1000000000
91 92 1000000000
92 93 1000000000
93 94 1000000000
94 95 1000000000
95 96 1000000000
96 97 1000000000
97 98 1000000000
98 99 1000000000
99 100 1000000000
100 101 1000000000
101 102 1000000000
102 103 1000000000
103 104 1000000000
104 105 1000000000
105 106 1000000000
106 107 1000000000
107 108 1000000000
108 109 1000000000
109 110 1000000000
110 111 1000000000
111 112 1000000000
112 113 1000000000
113 114 1000000000
114 115 1000000000
115 116 1000000000
116 117 1000000000
117 118 1000000000
118 119 1000000000
119 120 1000000000
120 121 1000000000
121 122 1000000000
122 123 1000000000
123 124 1000000000
124 125 1000000000
125 126 1000000000
126 127 1000000000
127 128 1000000000
128 129 1000000000
129 130 1000000000
130 131 1000000000
131 132 1000000000
132 133 1000000000
133 134 1000000000
134 135 1000000000
135 136 1000000000
136 137 1000000000
137 138 1000000000
138 139 1000000000
139 140 1000000000
140 141 1000000000
141 142 1000000000
142 143 1000000000
143 144 1000000000
144 145 1000000000
145 146 1000000000
146 147 1000000000
147 148 1000000000
148 149 1000000000
149 150 1000000000
150 151 1000000000
151 152 1000000000
152 153 1000000000
153 154 1000000000
154 155 1000000000
155 156 1000000000
156 157 1000000000
157 158 1000000000
158 159 1000000000
159 160 1000000000
160 161 1000000000
161 162 1000000000
162 163 1000000000
163 164 1000000000
164 165 1000000000
165 166 1000000000
166 167 1000000000
167 168 1000000000
168 169 1000000000
169 170 1000000000
170 171 1000000000
171 172 1000000000
172 173 1000000000
173 174 1000000000
174 175 1000000000
175 176 1000000000
176 177 1000000000
177 178 1000000000
178 179 1000000000
179 180 1000000000
180 181 1000000000
181 182 1000000000
182 183 1000000000
183 184 1000000000
184 185 1000000000
185 186 1000000000
186 187 1000000000
187 188 1000000000
188 189 1000000000
189 190 1000000000
190 191 1000000000
191 192 1000000000
192 193 1000000000
193 194 1000000000
194 195 1000000000
195 196 1000000000
196 197 1000000000
197 198 1000000000
198 199 1000000000
199 200 1000000000

6 6
10 0
0 3
0 7
3 0
0 2
0 1
1 2 120
5 2 80
5 1 20
5 6 30
6 1 110
4 3 30

10 9
1 0
0 0
0 0
0 1
0 1
0 1
0 1
0 1
0 1
0 1
2 1 10
3 1 4
4 3 2
5 3 5
6 2 11
7 5 1
8 6 7
9 4 1
10 2 10

3 2
1 0
0 0
0 1
1 2 1
2 3 1

output:
199000000000
-1
6
2

猜你喜欢

转载自blog.csdn.net/m0_37444209/article/details/80216672