Cod sursa(job #2555936)

Utilizator memecoinMeme Coin memecoin Data 24 februarie 2020 15:58:20
Problema Adapost Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 3.99 kb
#include <fstream>
#include <string>
#include <stdio.h>
#include <vector>
#include <algorithm>
#include <math.h>
#include <set>
#include <map>
#include <string.h>
#include <queue>
#include <stack>
#include <iomanip>

#define INF 0x3f3f3f3f
#define real long double

using namespace std;

#ifdef DEBUG
string name = "data";
#else
string name = "adapost";
#endif

ifstream fin(name + ".in");
ofstream fout(name + ".out");

#define MAXN 820

int n, m, s, t;

int c[MAXN][MAXN];
int f[MAXN][MAXN];
real d[MAXN][MAXN];

vector<int> g[MAXN];

int parent[MAXN];

bool inQueue[MAXN];
real dist[MAXN];

struct Point {
    real x, y;
};

real pDist(Point a, Point b) {
    return sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y));
}

int np;
Point soldiers[MAXN];
Point outpost[MAXN];

bool bellmanFord() {
    memset(inQueue, false, sizeof(inQueue));
    for (int i = 0; i <= np * 2 + 1; ++i) {
        dist[i] = INF;
    }
    
    queue<int> q;
    
    q.push(s);
    inQueue[s] = true;
    dist[s] = 0;
    bool reachedSink = false;
    
    while (!q.empty()) {
        auto node = q.front();
        q.pop();
        inQueue[node] = false;
        
        for (auto x: g[node]) {
            bool allowsFlow = f[node][x] != c[node][x];
            
            if (allowsFlow && (dist[x] > dist[node] + d[node][x])) {
                dist[x] = dist[node] + d[node][x];
                parent[x] = node;
                
                if (!inQueue[node]) {
                    q.push(x);
                    inQueue[x] = true;
                }
                
                if (x == t) {
                    reachedSink = true;
                }
            }
        }
    }
    
    return reachedSink;
}

real flow() {
    memset(f,0,sizeof(f));
    int flow = 0;
    real cost = 0;
    
    while (bellmanFord()) {
        int minFlow = INF;
        
        int node = t;
        
        while (node != s) {
            int p = parent[node];
            
            minFlow = min(minFlow, c[p][node] - f[p][node]);
            
            node = p;
        }
        
        node = t;
        
        while (node != s) {
            int p = parent[node];
            cost += minFlow * d[p][node];
            
            f[p][node] += minFlow;
            f[node][p] -= minFlow;
            
            node = p;
        }
        
        flow += minFlow;
    }
    
    return flow == np ? cost : INF;
}

vector<real> dd;

void connect(real limit) {
    for (int i = 1; i <= np; ++i) {
        for (int j = np + 1; j <= 2 * np; ++j) {
            c[i][j] = d[i][j] <= limit;
            g[i].push_back(j);
            g[j].push_back(i);
        }
    }
}

int search(int low, int high, real target) {
    if (low < 0 || low > high) {
        return -1;
    }
    
    int mid = (low + high) / 2;
    
    connect(dd[mid]);
    auto v = flow();
    
    if (v > target) {
        return search(mid + 1, high, target);
    }
    
    if (v < target) {
        return search(low, mid - 1, target);
    }
    
    connect(dd[mid - 1]);
    if (flow() > v) {
        return mid;
    }
    
    return search(low, mid - 1, target);
}

int main() {
    
    fin >> np;
    
    s = 0;
    t = np * 2 + 1;
    
    for (int i = 1; i <= np; ++i) {
        real x,y;
        fin >> x >> y;
        soldiers[i] = {x, y};
        
        g[0].push_back(i);
        g[i].push_back(0);
        c[0][i] = 1;
    }
    
    for (int i = 1; i <= np; ++i) {
        real x,y;
        fin >> x >> y;
        outpost[i] = {x, y};
        
        g[np + i].push_back(t);
        g[t].push_back(np + i);
        c[np + i][t] = 1;
    }
    
    for (int i = 1; i <= np; ++i) {
        for (int j = np + 1; j <= 2 * np; ++j) {
            d[i][j] = pDist(soldiers[i], outpost[j - np]);
            dd.push_back(d[i][j]);
            d[j][i] = -d[i][j];
        }
    }
    
    sort(dd.begin(), dd.end());
    
    connect(dd[dd.size() - 1]);
    real f = flow();
    
    int ndx = 0;
    
    fout << setprecision(9) << dd[ndx] << " " <<setprecision(9) << f;
    
    return 0;
}