poj 2112 Optimal Milking(spfa+二分+最大流)

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


首先求出每只牛到机器的最短路径,然后二分答案,用最大流。
#include <iostream>
#include <cstdio>
#include <cmath>
#include <vector>
#include <queue>
#include <algorithm>
#include <cstring>
#include <string>
using namespace std;

#define INF 1000000000
const int maxn = 300;
const int maxe = 300*300;
int K , C , M , dis[maxn][maxn] , mp[maxn][maxn];

int spfa_vis[maxn] , spfa_dis[maxn] , spfa_vt[maxn];

void spfa_init(){
    for(int i = 0; i < maxn; i++){
        spfa_vis[i] = 0;
        spfa_vt[i] = 0;
        spfa_dis[i] = INF;
    }
}

bool spfa(int start){
    spfa_init();
    queue<int> q;
    q.push(start);
    spfa_dis[start] = 0;
    spfa_vt[start]++;
    while(!q.empty()){
        int u = q.front();
        spfa_vis[u] = 0;
        if(spfa_vt[u]>maxn) return false;
        q.pop();
        for(int i = 1; i <= K+C; i++){
            if(mp[u][i] != 0 && spfa_dis[i] > spfa_dis[u]+mp[u][i]){
                spfa_dis[i] = spfa_dis[u]+mp[u][i];
                if(!spfa_vis[i]){
                    spfa_vis[i] = 1;
                    spfa_vt[i]++;
                    q.push(i);
                }
            }
        }
    }
    return true;
}


int dinic_d[maxn];
bool dinic_vis[maxn];
struct edge{
    int v,next,c;
}e[maxe];
int head[maxn],tol , s , t;
void dinic_init(){
    tol=0;
    memset(head,-1,sizeof head);
}
void AddEdge(int u,int v,int c)
{
    e[tol].v=v,e[tol].c=c,e[tol].next=head[u],head[u]=tol++;
    e[tol].v=u,e[tol].c=0,e[tol].next=head[v],head[v]=tol++;
}
bool bfs(){
    memset(dinic_d,0,sizeof dinic_d);
    memset(dinic_vis,0,sizeof dinic_vis);
    queue<int> q;
    q.push(s);
    dinic_vis[s]=1;
    while(!q.empty()){
        int u=q.front();
        q.pop();
        for(int i=head[u];i!=-1;i=e[i].next){
            int v=e[i].v;
            if(e[i].c&&!dinic_vis[v]){
                dinic_vis[v]=1;
                dinic_d[v]=dinic_d[u]+1;
                q.push(v);
            }
        }
    }
    return dinic_vis[t];
}
int dfs(int x,int pf){
    if(x==t) return pf;
    else{
        int ret=0;
        for(int i=head[x];i!=-1&&pf;i=e[i].next){
            int v=e[i].v;
            if(e[i].c&&dinic_d[v]==dinic_d[x]+1){
                int d=dfs(v,min(e[i].c,pf));
                e[i].c-=d;
                e[i^1].c+=d;
                pf-=d;
                ret+=d;
            }
        }
        return ret;
    }
}
int dinic(){
    int ret=0;
    while(bfs()){
        ret+=dfs(s,INF);
    }
    return ret;
}


void initial(){
    dinic_init();
    spfa_init();
    for(int i = 0; i < maxn; i++){
        for(int j = 0; j < maxn; j++) mp[i][j] = 0 , dis[i][j] = 0;
    }
}

void readcase(){
    for(int i = 1; i <= K+C; i++){
        for(int j = 1; j <= K+C; j++) scanf("%d" , &mp[i][j]);
    }
}

void build_graph(int cost){
    s = 0;
    t = K+C+1;
    dinic_init();
    for(int i = 1; i <= K; i++) AddEdge(i , t , M);
    for(int i = K+1; i <= K+C; i++) AddEdge(s , i , 1);
    for(int i = 1; i <= K; i++){
        for(int j = K+1; j <= K+C; j++){
            if(dis[i][j] < INF && dis[i][j] <= cost) AddEdge(j , i , 1);
        }
    }
}

void computing(){
    int MD = 0;
    for(int i = 1; i <= K; i++){
        if(spfa(i)){
            for(int j = K+1; j <= K+C; j++){
                dis[i][j] = spfa_dis[j];
                if(dis[i][j] < INF) MD = max(MD , dis[i][j]);
            }
        }
    }
    build_graph(INF);
    int Maxflow = dinic();
    //cout << MD << " " << Maxflow << endl;
    int l = 0 , r = MD;
    while(l < r){
        int mid = (l+r)/2;
        build_graph(mid);
        int flow = dinic();
        if(flow < Maxflow) l = mid+1;
        else r = mid;
    }
    printf("%d\n" , l);
}

int main(){
    while(~scanf("%d%d%d" , &K , &C , &M)){
        initial();
        readcase();
        computing();
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值