1. 程式人生 > >POJ 3189--Steady Cow Assignment【二分圖多重匹配 && 最大流求解 && 列舉 && 經典】

POJ 3189--Steady Cow Assignment【二分圖多重匹配 && 最大流求解 && 列舉 && 經典】

Steady Cow Assignment
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 6023 Accepted: 2078

Description

Farmer John's N (1 <= N <= 1000) cows each reside in one of B (1 <= B <= 20) barns which, of course, have limited capacity. Some cows really like their current barn, and some are not so happy. 

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

Each cow gives FJ the order in which she prefers the barns. A cow's happiness with a particular assignment is her ranking of her barn. Your job is to find an assignment of cows to barns such that 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 are 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 that line is 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 be 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 endpoints.

Sample Input

6 4
1 2 3 4
2 3 1 4
4 2 3 1
3 1 2 4
1 3 4 2
1 4 2 3
2 1 3 2

Sample Output

2

題意:

有n頭牛,m個牛棚,每個牛棚都有一定的容量(就是最多能裝多少隻牛),然後每隻牛對每個牛圈的喜好度不同(就是所有牛圈在每個牛心中都有一個排名),然後要求所有的牛都進豬圈,牛棚在牛心中的排名差計算方法為:所有牛中最大排名和最小排名之差。問最小的排名差。

英語不好太坑了 ,理解錯了題意,以測試資料舉例:

1 2 3 4
2 3 1 4
4 2 3 1
3 1 2 4
1 3 4 2
1 4 2 3
2 1 3 2

前6行是每頭牛對牛棚的排名, 第二行2  3  1  4 代表的是 對第二頭牛而言,第二個牛棚排第一,第三個牛棚排第二,第一個牛棚排第三,第四個牛棚排第四。

不是第一個牛棚排第二,第二個牛棚排第三,第三個牛棚排第一,第四個牛棚排第四。這點理解搓個 ,wa了好多遍,英語不好真是要哭了。

解析:這一題其實不難,資料比較小,可以用最大流來寫,建圖有點麻煩,下面是建圖過程:

(1)首先虛擬一個源點,匯點。

(2)源點向每頭牛建邊, 權值為1,

(3)牛棚向匯點建邊,權值為每個牛棚的容量。

(4)列舉牛棚的最差排名和最好排名(即列舉排名差) ,在這個排名之內牛和這個牛棚建邊。

看看每種情況判斷是否合法(是否滿流),取最小值。

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#define maxn 1200
#define maxm 100005
#define INF 0x3f3f3f3f
using namespace std;
int n, m;

struct node {
    int u, v, cap, flow, next;
};
node edge[maxm];
int head[maxn], cnt;
int cur[maxn];
int dist[maxn], vis[maxn];
int num[maxn];//每個牛棚的容量
int val[1200][25];

void init(){
    cnt = 0;
    memset(head, -1, sizeof(head));
}

void add(int u, int v, int w){
    node E;
    edge[cnt] = {u, v, w, 0, head[u]};
    head[u] = cnt++;
    edge[cnt] = {v, u, 0, 0 ,head[v]};
    head[v] = cnt++;
}

void input(){
    for(int i = 1; i <= n; ++i)
        for(int j = 1; j <= m; ++j)
            scanf("%d", &val[i][j]);
    for(int i = 1; i <= m; ++i){
        scanf("%d", &num[i]);
    }
}

void getmap(int l, int r){
    for(int i = 1; i <= n; ++i)
        add(0, i, 1);
    for(int j = 1; j <= m; ++j)
        add(n + j, n + m + 1, num[j]);
//    for(int i = 1; i <= n; ++i)
//        for(int j = 1; j <= m; ++j)
//        if(val[i][j] >= l && val[i][j] <= r)
//        add(i, j + n, 1);
    for(int i = 1; i <= n; ++i)
        for(int j = l; j <= r; ++j)
        add(i, val[i][j] + n, 1);
}

bool BFS(int st ,int ed){
    queue<int>q;
    memset(vis, 0 ,sizeof(vis));
    memset(dist, -1, sizeof(dist));
    vis[st] = 1;
    dist[st] = 0;
    q.push(st);
    while(!q.empty()){
        int u = q.front();
        q.pop();
        for(int i = head[u]; i != -1; i = edge[i].next){
            node E = edge[i];
            if(!vis[E.v] && E.cap > E.flow){
                vis[E.v] = 1;
                dist[E.v] = dist[u] + 1;
                if(E.v == ed)
                    return true;
                q.push(E.v);
            }
        }
    }
    return false;
}

int DFS(int x, int ed, int a){
    if(x == ed || a == 0)
        return a;
    int flow = 0, f;
    for(int &i = cur[x]; i != -1; i = edge[i].next){
        node &E = edge[i];
        if(dist[E.v] == dist[x] + 1 && (f = DFS(E.v, ed, min(a, E.cap - E.flow))) > 0){
            E.flow += f;
            edge[i ^ 1].flow -= f;
            a -= f;
            flow += f;
            if(a == 0)
                break;
        }
    }
    return flow;
}

int maxflow(int st, int ed){
    int flowsum = 0;
    while(BFS(st,ed)){
        memcpy(cur, head, sizeof(head));
        flowsum += DFS(st, ed, INF);
    }
    return flowsum;
}

int main(){
    while(scanf("%d%d", &n, &m) != EOF){
        input();
        int ans = INF;
        for(int i = 1; i <= m; ++i){//列舉排名的下界
            for(int j = i; j <= m; ++j){//列舉排名的上界
                init();
                getmap(i, j);
                if(maxflow(0, n + m + 1) == n)
                    ans = min(ans, j - i + 1);
            }
        }
        printf("%d\n", ans);
    }
    return 0;
}