poj Optimal Milking

Optimal Milking

题目:

有K个机器,C只牛。要求求出最所有牛到各个产奶机的最短距离。给出一个C+K的矩阵,表示各种标号间的距离。

而每个地方最多有M只牛。

算法分析:

二分+最短路+网络流

想法难以想到。我是看解题报告的思路。然后,自己上了手。开始wrong 了3次。后来各种该,无意的一个更改就AC了。无语勒。。。。

wrong 在了,网络流建图的时候只能是机器和奶牛之间的距离关系。而奶牛跟奶牛或者机器跟机器不要建边。当时脑残了!!!!

#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <cstdio>
#include <cstring>
using namespace std;

const int INF = 1 << 20;
const int MAXN = 1000;

struct Edge{
   int from,to,cap,flow,cost;
   Edge(){};
   Edge(int _from,int _to,int _cap,int _flow)
       :from(_from),to(_to),cap(_cap),flow(_flow){};
};
vector<Edge> edges;
vector<int> G[MAXN];
int cur[MAXN],d[MAXN];
bool vst[MAXN];
int src,sink;
int dist[MAXN][MAXN];
int K,C,M,V;

void init(){
    src = V + 1; sink = src + 1;
    for(int i = 0;i <= sink;++i)
        G[i].clear();
    edges.clear();
}

void flody(){
    for(int k = 0;k < V;++k)
     for(int i = 0;i < V;++i)
       for(int j = 0;j < V;++j)
         if(dist[i][j] > dist[i][k] + dist[k][j])
           dist[i][j] = dist[i][k] + dist[k][j];
//    for(int i = 0;i < V;++i){
//        for(int j = 0;j < V;++j)
//           printf("%d ",dist[i][j]);
//        puts("");
//    }
}

void addEdge(int from,int to,int cap){
    edges.push_back(Edge(from,to,cap,0));
    edges.push_back(Edge(to,from,0,0));
    int sz = edges.size();
    G[from].push_back(sz - 2);
    G[to].push_back(sz - 1);
}

void build(int limit){
    init();

    for(int i = K;i < V;++i){  // 奶牛与源点
        addEdge(src,i,1);
    }

    for(int i = 0;i < K;++i){ //机器与汇点
        addEdge(i,sink,M);
    }

   //注意---> i = K!!! j < K!!!!
    for(int i = K;i < V;++i){  //奶牛与机器的连接
        for(int j = 0;j < K;++j){
            if(dist[i][j] <= limit){
                addEdge(i,j,1);
            }
        }
    }

}

bool BFS(){
    memset(vst,0,sizeof(vst));
    queue<int> Q;
    Q.push(src);
    d[src] = 0;
    vst[src] = 1;

    while(!Q.empty()){
        int x = Q.front(); Q.pop();
        for(int i = 0;i < (int)G[x].size();++i){
            Edge& e = edges[G[x][i]];
            if(!vst[e.to] && e.cap > e.flow){
                vst[e.to] = 1;
                d[e.to] = d[x] + 1;
                Q.push(e.to);
            }
        }
    }

    return vst[sink];
}

int DFS(int x,int a){
    if(x == sink||a == 0)
        return a;

    int flow = 0,f;
    for(int& i = cur[x];i < (int)G[x].size();++i){
        Edge& e = edges[G[x][i]];
        if(d[e.to] == d[x] + 1&&(f = DFS(e.to,min(a,e.cap - e.flow))) > 0){
            e.flow += f;
            edges[G[x][i]^1].flow -= f;
            flow += f;
            a -= f;
            if(a == 0) break;
        }
    }
    return flow;
}

int maxFlow(){
    int flow = 0;
    while(BFS()){
        memset(cur,0,sizeof(cur));
        flow += DFS(src,INF);
    }
    return flow;
}

bool Check(int mid){
    build(mid);
    int flow = maxFlow();       //cout << "flow : " << flow << endl;

    return flow == C;
}

void solve(){
    flody();

    int lb = -1,ub = INF + 100;
    while(ub - lb > 1){
        int mid = (lb + ub) / 2;
        if(Check(mid))
            ub = mid;
        else
            lb = mid;

       //cout << "mid: " << mid << " lb: " << lb << "  ub: " << ub << endl;
    }

    printf("%d\n",ub);
}

int main()
{
  //  freopen("Input.txt","r",stdin);

    while(~scanf("%d%d%d",&K,&C,&M)){
        V = K + C;
        int x;
        for(int i = 0;i < V;++i){
            for(int j = 0;j < V;++j){
                scanf("%d",&x);
                dist[i][j] = (x == 0 ? INF : x);
            }
            dist[i][i] = 0;
        }

        solve();
    }
    return 0;
}

还有一种是多重匹配,,还没写。写完补上。

时间: 2024-10-07 23:41:02

poj Optimal Milking的相关文章

POJ Optimal Milking 2012-08-28

1 Program poj2112; 2 3 var n,c,m,mid,s,t:longint; 4 5 map,a:array[1..300,1..300]of longint; 6 7 cur,dis:array[1..300]of longint; 8 9 vh:array[0..300]of longint; 10 11 Procedure init; 12 13 var i,j,k:longint; 14 15 begin 16 17 readln(n,c,m); 18 19 for

POJ 2112 Optimal Milking (二分 + floyd + 网络流)

POJ 2112 Optimal Milking 链接:http://poj.org/problem?id=2112 题意:农场主John 将他的K(1≤K≤30)个挤奶器运到牧场,在那里有C(1≤C≤200)头奶牛,在奶牛和挤奶器之间有一组不同长度的路.K个挤奶器的位置用1-K的编号标明,奶牛的位置用K+1-K+C 的编号标明.每台挤奶器每天最多能为M(1≤M≤15)头奶牛挤奶.寻找一个方案,安排每头奶牛到某个挤奶器挤奶,并使得C 头奶牛需要走的所有路程中的最大路程最小.每个测试数据中至少有一

POJ 2112 Optimal Milking(二分+最大流)

POJ 2112 Optimal Milking 题目链接 题意:给定一些机器和奶牛,在给定距离矩阵,(不在对角线上为0的值代表不可达),每个机器能容纳m个奶牛,问所有奶牛都能挤上奶,那么走的距离最大的奶牛的最小值是多少 思路:明显的二分+最大流,注意floyd求出的距离矩阵最大值可能不止200,所以二分的上限要注意 代码: #include <cstdio> #include <cstring> #include <queue> #include <algori

POJ 2112 Optimal Milking 最优挤奶方案 Floyd算法+二分查找+最大流

题目链接:POJ 2112 Optimal Milking Optimal Milking Time Limit: 2000MS   Memory Limit: 30000K Total Submissions: 12446   Accepted: 4494 Case Time Limit: 1000MS Description FJ has moved his K (1 <= K <= 30) milking machines out into the cow pastures among

POJ 2112—— Optimal Milking——————【多重匹配、二分枚举答案、floyd预处理】

Optimal Milking Time Limit:2000MS     Memory Limit:30000KB     64bit IO Format:%I64d & %I64u Submit Status Practice POJ 2112 Description FJ has moved his K (1 <= K <= 30) milking machines out into the cow pastures among the C (1 <= C <= 20

POJ 2112 Optimal Milking 二分答案+最大流

首先二分最长的边,然后删去所有比当前枚举的值长的边,算最大流,看是否能满足所有的牛都能找到挤奶的地方 #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #include <climits> #include <string> #include <iostream> #include <map> #include

POJ 2112 Optimal Milking

Optimal Milking Time Limit: 2000ms Memory Limit: 30000KB This problem will be judged on PKU. Original ID: 211264-bit integer IO format: %lld      Java class name: Main FJ has moved his K (1 <= K <= 30) milking machines out into the cow pastures amon

Optimal Milking (poj 2112 网络流+二分+floyd)

Language: Default Optimal Milking Time Limit: 2000MS   Memory Limit: 30000K Total Submissions: 12951   Accepted: 4688 Case Time Limit: 1000MS Description FJ has moved his K (1 <= K <= 30) milking machines out into the cow pastures among the C (1 <

POJ 2122 Optimal Milking

Optimal Milking Time Limit: 2000MS   Memory Limit: 30000K Total Submissions: 14040   Accepted: 5065 Case Time Limit: 1000MS Description FJ has moved his K (1 <= K <= 30) milking machines out into the cow pastures among the C (1 <= C <= 200) co