BNUOJ 12887 isumi(計算幾何+最大流)

Tony5t4rk發表於2019-02-21

Description:

isumi是個斐波那契數迷。他是如此的酷愛這個數列,因此他想知道很多關於這個數列的東西,比方說第N個斐波那契數是多少啊、前N項的和是多少啊如何用若干個斐波那契數的和表示一個自然數啊之類之類的。

今天他希望能想出一個有關斐波那契數的問題能難倒易牛。當然,這需要想相當長的時間,他想到該去吃飯了依然沒想出題來,因此他決定邊去吃飯邊想。isumi的宿舍到食堂之間的路可以看做一個矩形,宿舍在x=0的線的左邊,食堂在x=l的線的右邊。isumi可以從x=0上的任意一個點出發,只要到達x=l的任意一個點就可以,不過在走的時候不能走出路、也就是矩形的外面,不然正在思考問題的isumi說不定會撞上電線杆- -||

isumi用望遠鏡觀察到在路上有N只大牛(編號為0,1,…N-1),第i只大牛所在的位置為(xi,yi)(大牛們不會移動,並且每隻大牛都在路、也就是矩形之內),根據isumi對這些大牛的瞭解,只要isumi出現在某隻大牛的視線範圍之內這隻大牛就會衝過來問isumi一道神題來虐他,虐完之後該大牛就會心滿意足的離開……每隻大牛的視力不同,當isumi距離第i只大牛不超過di時就會被這隻大牛發現。

現在isumi希望知道他至少要被幾隻大牛發現才能到達食堂。

Input:

第一行為三個整數l,w,N(0<l, w≤10000, 1≤N≤100),接下來的N行每行三個整數xi,yi,di(0<xi<l, 0<yi<w, 0≤di≤1000)。

Output:

輸出一個整數表示isumi至少要被幾隻大牛發現才能到達食堂。

Sample Input:

500 300 5
250 1 75
250 150 75
250 299 100
100 150 80
400 150 20

Sample Output:

1

題目連結

如圖,若 isumiisumi 向要從左邊的宿舍走到右邊的食堂則它需要橫向穿過矩形區域 ABCDABCD

考慮在矩形上面設立源點,矩形下面設立匯點根據圓與矩形邊界的相交關係、圓與圓的相交關係建圖

在途中很顯然的可以看出若 isumiisumi 想要最少地被大牛發現橫向穿過矩形區域則他必須穿過建圖關係中最少的邊

這樣問題就轉化為網路流中的最小割問題,又因為最大流最小割定理所以求出建圖關係中的最大流即為答案

AC程式碼:

#include <bits/stdc++.h>
using namespace std;
typedef double db;
const int INF = 0x3f3f3f3f;
const int maxn = 1e4 + 5;
const db eps = 1e-9;

int Sgn(db Key) {return fabs(Key) < eps ? 0 : (Key < 0 ? -1 : 1);}
int Cmp(db Key1, db Key2) {return Sgn(Key1 - Key2);}
struct Point {db X, Y;};
typedef Point Vector;
Vector operator - (Vector Key1, Vector Key2) {return (Vector){Key1.X - Key2.X, Key1.Y - Key2.Y};}
Vector operator + (Vector Key1, Vector Key2) {return (Vector){Key1.X + Key2.X, Key1.Y + Key2.Y};}
db operator * (Vector Key1, Vector Key2) {return Key1.X * Key2.X + Key1.Y * Key2.Y;}
db operator ^ (Vector Key1, Vector Key2) {return Key1.X * Key2.Y - Key1.Y * Key2.X;}
db GetLen(Vector Key) {return sqrt(Key * Key);}
db DisPointToPoint(Point Key1, Point Key2) {return GetLen(Key2 - Key1);}
struct Line {Point S, T;};
typedef Line Segment;
struct Circle {Point Center; db Radius;};
bool IsCircleInterCircle(Circle Key1, Circle Key2) {return Cmp(DisPointToPoint(Key1.Center, Key2.Center), Key1.Radius + Key2.Radius) <= 0;}

struct Edge {int V, Weight, Next;};

Edge edges[maxn << 4];
int Head[maxn];
int Tot;
int Depth[maxn];
int Current[maxn];

void GraphInit() {Tot = 0; memset(Head, -1, sizeof(Head));}
void AddEdge(int U, int V, int Weight, int ReverseWeight = 0) {
    edges[Tot] = (Edge){V, Weight, Head[U]};
    Head[U] = Tot++;
    edges[Tot] = (Edge){U, ReverseWeight, Head[V]};
    Head[V] = Tot++;
}

bool Bfs(int Start, int End) {
    memset(Depth, -1, sizeof(Depth));
    std::queue<int> Que;
    Depth[Start] = 0;
    Que.push(Start);
    while (!Que.empty()) {
        int Cur = Que.front();
        Que.pop();
        for (int i = Head[Cur]; ~i; i = edges[i].Next) {
            if (Depth[edges[i].V] == -1 && edges[i].Weight > 0) {
                Depth[edges[i].V] = Depth[Cur] + 1;
                Que.push(edges[i].V);
            }
        }
    }
    return Depth[End] != -1;
}

int Dfs(int Cur, int End, int NowFlow) {
    if (Cur == End || NowFlow == 0) return NowFlow;
    int UsableFlow = 0, FindFlow;
    for (int &i = Current[Cur]; ~i; i = edges[i].Next) {
        if (edges[i].Weight > 0 && Depth[edges[i].V] == Depth[Cur] + 1) {
            FindFlow = Dfs(edges[i].V, End, std::min(NowFlow - UsableFlow, edges[i].Weight));
            if (FindFlow > 0) {
                edges[i].Weight -= FindFlow;
                edges[i ^ 1].Weight += FindFlow;
                UsableFlow += FindFlow;
                if (UsableFlow == NowFlow) return NowFlow;
            }
        }
    }
    if (!UsableFlow) Depth[Cur] = -2;
    return UsableFlow;
}

int Dinic(int Start, int End) {
    int MaxFlow = 0;
    while (Bfs(Start, End)) {
        for (int i = Start; i <= End; ++i) Current[i] = Head[i];
        MaxFlow += Dfs(Start, End, INF);
    }
    return MaxFlow;
}

bool CheckCircle(Circle Key1, db Key2) {
    return Cmp(Key1.Center.Y - Key1.Radius, Key2) <= 0 && Cmp(Key1.Center.Y + Key1.Radius, Key2) >= 0;
}

db L, W;
int N;
Circle Daniel[maxn];

int main(int argc, char *argv[]) {
    scanf("%lf%lf%d", &L, &W, &N);
    for (int i = 1; i <= N; ++i) scanf("%lf%lf%lf", &Daniel[i].Center.X, &Daniel[i].Center.Y, &Daniel[i].Radius);
    GraphInit();
    for (int i = 1; i <= N; ++i) AddEdge(i, N + i, 1);
    for (int i = 1; i <= N; ++i) {
        if (CheckCircle(Daniel[i], 0)) AddEdge(0, i, INF);
        if (CheckCircle(Daniel[i], W)) AddEdge(N + i, 2 * N + 1, INF);
    }
    for (int i = 1; i <= N; ++i) {
        for (int j = i + 1; j <= N; ++j) {
            if (IsCircleInterCircle(Daniel[i], Daniel[j])) {
                AddEdge(N + i, j, INF);
                AddEdge(N + j, i, INF);
            }
        }
    }
    printf("%d\n", Dinic(0, 2 * N + 1));
    return 0;
}

相關文章