poj 2391(拆点 + 二分 + dinic)

说一下这个题吧。。刚开始的时候。。没想到分点。。想简单了: 认为一个field可以只剩下牛或者牛棚(两者都有可以相减)。。。然后超级源点跟牛之间建边。。超级汇点跟牛棚建边。。。。YY的挺好。。。可是明显是错的。。。看一下样例就可以发现错误了。。然后开始拆点。。。。结果。。之后wa了好久。。。不得已参考了别人的代码。。发现是二分的时候最大值开小了。。。。OMG。。。总的来说这个题不难。。。。细节是关键。。。。


Ombrophobic Bovines
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 7549 Accepted: 1725

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 4
7 2
0 4
2 6
1 2 40
3 2 70
2 3 90
1 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.

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
#define cc(m,v) memset(m,v,sizeof(m))
#define maxM 100010
#define maxN 1010
#define inf 1<<30
#define LL __int64

struct node {
    int u, v, f, next;
} edge[maxM];
int head[maxN], p, lev[maxN], cur[maxN];
int que[maxM];

void init1() {
    p = 0, cc(head, -1);
}

bool bfs(int s, int t) {
    int u, v, qin = 0, qout = 0, i;
    cc(lev, 0);
    lev[s] = 1, que[qin++] = s;
    while (qout != qin) {
        u = que[qout++];
        for (i = head[u]; i != -1; i = edge[i].next)
            if (edge[i].f > 0 && lev[v = edge[i].v] == 0) {
                lev[v] = lev[u] + 1, que[qin++] = v;
                if (v == t) return 1;
            }
    }
    return lev[t];
}

int dinic(int s, int t) {
    int qin, i, k, u, f;
    int flow = 0;
    while (bfs(s, t)) {
        memcpy(cur, head, sizeof (head));
        u = s, qin = 0;
        while (1) {
            if (u == t) {
                for (k = 0, f = inf; k < qin; k++)
                    if (edge[que[k]].f < f)
                        f = edge[que[i = k]].f;
                for (k = 0; k < qin; k++)
                    edge[que[k]].f -= f, edge[que[k]^1].f += f;
                flow += f, u = edge[que[qin = i]].u;
            }
            for (i = cur[u]; cur[u] != -1; i = cur[u] = edge[cur[u]].next)
                if (edge[i].f > 0 && lev[u] + 1 == lev[edge[i].v]) break;
            if (cur[u] != -1) {
                que[qin++] = cur[u], u = edge[cur[u]].v;
            } else {
                if (qin == 0) break;
                lev[u] = -1, u = edge[que[--qin]].u;
            }
        }
    }
    return flow;
}

void addedge(int u, int v, int f) {
    edge[p].u = u, edge[p].v = v, edge[p].f = f, edge[p].next = head[u], head[u] = p++;
    edge[p].u = v, edge[p].v = u, edge[p].f = 0, edge[p].next = head[v], head[v] = p++;
}
LL maze[maxN][maxN];
int val[maxN][2], f;

int main() {
    int i, j, n, m, s, t, sum, u, v;
    LL lef, rig, mid,ans;
    while (scanf("%d%d", &n, &m) != -1) {
        s = 0, t = 2 * n + 1, sum = 0;
        for (i = 1; i <= n; i++) {
            scanf("%d%d", &val[i][0], &val[i][1]);
            sum += val[i][0];
        }
        for(i=1;i<=n;i++)
            for (j = 1; j <= n; j++)
                maze[i][j] = (i == j ? 0 : -1);
       
        rig = -1, lef = 0;
        for (i = 1; i <= m; i++) {
            scanf("%d%d%d", &u, &v, &f);
            if (maze[u][v] == -1 || f < maze[u][v]){
                maze[u][v] = maze[v][u] = f;
            }
        }
        for (int k = 1; k <= n; k++)
            for (i = 1; i <= n; i++)
                if(maze[i][k]!=-1)for (j = 1; j <= n; j++)
                    if (maze[k][j]!=-1 && (maze[i][j]>maze[i][k]+maze[k][j] || maze[i][j]==-1)){
                            maze[i][j] = maze[i][k] + maze[k][j];
                            if(maze[i][j]>rig) rig=maze[i][j];
                     }
        rig+=1000;ans=-1;
        while (lef <= rig) {
            init1();
            mid = (lef + rig) >> 1;
            for (i = 1; i <= n; i++) {
                addedge(s, i, val[i][0]), addedge(i + n, t, val[i][1]), addedge(i, i + n, inf);
                for (j = 1; j <= n; j++)
                    if (i != j && maze[i][j] != -1 && maze[i][j] <= mid)
                        addedge(i, j + n, inf);
            }
            int tmp = dinic(s, t);
            if(tmp==sum){
                ans =mid; rig=mid-1;
            }else
                lef=mid+1;
        }
        printf("%I64d\n", ans);
    }
    return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值