网络流 1024

来源:互联网 发布:深圳市软件产业基地4栋 编辑:程序博客网 时间:2024/06/07 03:55

Ombrophobic Bovines

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other)
Total Submission(s) : 5   Accepted Submission(s) : 2
Problem 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
 


题意:n个遮雨处有若干牛,每个遮雨处有一定的容纳量。给出从一个地方到另一的地方的用时,问所有的牛都能到遮雨处的最短时间。如果不能满足输出-1;

思路:sap+floyd+二分(将时间二分)

此题做的是相当的头大,不知道为何不是re就是tle,还有wa,改了若干遍,直接快要丧失信心了,o(╯□╰)o……………………

#include<iostream>#include<algorithm>#include<cstdio>#include<cstring>#define max(a,b) ((a)>(b)?(a):(b))using namespace std;const int N=420;const int M=82000;const int INF=1<<30;int n,m;int gap[N],dis[N],pre[N],head[N],cur[N];int NE,NV;struct Node{    int pos,next;    int c;} E[M];#define FF(i,NV) for(int i=0;i<NV;i++)int sap(int s,int t){    memset(dis,0,sizeof(int)*(NV+1));    memset(gap,0,sizeof(int)*(NV+1));    FF(i,NV) cur[i] = head[i];    int u = pre[s] = s,maxflow = 0;    int aug =INF;    gap[0] = NV;    while(dis[s] < NV)    {loop:        for(int &i = cur[u]; i != -1; i = E[i].next)        {            int v = E[i].pos;            if(E[i].c && dis[u] == dis[v] + 1)            {                aug=min(aug,E[i].c);                pre[v] = u;                u = v;                if(v == t)                {                    maxflow += aug;                    for(u = pre[u]; v != s; v = u,u = pre[u])                    {                        E[cur[u]].c -= aug;                        E[cur[u]^1].c += aug;                    }                    aug =INF;                }                goto loop;            }        }        if( (--gap[dis[u]]) == 0)   break;        int mindis = NV;        for(int i = head[u]; i != -1 ; i = E[i].next)        {            int v = E[i].pos;            if(E[i].c && mindis > dis[v])            {                cur[u] = i;                mindis = dis[v];            }        }        gap[ dis[u] = mindis+1 ] ++;        u = pre[u];    }    return maxflow;}void addEdge(int u,int v,int c ){    E[NE].c = c;    E[NE].pos = v;    E[NE].next = head[u];    head[u] = NE++;    E[NE].c = 0;    E[NE].pos = u;    E[NE].next = head[v];    head[v] = NE++;}int cow[N],shelter[N];__int64 map[N][N];int main(){    int i, j, k, u, v, w, sum;    while(scanf("%d%d", &n, &m) != EOF)    {        for(i = 1; i <= n; i ++)            for(j = 1; j <= n; j ++)            {                map[i][j] = (i == j ? 0 : -1);            }        for(sum = 0, i = 1; i <= n; i ++)        {            scanf("%d%d", &cow[i], &shelter[i]);            sum += cow[i];        }        __int64 ma = -1;        while(m --)        {            scanf("%d%d%d", &u, &v, &w);            if(map[u][v] == -1 || w < map[u][v])            {                map[u][v] = map[v][u] = w;                ma = max(ma, w);            }        }        for(k = 1; k <= n; k ++)     //  floyd,中间顶点k是最前的。            for(i = 1; i <= n; i ++)                for(j = 1; j <= n; j ++)                {                    if(map[i][k] == -1 || map[k][j] == -1) continue;                    if(map[i][j] == -1 || map[i][k] + map[k][j] < map[i][j])                    {                        map[i][j] = map[i][k] + map[k][j];                        ma = max(ma, map[i][j]);                    }                }        __int64 mid, low = 0, high = ma + 1, ans = -1;        while(low < high)  // 二分答案,网络流判定。        {            mid = (low + high) / 2;            NE = 0;NV=2*n+2;               //  重建图。            memset(head, -1, sizeof(head));            for(i = 1; i <= n; i ++)            {                addEdge(0, i, cow[i]);                addEdge(i, i + n, INF);                addEdge(i + n, 2 * n + 1, shelter[i]);                for(j = 1; j <= n; j ++)                    if(i != j && map[i][j] != -1 && map[i][j] <= mid)                    {                        addEdge(i, j + n, INF);                    }            }            if(sap(0, 2 * n + 1) == sum)    //  所有牛能避雨,符合条件。            {                ans = high = mid;            }            else    //  有牛不能避雨,不符合条件。            {                low = mid + 1;            }        }        printf("%I64d\n", ans);    }    return 0;}/*3 47 20 42 61 2 403 2 702 3 901 3 120*/


 

原创粉丝点击