zoukankan      html  css  js  c++  java
  • 18.11.28 POJ 2112 Optimal Milking(网络流+二分法+Floyd)

    描述

    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
    

    来源

    USACO 2003 U S Open

      1 #include <iostream>
      2 #include <string.h>
      3 #include <algorithm>
      4 #include <stack>
      5 #include <string>
      6 #include <math.h>
      7 #include <queue>
      8 #include <stdio.h>
      9 #include <string.h>
     10 #include <vector>
     11 #include <fstream>
     12 #include <set>
     13 #define maxn 255
     14 #define inf 0x7fffffff
     15 
     16 using namespace std;
     17 int G[maxn][maxn],m,c,k;
     18 int route[maxn][maxn];
     19 int maxdist;
     20 
     21 void clear() {
     22     memset(G, 0, sizeof(G));
     23     for (int i = 1; i <= c; i++)
     24         G[0][k+i] = 1;
     25     for (int i = 1; i <= k; i++)
     26         G[i][k+1+c] = m;
     27     for (int i = k + 1; i <= k + c; i++)
     28         for (int j = 1; j <= k; j++)
     29             if (route[i][j] <= maxdist)
     30                 G[i][j] = 1;
     31 }
     32 
     33 void floyd() {
     34     int size = k + c;
     35     for (int k = 1; k <= size; k++)
     36         for (int i = 1; i <= size; i++)
     37             for (int j = 1; j <= size; j++)
     38                 route[i][j] = min(route[i][k] + route[k][j], route[i][j]);
     39 }
     40 
     41 int Prev[maxn];
     42 bool visited[maxn];
     43 
     44 int findpath() {
     45     int v, i;
     46     deque<int>q;
     47     memset(Prev, 0, sizeof(Prev));
     48     memset ( visited, 0, sizeof(visited));
     49     Prev[0] = -1;
     50     visited[0] = true;
     51     q.push_back(0);
     52     bool findPath = false;
     53     while (!q.empty()) {
     54         v = q.front();
     55         q.pop_front();
     56         for (i = 0; i <= k + c + 1; i++) {
     57             if (G[v][i] > 0 && visited[i] == false) {
     58                 Prev[i] = v;
     59                 visited[i] = true;
     60                 if (i == k + c + 1) {
     61                     findPath = true;
     62                     q.clear();
     63                     break;
     64                 }
     65                 else
     66                     q.push_back(i);
     67             }
     68         }
     69     }
     70     if (!findPath)
     71         return 0;
     72     v = k + c + 1;
     73     while (Prev[v]!=-1) {
     74         G[Prev[v]][v] --;
     75         G[v][Prev[v]]++;
     76         v = Prev[v];
     77     }
     78     return 1;
     79 }
     80 
     81 void init() {
     82     scanf("%d%d%d", &k, &c, &m);
     83     int size = k + c;
     84     for (int i = 1; i <= size; i++)
     85         for (int j = 1; j <= size; j++)
     86         {
     87             scanf("%d", &route[i][j]);
     88             if (route[i][j] == 0)route[i][j] = 999999;
     89         }
     90     floyd();
     91     int s = 0,e=10000;
     92     while (s<e) {
     93         maxdist = (s + e) / 2;
     94         clear();
     95         int flow=0, aug;
     96         while (aug = findpath())
     97             flow += aug;
     98         if (flow==c)
     99             e = maxdist;
    100         else
    101             s = maxdist + 1;
    102     }
    103     printf("%d
    ", s);
    104 }
    105 
    106 int main()
    107 {
    108     init();
    109     return 0;
    110 }
    View Code

    我为啥要作死用EK做呢???

    慢的要死,也没短多少……懒了一下

    因为数字写成字母/inf写得太大/二分法写错等原因WA了蛮久的……

    注定失败的战争,也要拼尽全力去打赢它; 就算输,也要输得足够漂亮。
  • 相关阅读:
    请求返回结果模板
    Oracle的sql语句中case关键字的用法 & 单双引号的使用
    java如何从方法返回多个值
    junit的简单用法
    java命令启动jar包
    Fastjson-fastjson中$ref对象重复引用问题
    指定cmd窗口或tomcat运行窗口的名称
    Spring boot配置log4j输出日志
    The import XXX cannot be resolved
    斐波那契数列
  • 原文地址:https://www.cnblogs.com/yalphait/p/10022348.html
Copyright © 2011-2022 走看看