Optimal Milking POJ - 2112

本文介绍了一种利用网络流算法解决农场中奶牛到挤奶机路径优化问题的方法。通过使用Floyd算法预处理最短路径,并结合二分搜索与Dinic算法,实现了在满足挤奶机容量限制下最小化最远行走距离的目标。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

题意:让你求每个奶牛都能被挤奶的情况下,走的最远的奶牛最少要走多少路2333
建图思路:简单的想想 这首先是个供求的关系,应该用网络流建模,然后用floyd先把所有点之间的最短路求出来,再二分这个最长的路,就是二分答案,如果路小于这个二分值就建边.真的建图应该挺简单的..难得是预处理还有想这个二分2333建图的话我是把源点和挤奶器连一条容量为m的边,然后和能够到达的牛连一条1的边,再把牛和汇点连1的边2333然后就好了floyd不可达的边一定要INF INF INF 不然会对二分建图产生影响…wa了好几次 心疼自己2333

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<string>
#include<vector>
#include<set>
#include<queue>
#include<map>
#include<sstream>
#include<stack>
#include<functional>
#include<cctype>
using namespace std;
typedef long long ll;

//thanks to pyf ...
//thanks to lmd ...

#define INF 0x3f3f3f3f
#define CLR(a,b) memset(a,b,sizeof(a))
const int N = 1005;

int Map[N][N];

struct Edge
{
    int from,to,cap,flow;
    Edge(int u,int v,int c,int f) : from(u),to(v),cap(c),flow(f){}
};

struct Dinic
{
    int n,m,s,t;
    bool vis[N];
    int d[N];
    int cur[N];
    vector<Edge>edges;
    vector<int>G[N];
    void add_edge(int u,int v,int cap)
    {
        edges.push_back(Edge(u,v,cap,0));
        edges.push_back(Edge(v,u,0,0));
        int m = edges.size();
        G[u].push_back(m-2);
        G[v].push_back(m-1);
    }
    bool bfs()
    {
        CLR(vis,0);
        queue<int>q;
        q.push(s);
        d[s] = 0;
        vis[s] = 1;
        while(!q.empty())
        {
            int x = q.front();
            q.pop();
            for(int i=0;i<G[x].size();i++)
            {
                Edge &e = edges[G[x][i]];
                if(!vis[e.to]&&e.cap>e.flow)
                {
                    q.push(e.to);
                    vis[e.to] = 1;
                    d[e.to] = d[x] + 1;
                }
            }
        }
        return vis[t];
    }
    int dfs(int x,int a)
    {
        if(x==t||a==0)
            return a;
        int flow = 0,f;
        for(int &i=cur[x];i<G[x].size();i++)
        {
            Edge &e = edges[G[x][i]];
            if(d[x]+1==d[e.to]&&(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)
                    break;
            }
        }
        return flow;
    }
    int Maxflow(int s,int t)
    {
        this -> s = s;
        this -> t = t;
        int flow = 0;
        while(bfs())
        {
            CLR(cur,0);
            flow+=dfs(s,INF);
        }
        return flow;
    }
};
void short_path(int l)
{
    for(int i=1;i<=l;i++)
    {
        for(int j=1;j<=l;j++)
        {
            if(Map[i][j]==0)
                Map[i][j] = INF;
        }
    }
    for(int k=1;k<=l;k++)
    {
        for(int i=1;i<=l;i++)
        {
            if(Map[i][k]!=INF)
            {
                for(int j=1;j<=l;j++)
                {
                    Map[i][j] = min(Map[i][j],Map[i][k]+Map[k][j]);
                }
            }
        }
    }
}
void Create_Graph(int tar,Dinic & ans,int k,int c,int m)
{
    int s = 0,t = k+c+1;
    for(int i=k+1;i<=k+c;i++)
        ans.add_edge(0,i,1);
    for(int i=1;i<=k;i++)
        ans.add_edge(i,t,m);
    for(int i=k+1;i<=k+c;i++)
    {
        for(int j=1;j<=k;j++)
        {
            if(Map[i][j]<=tar)
                ans.add_edge(i,j,1);
        }
    }
}
int main()
{
    int k,c,m;
    while(scanf("%d%d%d",&k,&c,&m)==3)
    {
        CLR(Map,0);
        for(int i=1;i<=k+c;i++)
        {
            for(int j=1;j<=k+c;j++)
            {
                scanf("%d",&Map[i][j]);
            }
        }
        short_path(k+c);
        int l,r;
        int final_ans=-1;
        l = 0,r=100000;
        while(l<=r)
        {
            Dinic ans;
            int mid = (l+r)/2;
            Create_Graph(mid,ans,k,c,m);
            int res = ans.Maxflow(0,k+c+1);
            if(res>=c)
            {
                final_ans = mid;
                r = mid-1;
            }
            else
                l = mid+1;
        }
        printf("%d\n",final_ans);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值