POJ 2391.Ombrophobic Bovines 解题报告

实际上是求最短的避雨时间。

首先将每个点拆成两个,一个连接源点,一个连接汇点,连接源点的点的容量为当前单的奶牛数,连接汇点的点为能容纳的奶牛数。

floyd求任意两点互相到达的最短时间,二分最长时间,最大流判断是否可行。

注意路径时间会超过int

/*
      最大流SAP
      邻接表
      思路:基本源于FF方法,给每个顶点设定层次标号,和允许弧。
      优化:
      1、当前弧优化(重要)。
      1、每找到以条增广路回退到断点(常数优化)。
      2、层次出现断层,无法得到新流(重要)。
      时间复杂度(m*n^2)
*/
#include <iostream>
#include <cstdio>
#include <cstring>
#define ms(a,b) memset(a,b,sizeof a)
using namespace std;
const int INF = 500;
long long G[INF][INF];
struct node {
    int v, c, next;
} edge[INF*INF * 4];
long long  pHead[INF*INF], SS, ST, nCnt;
void addEdge (int u, int v, int c) {
    edge[++nCnt].v = v; edge[nCnt].c = c, edge[nCnt].next = pHead[u]; pHead[u] = nCnt;
    edge[++nCnt].v = u; edge[nCnt].c = 0, edge[nCnt].next = pHead[v]; pHead[v] = nCnt;
}
int SAP (int pStart, int pEnd, int N) {
    int numh[INF], h[INF], curEdge[INF], pre[INF];
    int cur_flow, flow_ans = 0, u, neck, i, tmp;
    ms (h, 0); ms (numh, 0); ms (pre, -1);
    for (i = 0; i <= N; i++) curEdge[i] = pHead[i];
    numh[0] = N;
    u = pStart;
    while (h[pStart] <= N) {
        if (u == pEnd) {
            cur_flow = 1e9;
            for (i = pStart; i != pEnd; i = edge[curEdge[i]].v)
                if (cur_flow > edge[curEdge[i]].c) neck = i, cur_flow = edge[curEdge[i]].c;
            for (i = pStart; i != pEnd; i = edge[curEdge[i]].v) {
                tmp = curEdge[i];
                edge[tmp].c -= cur_flow, edge[tmp ^ 1].c += cur_flow;
            }
            flow_ans += cur_flow;
            u = neck;
        }
        for ( i = curEdge[u]; i != 0; i = edge[i].next)
            if (edge[i].c && h[u] == h[edge[i].v] + 1)     break;
        if (i != 0) {
            curEdge[u] = i, pre[edge[i].v] = u;
            u = edge[i].v;
        }
        else {
            if (0 == --numh[h[u]]) continue;
            curEdge[u] = pHead[u];
            for (tmp = N, i = pHead[u]; i != 0; i = edge[i].next)
                if (edge[i].c)  tmp = min (tmp, h[edge[i].v]);
            h[u] = tmp + 1;
            ++numh[h[u]];
            if (u != pStart) u = pre[u];
        }
    }
    return flow_ans;
}
long long m, n, x, y,sum,c;
int in[INF], out[INF];
bool check (long long tem) {
    nCnt = 1;
    SS = 2 * n + 1, ST = 2 * n + 2;
    memset (pHead, 0, sizeof pHead);
    for (int i = 1; i <= n; i++) {
        if(out[i]) addEdge (SS, i, out[i]);
        for (int j =1; j <= n; j++)
            if (G[i][j] <= tem&&G[i][j]!=-1)
                addEdge (i, j+n, 5000);
    }
    for (int i = 1; i <= n; i++)
              if(in[i])addEdge (i + n, ST, in[i]);
    int ans = SAP (SS, ST, ST);
    if (ans == sum) return 1;
    return 0;
}
int main() {
    /*
           建图,前向星存边,表头在pHead[],边计数 nCnt.
           SS,ST分别为源点和汇点
    */
    ms (G, -1);
    cin>>n>>m;
    for (int i = 1; i <= n; i++) {
        cin>>out[i]>>in[i];
        sum += out[i];
    }
    long long l = 0x7fffffffffffffff, r = 0;
    for (int i = 1; i <= n; i++) G[i][i] = 0;
    for (int i = 1; i <= m; i++) {
        cin>>x>>y>>c;
        if(G[x][y]>0) G[x][y] = G[y][x] = min(c,G[x][y]);
        else
                     G[x][y]=G[y][x]=c;
        l = min (l, c), r = max (r, c);
    }
    for (int  t = 1; t <= n; t++)
        for (int i = 1; i <= n; i++)
            for (int j = 1; j <= n; j++) {
                if (G[i][t] == -1 || G[t][j] == -1) continue;
                if (G[i][j] == -1 || G[i][j] > G[i][t] + G[t][j])
                    G[i][j] = G[i][t] + G[t][j], l = min (l, G[i][j]), r = max (r, G[i][j]);
            }
    long long last = -1,mid;
    while (l <= r) {
        mid = (l + r) >> 1;
        if (check (mid) ) {
            last = mid;
            r = mid - 1;
        }
        else l = mid + 1;
    }
    cout<<last;
    return 0;
}

时间: 2024-12-15 01:52:00

POJ 2391.Ombrophobic Bovines 解题报告的相关文章

POJ 2391 Ombrophobic Bovines (二分,最短路径,网络流sap,dinic,预留推进 )

Ombrophobic Bovines Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 14019   Accepted: 3068 Description FJ's cows really hate getting wet so much that the mere thought of getting caught in the rain makes them shake in their hooves. They h

POJ 2391 Ombrophobic Bovines 不喜欢雨的奶牛 Floyd+二分枚举+最大流

题目链接:POJ 2391 Ombrophobic Bovines Ombrophobic Bovines Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 15006   Accepted: 3278 Description FJ's cows really hate getting wet so much that the mere thought of getting caught in the rain makes

poj 2391 Ombrophobic Bovines, 最大流, 拆点, 二分, dinic

poj 2391 Ombrophobic Bovines, 最大流, 拆点, 二分 dinic /* * Author: yew1eb * Created Time: 2014年10月31日 星期五 15时39分22秒 * File Name: poj2391.cpp */ #include <ctime> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring&g

POJ 2391 Ombrophobic Bovines (二分 + floyd + 网络流)

POJ 2391 Ombrophobic Bovines 链接:http://poj.org/problem?id=2391 题目:农场有F 块草地,1≤F≤200,奶牛们在草地上吃草.这些草地之间有P 条路相连,1≤P≤1500,这些路足够宽,再多的奶牛也能同时在路上行走.有些草地上有避雨点,奶牛们可以在此避雨.避雨点的容量是有限的,所以一个避雨点不可能容纳下所有的奶牛.草地与路相比很小,奶牛们通过时不需要花费时间.计算警报至少需要提前多少时间拉响,以保证所有的奶牛都能到达一个避雨点. 思路:

POJ 2391 Ombrophobic Bovines(最大流+拆点)

POJ 2391 Ombrophobic Bovines 题目链接 题意:一些牛棚,有a只牛,现在下雨,每个牛棚容量量变成b,现在有一些道路连接了牛棚,问下雨后牛走到其他牛棚,使得所有牛都有地方躲雨,最后一只牛要走多久 思路:二分答案,然后最大流去判断,建图的方式为,牛棚拆点,源点连向入点,容量为a,出点连向汇点容量为b,中间入点和出点之间根据二分的值判断哪些边是可以加入的 代码: #include <cstdio> #include <cstring> #include <

poj 2391 Ombrophobic Bovines(最大流+floyd+二分)

Ombrophobic Bovines Time Limit: 1000MSMemory Limit: 65536K Total Submissions: 14519Accepted: 3170 Description FJ's cows really hate getting wet so much that the mere thought of getting caught in the rain makes them shake in their hooves. They have de

Poj 2391 Ombrophobic Bovines 网络流 拆点

Poj 2391 Ombrophobic Bovines 网络流 拆点 FJ's cows really hate getting wet so much that the mere thought of getting caught in the rain makes them shake in their hooves. They have decided to put a rain siren on the farm to let them know when rain is approa

POJ 2391 Ombrophobic Bovines

Ombrophobic Bovines Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 18623   Accepted: 4057 Description FJ's cows really hate getting wet so much that the mere thought of getting caught in the rain makes them shake in their hooves. They h

poj 2391 Ombrophobic Bovines 二分+最大流

同poj 2112. 代码: //poj 2391 //sep9 #include <iostream> #include <queue> #include <algorithm> using namespace std; typedef long long ll; const int maxN=1024; const int maxM=100002; const ll MAX=(1ULL<<63)-1; struct Edge { int v,f,nxt;