Luo Gu [P2857] [] [USACO06FEB] JZOJ1259 cow stable distribution

statement:

This problem solution draws @Heartlessly (luogu UID = 32139) Content

Subject to the effect:

Each cattle has a negative feedback value for each cow, now assign cow, so that the minimum difference between the maximum value of all Poor Poor cattle and a minimum value, find the value.

text:

Because the topics have mentioned limits for each cow, that we can use to solve network tumors.

Sample Figure:

Sample map

Number bullpen to add \ (n \) because there has already been used cattle

Then how to do? We consider that half the answer.

Let's think about that the biggest difference is how much the maximum value and the minimum Poor Poor value, because the maximum value of the maximum negative feedback \ (= b \) , the minimum value of the minimum Poor \ (= 1 \) , then the maximum negative feedback the difference between the maximum value and the minimum value of the Poor \ (b-1 \)

We can then \ ([1, b] \ ) to find our answer.


Suppose we have two assigned \ (X \) (difference between the maximum difference between the assessment value and the minimum negative feedback value), the minimum negative feedback value \ (= I \) , then the maximum negative feedback value \ (= I + X-. 1 \) . The figure put the case into the adjacent table, we certainly can not be directly connected to all the cattle even only part of the bullpen. Since the two assigned \ (X \) is the minimum value of the Poor \ (I = \) , the maximum value of the Poor \ (. 1-X = I + \) , each cow then it can only be attached on \ (I \) th to \ ((i + x-1 ) \) a cow.

DoneEven in a rowAfter running the maximum flow again, after all we are is itself \ (n \) cattle, if the maximum flow is not \ (n \) then the program is not feasible.

Code:



bool check(int x)
{
    for (int i = 1; i + x - 1 <= b; i++)
    {
        memset(head, 0, sizeof(head));
        tot = 0;
        s = n + b + 1, t = n + b + 2;
        for (int j = 1; j <= n; j++) Add(s, j, 1);       //连成上图
        for (int j = 1; j <= b; j++) Add(j + n, t, v[j]);
        for (int j = 1; j <= n; j++)
            for (int k = i; k <= i + x - 1; k++)
                Add(j, like[j][k] + n, 1);        //连第i个到第(i+x-1)个牛棚
        int ans = dinic();
        if(ans == n) return 1;     // 网络瘤
    }
    return 0;
}

int main()
{
    scanf("%d%d", &n, &b); 
    for (int i = 1; i <= n; i++)
        for (int j = 1; j <= b; j++)
            scanf("%d", &like[i][j]);  //like[i][j] 表示第i只牛差评度为j的牛棚
    for (int j = 1; j <= b; j++)
        scanf("%d", &v[j]);   // v[i] 表示第i个牛棚的限度
    int l = 1, r = b, mid, ans = 0;
    while(l <= r)           //二分开始
    {
        mid = (l + r) >> 1;
        if(check(mid))
        {
            ans = mid;
            r = mid - 1;
        } 
        else 
            l = mid + 1;
    }
    printf("%d", ans);
    return 0;
}

Guess you like

Origin www.cnblogs.com/GJY-JURUO/p/12109826.html