zoukankan      html  css  js  c++  java
  • POJ2112 Optimal Milking (网络流)(Dinic)

                                             Optimal Milking
    Time Limit: 2000MS   Memory Limit: 30000K
    Total Submissions: 16461   Accepted: 5911
    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) cows. A set of paths of various lengths runs among the cows and the milking machines. The milking machine locations are named by ID numbers 1..K; the cow locations are named by ID numbers K+1..K+C.

    Each milking point can "process" at most M (1 <= M <= 15) cows each day.

    Write a program to find an assignment for each cow to some milking machine so that the distance the furthest-walking cow travels is minimized (and, of course, the milking machines are not overutilized). At least one legal assignment is possible for all input data sets. Cows can traverse several paths on the way to their milking machine.

    Input

    * Line 1: A single line with three space-separated integers: K, C, and M.

    * Lines 2.. ...: Each of these K+C lines of K+C space-separated integers describes the distances between pairs of various entities. The input forms a symmetric matrix. Line 2 tells the distances from milking machine 1 to each of the other entities; line 3 tells the distances from machine 2 to each of the other entities, and so on. Distances of entities directly connected by a path are positive integers no larger than 200. Entities not directly connected by a path have a distance of 0. The distance from an entity to itself (i.e., all numbers on the diagonal) is also given as 0. To keep the input lines of reasonable length, when K+C > 15, a row is broken into successive lines of 15 numbers and a potentially shorter line to finish up a row. Each new row begins on its own line.

    Output

    A single line with a single integer that is the minimum possible total distance for the furthest walking cow.

    Sample Input

    2 3 2
    0 3 2 1 1
    3 0 3 2 0
    2 3 0 1 0
    1 2 1 0 2
    1 0 0 2 0
    

    Sample Output

    2
    #include <iostream>
    #include <cstring>
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    #include <cmath>
    #include <time.h>
    #include <string>
    #include <map>
    #include <stack>
    #include <vector>
    #include <set>
    #include <queue>
    #define inf 0x3f3f3f3f
    #define mod 10000
    typedef long long ll;
    using namespace std;
    const int N=305;
    const int M=300005;
    int power(int a,int b,int c){int ans=1;while(b){if(b%2==1){ans=(ans*a)%c;b--;}b/=2;a=a*a%c;}return ans;}
    int dis[N][N];
    int w[N][N];
    bool sign[N][N];
    bool used[N];
    int k,c,n,m;
    void Build_Graph(int min_max)
    {
        memset(w,0,sizeof(w));
        for(int i=1;i<=k;i++)w[0][i]=m;
        for(int i=k+1;i<=n;i++)w[i][n+1]=1;
        for(int i=1;i<=k;i++){
            for(int j=k+1;j<=n;j++){
                if(dis[i][j]<=min_max) w[i][j]=1;
            }
        }
    }
    bool BFS()
    {
        memset(used,false,sizeof(used));memset(sign,0,sizeof(sign));
        queue<int>q;
        q.push(0);used[0]=true;
        while(!q.empty()){
            int t=q.front();q.pop();
            for(int i=0;i<=n+1;i++){
                if(!used[i]&&w[t][i]){
                    q.push(i);
                    used[i]=true;
                    sign[t][i]=1;
                }
            }
        }
        if(used[n+1])return true;
        return false;
    }
    int DFS(int v,int sum)
    {
        if(v==n+1)return sum;
        int s=sum,t;
        for(int i=0;i<=n+1;i++){
            if(sign[v][i]){
                t=DFS(i,min(w[v][i],sum));
                w[v][i]-=t;
                w[i][v]+=t;
                sum-=t;
            }
        }
        return s-sum;
    }
    int main()
    {
        scanf("%d%d%d",&k,&c,&m);
        n=k+c;
        for(int i=1;i<=n;i++){
            for(int j=1;j<=n;j++){
                scanf("%d",&dis[i][j]);
                if(!dis[i][j])dis[i][j]=inf;
            }
        }
        for(int k=1;k<=n;k++){
            for(int i=1;i<=n;i++){
                if(dis[i][k]!=inf){
                    for(int j=1;j<=n;j++){
                        dis[i][j]=min(dis[i][j],dis[i][k]+dis[k][j]);
                    }
                }
            }
        }
        int l=0,r=10000;
        while(l<r){
            int mid=(l+r)/2;
            int ans=0;
            Build_Graph(mid);
            while( BFS() )ans+=DFS(0,inf);//Dinic求最大流
            if(ans>=c) r=mid;
            else l=mid+1;
        }
        printf("%d
    ",r);
        return 0;
    }
    View Code
  • 相关阅读:
    fixed 和 fixed
    SVN命令概要
    项目目录的划分
    作用域(scope), 定义空间(declaration space) 和 生存期(lifetime)
    可伸缩性最佳实践:来自eBay的经验(转)
    TCP连接可用性检测
    面向对象设计
    如何截取Http请求
    eBay架构
    .net3.5下的Socket通信框架
  • 原文地址:https://www.cnblogs.com/jianrenfang/p/5828880.html
Copyright © 2011-2022 走看看