当前位置:编程学习 > C/C++ >>

网络流 1009

Optimal Milking
Time Limit : 4000/2000ms (Java/Other)   Memory Limit : 60000/30000K (Java/Other)
 
Total Submission(s) : 5   Accepted Submission(s) : 2
 
Problem 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
 
题意:奶牛要走到产奶机器,每头牛只能到一个产奶机,每个机器有一定的容量。问走的最远的牛所走的最短路径是多少。
思路:floyd求最短路,二分答案,网络流求解。先求出点直接的距离。选择出路径的上界作为二分的上界,(开始的时候二人上界搞错了,wa了几次)。
建边:源点与牛建边,容量为1,机器与汇点建边,容量为机器的容纳量。牛与机器之间,距离小于二分的答案的两点建一条边。sap判断是否符合,符合改变上下界大小,最后输出结果即可。每次二分的一个答案重新建边时要重新初始化NE与head函数。开始的时候忘了,o(╯□╰)o……
[cpp]  
#include<iostream>  
#include<algorithm>  
#include<cstdio>  
#include<cstring>  
#define max(a,b) ((a)>(b)?(a):(b))  
using namespace std;  
const int N=420;  
const int M=82000;  
const int INF=99999999;  
int n;  
int gap[N],dis[N],pre[N],head[N],cur[N];  
int map[N][N];  
int NE,NV;  
struct Node  
{  
    int pos,next;  
    int c;  
} E[M];  
#define FF(i,NV) for(int i=0;i<NV;i++)  
int sap(int s,int t)  
{  
    memset(dis,0,sizeof(int)*(NV+1));  
    memset(gap,0,sizeof(int)*(NV+1));  
    FF(i,NV) cur[i] = head[i];  
    int u = pre[s] = s,maxflow = 0;  
    int aug =INF;  
    gap[0] = NV;  
    while(dis[s] < NV)  
    {  
loop:  
        for(int &i = cur[u]; i != -1; i = E[i].next)  
        {  
            int v = E[i].pos;  
            if(E[i].c && dis[u] == dis[v] + 1)  
            {  
                aug=min(aug,E[i].c);  
                pre[v] = u;  
                u = v;  
                if(v == t)  
                {  
                    maxflow += aug;  
                    for(u = pre[u]; v != s; v = u,u = pre[u])  
                    {  
                        E[cur[u]].c -= aug;  
                        E[cur[u]^1].c += aug;  
                    }  
                    aug =INF;  
                }  
                goto loop;  
            }  
        }  
        if( (--gap[dis[u]]) == 0)   break;  
        int mindis = NV;  
        for(int i = head[u]; i != -1 ; i = E[i].next)  
        {  
            int v = E[i].pos;  
            if(E[i].c && mindis > dis[v])  
            {  
                cur[u] = i;  
                mindis = dis[v];  
            }  
        }  
        gap[ dis[u] = mindis+1 ] ++;  
        u = pre[u];  
    }  
    return maxflow;  
}  
void addEdge(int u,int v,int c )  
{  
    E[NE].c = c;  
    E[NE].pos = v;  
    E[NE].next = head[u];  
    head[u] = NE++;  
  
    E[NE].c = 0;  
    E[NE].pos = u;  
    E[NE].next = head[v];  
    head[v] = NE++;  
}  
int ans;  
void floyed()  
{  
    for(int k=1; k<=n; k++)  
        for(int i=1; i<=n; i++)  
        {  
  &nb
补充:软件开发 , C++ ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,