poj 2112(二分+网络流)

本文解析了OptimalMilking问题,通过Floyd算法计算最短路径,采用二分法结合最大流算法确定奶牛到挤奶机的最佳分配方案,以最小化最远行走距离。

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

Optimal Milking
Time Limit: 2000MS Memory Limit: 30000K
Total Submissions: 15749 Accepted: 5617
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

题意:有K台挤奶机,C头奶牛,每台挤奶机可以容纳M头奶牛,挤奶机和奶牛两两之间都有个距离,现在问在保证所有的奶牛都可以产奶的情况下,走到挤奶机需要走最远的奶牛的最短要走的距离是多少?
题解:先用floyed算法算出每头奶牛和挤奶机之间的最短路径,在保证所有奶牛都能够产奶的情况下二分求解,设立超级源点S,S向每台挤奶机之间连容量为M的边,每台挤奶机向奶牛连容量为1的边,所有奶牛
向超级汇点连容量为1的边,求解最大流。
#include <stdio.h>
#include <algorithm>
#include <queue>
#include <string.h>
#include <math.h>
#include <iostream>
#include <math.h>
using namespace std;
const int N = 305;
const int INF = 999999999;
struct Edge
{
    int v,next;
    int w;
} edge[N*N];
int head[N];
int level[N];
int tot;
void init()
{
    memset(head,-1,sizeof(head));
    tot=0;
}
void addEdge(int u,int v,int w,int &k)
{
    edge[k].v = v,edge[k].w=w,edge[k].next=head[u],head[u]=k++;
    edge[k].v = u,edge[k].w=0,edge[k].next=head[v],head[v]=k++;
}
int BFS(int src,int des)
{
    queue<int >q;
    memset(level,0,sizeof(level));
    level[src]=1;
    q.push(src);
    while(!q.empty())
    {
        int u = q.front();
        q.pop();
        if(u==des) return 1;
        for(int k = head[u]; k!=-1; k=edge[k].next)
        {
            int v = edge[k].v;
            int w = edge[k].w;
            if(level[v]==0&&w!=0)
            {
                level[v]=level[u]+1;
                q.push(v);
            }
        }
    }
    return -1;
}
int dfs(int u,int des,int increaseRoad)
{
    if(u==des||increaseRoad==0) return increaseRoad;
    int ret=0;
    for(int k=head[u]; k!=-1; k=edge[k].next)
    {
        int v = edge[k].v,w=edge[k].w;
        if(level[v]==level[u]+1&&w!=0)
        {
            int MIN = min(increaseRoad-ret,w);
            w = dfs(v,des,MIN);
            if(w > 0)
            {
                edge[k].w -=w;
                edge[k^1].w+=w;
                ret+=w;
                if(ret==increaseRoad) return ret;
            }
            else level[v] = -1;
            if(increaseRoad==0) break;
        }
    }
    if(ret==0) level[u]=-1;
    return ret;
}
int Dinic(int src,int des)
{
    int ans = 0;
    while(BFS(src,des)!=-1) ans+=dfs(src,des,INF);
    return ans;
}
int graph[N][N];
int k,c,m;
int floyed(int n)
{
    int MAX=-1;
    for(int k=1; k<=n; k++)
    {
        for(int i=1; i<=n; i++)
        {
            for(int j=1; j<=n; j++)
            {
                graph[i][j] = min(graph[i][j],graph[i][k]+graph[k][j]);
            }
        }
    }
    for(int i=1;i<=n;i++){
        for(int j=1;j<=n;j++){
            if(graph[i][j]!=INF)
            MAX = max(MAX,graph[i][j]);
        }
    }
    return MAX;
}
int build(int v){
    init();
    int src = 0,des = k+c+1;
    for(int i=1;i<=k;i++) addEdge(src,i,m,tot);
    for(int i=k+1;i<=k+c;i++) addEdge(i,des,1,tot);
    for(int i=1;i<=k;i++){
        for(int j=k+1;j<=k+c;j++){
            if(graph[i][j]<=v) addEdge(i,j,1,tot);
        }
    }
    return Dinic(src,des);
}
int main()
{
    while(scanf("%d%d%d",&k,&c,&m)!=EOF)
    {
        for(int i=1;i<=k+c;i++){
            for(int j=1;j<=k+c;j++){
                scanf("%d",&graph[i][j]);
                if(graph[i][j]==0&&i!=j) graph[i][j] = INF;
            }
        }
        int MAX = floyed(k+c);
        int l=1,r = MAX;
        int ans = MAX;
        while(l<=r){
            int mid = (l+r)>>1;
            if(build(mid)==c) {
                ans = mid;
                r = mid-1;
            }
            else l =mid+1;
        }
        printf("%d\n",ans);
    }
}

 

转载于:https://www.cnblogs.com/liyinggang/p/5555324.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值