Optimal Milking POJ - 2112

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);
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章