Poj3189_steady Cow Assignment (binary graph multiple match/network stream + binary composition)

Source: Internet
Author: User

Problem Solving Report

http://blog.csdn.net/juncoder/article/details/38340447

Topic Portal

Test instructions

b A pigsty, n pigs. Each pig has a pleasant value for each pigsty. Ask for these pigs to make the most comfortable and least comfortable pig difference

Ideas:

The multi-matching problem of two-fractal graphs;

The pigsty and the source point are connected, the volume for the pigsty volume. Pig with meeting point Edge, Volume 1;

The connection between pigsty and pig depends on the range of pleasant values;

Two points to find the range of the minimum difference in comfort value.

#include <iostream> #include <cstring> #include <cstdio> #include <queue> #define INF 99999999using namespace Std;int n,m,b,mmap[1030][22],edge[1030][1030],l[1030],c[22];int BFs () {memset (l,-1,sizeof (l    ));    l[0]=0;    int i;    Queue<int >Q;    Q.push (0); while (!        Q.empty ()) {int U=q.front ();        Q.pop ();                for (i=0; i<=m; i++) {if (edge[u][i]&&l[i]==-1) {l[i]=l[u]+1;            Q.push (i);    }}} if (l[m]>1) return 1; return 0;}    int dfs (int x,int f) {if (x==m) return F;    int i,a; for (i=0; i<=m; i++) {if (l[i]==l[x]+1&&edge[x][i]&& (A=dfs (i,min)))) {Ed            Ge[x][i]-=a;            Edge[i][x]+=a;        return A;    }} L[x]=-1; return 0;}    int dinic () {int ans=0,a;    while (BFS ()) while (A=dfs (0,inf)) ans+=a; return ans;}    int cow (int mid) {int i,j,k;   for (I=1; i<=b-mid+1; i++) {     memset (Edge,0,sizeof (Edge));        for (j=1; j<=b; J + +) {Edge[0][j]=c[j];            } for (j=1; j<=n; J + +) {for (k=i; k<=i+mid-1; k++) {edge[mmap[j][k]][j+b]=1;        } edge[j+b][m]=1;    } if (Dinic () ==n) return 1; } return 0;}    int main () {int i,j;        while (~SCANF ("%d%d", &n,&b)) {memset (mmap,0,sizeof (mmap));        Memset (C,0,sizeof (c));        m=n+b+1;            for (I=1; i<=n; i++) {for (j=1; j<=b; J + +) {scanf ("%d", &mmap[i][j]);        }} for (I=1; i<=b; i++) {scanf ("%d", &c[i]);        } int l=1,r=b,t=-1;            while (l<=r) {int mid= (L+R)/2;                if (Cow (mid)) {T=mid;            R=mid-1;            } else {l=mid+1;    }} printf ("%d\n", t); } return 0;}

Steady Cow Assignment
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 5369 Accepted: 1845

Description

Farmer John ' s n (1 <= n <=) cows each reside in one of B (1 <= B <=) barns which, of course, with Limi Ted capacity. Some cows really like their current barn, and Some is not so happy.

FJ would like to rearrange the cows such that the cows is as equally happy as possible, even if that means all the cows H Ate their assigned barn.

Each cow gives FJ the order in which she prefers the barns. A cow ' s happiness with a particular assignment was her ranking of her barn. Your job is to find a assignment of cows to barns such this no barn ' s capacity is exceeded and the size of the range (i.e ., one more than the positive difference between the The Highest-ranked barn chosen and that lowest-ranked barn chosen) of Barn rankings The cows give their assigned barns is as small as possible.

Input

Line 1:two space-separated integers, N and B

Lines 2..n+1:each line contains B space-separated integers which is exactly 1..B sorted into some order. The first integer on line i+1 is the number of the cow I's Top-choice barn, the second integer on the the number of the i ' th cow ' s second-choice barn, and so on.

Line n+2:b space-separated integers, respectively the capacity of the first barn, then the capacity of the second, and so On. The sum of these numbers is guaranteed to being at least N.

Output

Line 1:one Integer, the size of the minumum range of barn rankings the cows give their assigned barns, including the ENDP Oints.

Sample Input

6 41 2 3 42 3 1 44 2 3 13 1 2 41 3 4 21 4 2 32 1 3 2

Sample Output

2

Hint

Explanation of the sample:

Each cow can is assigned to her first or second Choice:barn 1 gets cows 1 and 5, barn 2 gets cow 2, barn 3 gets cow 4, an D Barn 4 gets cows 3 and 6.

Poj3189_steady Cow Assignment (binary graph multiple match/network stream + binary composition)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.