zoukankan      html  css  js  c++  java
  • TZOJ 1594 Optimal Milking(二分+最大流)

    描述

    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.

    输入

    * 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.

    输出

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

    样例输入

    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

    样例输出

    2

    题意

    给你(K+C)*(K+C)的图,K个牛奶机,每个牛奶机最多供M头牛,一共C头牛,问所有方案中使得距离牛奶机器最远的牛的距离最小

    题解

    先把牛奶机连汇点T流量M,牛连源点S流量1,牛和牛奶机连流量1,如果C牛都能有饮料机,则说明汇点T=C

    然后是怎么连牛和牛奶机的问题,可以知道答案求的是最大值最小

    直接二分答案[0,200*(K+C)]

    每次把距离<=mid的边加进去,如果T=C,则说明可行,r=mid

    否则l=mid

    代码

      1 #include<bits/stdc++.h>
      2 using namespace std;
      3 
      4 const int maxn=1e5+5;
      5 const int maxm=2e5+5;
      6 int n,m,S,T;
      7 int deep[maxn],q[400000];
      8 int FIR[maxn],TO[maxm],CAP[maxm],COST[maxm],NEXT[maxm],tote;
      9 
     10 void add(int u,int v,int cap)
     11 {
     12     TO[tote]=v;
     13     CAP[tote]=cap;
     14     NEXT[tote]=FIR[u];
     15     FIR[u]=tote++;
     16 
     17     TO[tote]=u;
     18     CAP[tote]=0;
     19     NEXT[tote]=FIR[v];
     20     FIR[v]=tote++;
     21 }
     22 bool bfs()
     23 {
     24     memset(deep,0,sizeof deep);
     25     deep[S]=1;q[1]=S;
     26     int head=0,tail=1;
     27     while(head!=tail)
     28     {
     29         int u=q[++head];
     30         for(int v=FIR[u];v!=-1;v=NEXT[v])
     31         {
     32             if(CAP[v]&&!deep[TO[v]])
     33             {
     34                 deep[TO[v]]=deep[u]+1;
     35                 q[++tail]=TO[v];
     36             }
     37         }
     38     }
     39     return deep[T];
     40 }
     41 int dfs(int u,int fl)
     42 {
     43     if(u==T)return fl;
     44     int f=0;
     45     for(int v=FIR[u];v!=-1&&fl;v=NEXT[v])
     46     {
     47         if(CAP[v]&&deep[TO[v]]==deep[u]+1)
     48         {
     49             int Min=dfs(TO[v],min(fl,CAP[v]));
     50             CAP[v]-=Min;CAP[v^1]+=Min;
     51             fl-=Min;f+=Min;
     52         }
     53     }
     54     if(!f)deep[u]=-2;
     55     return f;
     56 }
     57 int maxflow()
     58 {
     59     int ans=0;
     60     while(bfs())
     61         ans+=dfs(S,1<<30);
     62     return ans;
     63 }
     64 void init()
     65 {
     66     tote=0;
     67     memset(FIR,-1,sizeof FIR);
     68 }
     69 int K,C,N,M,a[250][250];
     70 int main()
     71 {
     72     cin>>K>>C>>M;
     73     N=K+C;
     74     for(int i=1;i<=N;i++)
     75         for(int j=1;j<=N;j++)
     76         {
     77             scanf("%d",&a[i][j]);
     78             if(i!=j&&!a[i][j])a[i][j]=0x3f3f3f3f;
     79         }
     80     for(int k=1;k<=N;k++)
     81         for(int i=1;i<=N;i++)
     82             for(int j=1;j<=N;j++)
     83                 if(a[i][j]>a[i][k]+a[k][j])
     84                     a[i][j]=a[i][k]+a[k][j];
     85     int l=0,r=200*N;
     86     S=0,T=K+C+1;
     87     while(r-l>1)
     88     {
     89         int mid=(l+r)>>1;
     90         init();
     91         for(int i=1;i<=K;i++)
     92             add(S,i,M);
     93         for(int i=K+1;i<=N;i++)
     94             add(i,T,1);
     95         for(int i=1;i<=K;i++)
     96             for(int j=K+1;j<=N;j++)
     97                 if(a[i][j]&&a[i][j]<=mid)
     98                     add(i,j,a[i][j]);
     99         int sum=maxflow();
    100         if(sum==C)r=mid;
    101         else l=mid;
    102     }
    103     printf("%d
    ",r);
    104     return 0;
    105 }
  • 相关阅读:
    Supervisor 管理进程,Cloud Insight 监控进程,完美!
    【灵魂拷问】你为什么要来学习Node.js呢?
    Web数据交互技术
    请求与上传文件,Session简介,Restful API,Nodemon
    Express服务器开发
    HTTP协议,到底是什么鬼?
    大学我都是自学走来的,这些私藏的实用工具/学习网站我贡献出来了,建议收藏精品推荐
    Node.js安装使用-VueCLI安装使用-工程化的Vue.js开发
    React开发环境准备
    【可视化】Vue基础
  • 原文地址:https://www.cnblogs.com/taozi1115402474/p/9535645.html
Copyright © 2011-2022 走看看