poj 2391 Ombrophobic Bovines 【floyd + 二分 + 拆点网络流】

来源:互联网 发布:centos 系统时间不对 编辑:程序博客网 时间:2024/05/17 07:30
Ombrophobic Bovines
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 16494 Accepted: 3601

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 decided to put a rain siren on the farm to let them know when rain is approaching. They intend to create a rain evacuation plan so that all the cows can get to shelter before the rain begins. Weather forecasting is not always correct, though. In order to minimize false alarms, they want to sound the siren as late as possible while still giving enough time for all the cows to get to some shelter. 

The farm has F (1 <= F <= 200) fields on which the cows graze. A set of P (1 <= P <= 1500) paths connects them. The paths are wide, so that any number of cows can traverse a path in either direction. 

Some of the farm's fields have rain shelters under which the cows can shield themselves. These shelters are of limited size, so a single shelter might not be able to hold all the cows. Fields are small compared to the paths and require no time for cows to traverse. 

Compute the minimum amount of time before rain starts that the siren must be sounded so that every cow can get to some shelter.

Input

* Line 1: Two space-separated integers: F and P 

* Lines 2..F+1: Two space-separated integers that describe a field. The first integer (range: 0..1000) is the number of cows in that field. The second integer (range: 0..1000) is the number of cows the shelter in that field can hold. Line i+1 describes field i. 

* Lines F+2..F+P+1: Three space-separated integers that describe a path. The first and second integers (both range 1..F) tell the fields connected by the path. The third integer (range: 1..1,000,000,000) is how long any cow takes to traverse it.

Output

* Line 1: The minimum amount of time required for all cows to get under a shelter, presuming they plan their routes optimally. If it not possible for the all the cows to get under a shelter, output "-1".

Sample Input

3 47 20 42 61 2 403 2 702 3 901 3 120

Sample Output

110

Hint

OUTPUT DETAILS: 

In 110 time units, two cows from field 1 can get under the shelter in that field, four cows from field 1 can get under the shelter in field 2, and one cow can get to field 3 and join the cows from that field under the shelter in field 3. Although there are other plans that will get all the cows under a shelter, none will do it in fewer than 110 time units.


题意:有N个牧场,每个牧场上有一定数量的牛和一个避雨点,每个避雨点只能容纳一定数量的牛。又有M条连接牧场的无向边,现已给出走过该边所需要花费的时间。问你:使 所有牛不被雨淋 所花费的最少时间,若 无法保证所有的牛都不淋雨 输出-1。(若一个避雨点的牛 到达 它的容量上限,则其它牛不能在该避雨点避雨)


建边少了个判断 && 增广路径时 < 0 放在括号里面。。。这种错误让我想跳馨月湖!!!


思路:1,先Floyd预处理最短路。2,二分枚举时间值mid。3,用mid值做限制建新图。4,新图从超级源到超级汇跑一次最大流,若满流压缩时间,反之增大时间。5,继续上述过程。


当查找时间值mid时 建图如下:首先我们用 i + N 表示牧场 i 的 避雨点(很像拆点思路吧),超级源为0,超级汇为2*N+1。


一:超级源到每个牧场建边,边权为该牧场上牛的数量。


二:每个牧场上的避雨点到超级汇建边,边权为该避雨点的容量。


三:对于任意的牧场i 和j (1 <= i <= N && 1 <= j <= N),只要牧场i 到 牧场j 的最少时间花费不超过 mid ,就建边 i -> j + N,边权为INF。


注意:此题数据很大,超int型。


AC代码:


#include <cstdio>#include <cstring>#include <queue>#include <algorithm>#define MAXN 1000#define MAXM 200000+10#define INF 1000000000+100#define LL long longusing namespace std;struct Edge{    int from, to, cap, flow, next;};Edge edge[MAXM];int head[MAXN], edgenum;int cur[MAXN];int dist[MAXN];bool vis[MAXN];LL Map[MAXN][MAXN];//Map[i][j]存储i到j牧场的最少时间花费int N, M;int cow[MAXN], Cap[MAXN];//每个牧场牛的数量 避雨点的容量int sum;//记录牛的总数void Floyd()//预处理最短路{    for(int k = 1; k <= N; k++)    {        for(int i = 1; i <= N; i++)        {            if(Map[i][k] == INF) continue;            for(int j = 1; j <= N; j++)                Map[i][j] = min(Map[i][j], Map[i][k] + Map[k][j]);        }    }}void input(){    sum = 0;    for(int i = 1; i <= N; i++)        scanf("%d%d", &cow[i], &Cap[i]), sum += cow[i];    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] = 1e16;        }    }    int a, b;    LL c;    while(M--)    {        scanf("%d%d%lld", &a, &b, &c);        if(Map[a][b] > c)            Map[a][b] = Map[b][a] = c;    }    Floyd();}void init(){    edgenum = 0;    memset(head, -1, sizeof(head));}void addEdge(int u, int v, LL w){    Edge E1 = {u, v, w, 0, head[u]};    edge[edgenum] = E1;    head[u] = edgenum++;    Edge E2 = {v, u, 0, 0, head[v]};    edge[edgenum] = E2;    head[v] = edgenum++;}void getMap(LL mid){    //超级源点0 超级汇点 2*N + 1    //牧场编号1 - N   牧场对应避雨点编号 N+1 - 2*N    for(int i = 1; i <= N; i++)//超级源点 引一条边到每牧场 容量为牧场现有的牛的数目        addEdge(0, i, cow[i]);    for(int i = N+1; i <= 2*N; i++)//每个避雨点引一条到超级汇点的边 边权为避雨点的容量        addEdge(i, 2*N+1, Cap[i-N]);    for(int i = 1; i <= N; i++)    {        for(int j = 1; j <= i; j++)        {            if(Map[i][j] <= mid)//两个牧场 可在时间限制内相互可达            {                addEdge(i, j + N, INF);//牧场 引一条到另外一个牧场避雨点的边 容量为无穷大                //在i不等于j的前提下 j要向i的避雨点建边                if(i != j)                addEdge(j, i + N, INF);            }        }    }}bool BFS(int start, int end){    queue<int> Q;    memset(dist, -1, sizeof(dist));    memset(vis, false, sizeof(vis));    vis[start] = true;    dist[start] = 0;    Q.push(start);    while(!Q.empty())    {        int u = Q.front();        Q.pop();        for(int i = head[u]; i != -1; i = edge[i].next)        {            Edge E = edge[i];            if(!vis[E.to] && E.cap > E.flow)            {                vis[E.to] = true;                dist[E.to] = dist[u] + 1;                if(E.to == end) return true;                Q.push(E.to);            }        }    }    return false;}int DFS(int x, int a, int end){    if(x == end || a == 0) return a;    int flow = 0, f;    for(int &i = cur[x]; i != -1; i = edge[i].next)    {        Edge &E = edge[i];        if(dist[E.to] == dist[x] + 1 && (f = DFS(E.to, min(E.cap - E.flow, a), end)) > 0)//>0写括号里面了,想自杀!!!        {            E.flow += f;            edge[i^1].flow -= f;            flow += f;            a -= f;            if(a == 0) break;        }    }    return flow;}int Maxflow(int start, int end){    int flow = 0;    while(BFS(start, end))    {        memcpy(cur, head, sizeof(head));        flow += DFS(start, INF, end);    }    return flow;}void solve(){    LL left = 0, right = 1e16, mid, ans = 1e16;//二分查找 范围要足够大    while(right >= left)    {        mid = (left + right) >> 1;        init();        getMap(mid);        if(Maxflow(0, 2*N+1) == sum)        {            ans = mid;            right = mid - 1;        }        else            left = mid + 1;    }    if(ans == 1e16)        printf("-1\n");    else        printf("%lld\n", ans);}int main(){    while(scanf("%d%d", &N, &M) != EOF)    {        input();        solve();    }    return 0;}





0 0