zoukankan      html  css  js  c++  java
  • Optimal Milking POJ

     
    Optimal Milking
    Time Limit: 2000MS   Memory Limit: 30000K
    Total Submissions: 19347   Accepted: 6907
    Case Time Limit: 1000MS
    issions: 19347   Accepted: 6907
    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
    

    Source

    求行走距离的最远的奶牛的至少要走多远。

    注意要先用Floyd求每两点之间的最短路。。。。。。

    #include <iostream>
    #include <cstring>
    #include <cstdio>
    #include <cmath>
    #include <algorithm>
    #include <queue>
    #define mem(a,b) memset(a,b,sizeof(a))
    using namespace std;
    const int maxn = 3000, INF = 0x3f3f3f3f;
    typedef long long LL;
    
    int head[maxn], d[maxn], vis[maxn], p[maxn], f[maxn], way[500][500];
    int n, m, s, t, neng;
    int cnt, flow, value;
    
    struct node{
        int u, v, c, w, next;
    }Node[15000];
    
    void add_(int u, int v, int c, int w)
    {
        Node[cnt].u = u;
        Node[cnt].v = v;
        Node[cnt].c = c;
        Node[cnt].w = w;
        Node[cnt].next = head[u];
        head[u] = cnt++;
    }
    
    void add(int u, int v, int c, int w)
    {
        add_(u, v, c, w);
        add_(v, u, 0, -w);
    }
    
    int spfa()
    {
        queue<int> Q;
        for(int i=0; i<maxn; i++) d[i] = INF;
        d[s] = 0;
        mem(vis, 0);
        mem(p, -1);
        Q.push(s);
        vis[s] = 1;
        p[s] = 0; f[s] = INF;
        while(!Q.empty())
        {
            int u = Q.front(); Q.pop();
            vis[u] = 0;
            for(int i=head[u]; i!=-1; i=Node[i].next)
            {
                node e = Node[i];
                if(d[e.v] > max(d[e.u], e.w) && e.c > 0)
                {
                    d[e.v] = max(d[e.u], e.w);
                    p[e.v] = i;
                    f[e.v] = min(f[u], e.c);
                    if(!vis[e.v])
                    {
                        Q.push(e.v);
                        vis[e.v] = 1;
                    }
                }
            }
        }
        if(p[t] == -1) return 0;
     //  cout<< value <<endl;
        flow += f[t], value = d[t];
        for(int i=t; i!=s; i=Node[p[i]].u)
        {
            Node[p[i]].c -= f[t];
            Node[p[i]^1].c += f[t];
        }
        return 1;
    }
    
    void max_flow()
    {
        while(spfa());
        printf("%d
    ",value);
    }
    
    
    int main()
    {
        mem(head, -1);
        mem(way, INF);
        cnt = 0;
        scanf("%d%d%d", &n, &m, &neng);
        for(int i=1; i<=n+m; i++)
            way[i][i] = 0;
    
        for(int i=1; i<=n+m; i++)
        {
            for(int j=1; j<=n+m; j++)
            {
                int w;
                scanf("%d",&w);
                if(w) way[i][j] = w;
    
            }
        }
        for(int k=1;k<=n+m;k++)
            for(int i=1;i<=n+m;i++)
                for(int j=1;j<=n+m;j++)
                    way[i][j]=min(way[i][j],way[i][k]+way[k][j]);
        for(int i=1; i<=m; i++)
            for(int j=1; j<=n; j++)
                if(way[n+i][j] < INF)
                    add(n+i, j, 1, way[n+i][j]);
    
        s = 0; t = n + m + 1;
        for(int i=1; i<=m; i++)
            add(s, n+i, 1, 0);
        for(int j=1; j<=n; j++)
            add(j, t, neng, 0);
        max_flow();
    
    
        return 0;
    }
    自己选择的路,跪着也要走完。朋友们,虽然这个世界日益浮躁起来,只要能够为了当时纯粹的梦想和感动坚持努力下去,不管其它人怎么样,我们也能够保持自己的本色走下去。
  • 相关阅读:
    4. 单向循环链表
    3. 单向链表
    2. 队列的实现
    1. 时间复杂度(大O表示法)以及使用python实现栈
    Ubuntu16.04LTS安装集成开发工具IDE: CodeBlocks 和Eclipse-cdt
    Ubuntu16.04搭建各种开发环境的IDE: QT5 , CodeBlocks ,eclipse-cdt, PyCharm
    python2和python3 分别连接MySQL的代码
    面向对象基础
    文件的处理
    离散表(哈希表),深浅拷贝
  • 原文地址:https://www.cnblogs.com/WTSRUVF/p/9313394.html
Copyright © 2011-2022 走看看