Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

拆点1A,Dinic,留念

Posted by vjubge4 at 2019-06-01 18:51:06 on Problem 3498
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#include<queue>
#include<cmath>
using namespace std;
const int oo = 0x3f3f3f3f;
int V;
struct edge {
    int to, cap, rev;
};
vector<edge> G[202];
int iter[202];
int level[202];

void add_edge(int from, int to, int cap) {
    G[from].push_back((edge) {
        to, cap, G[to].size()
    } );
    G[to].push_back( (edge) {
        from, 0, G[from].size() - 1
    });
}

void bfs(int s) {
    memset(level, -1, sizeof(level));
    level[s] = 0;
    queue<int> que;
    que.push(s);
    while(!que.empty()) {
        int v = que.front();
        que.pop();
        for(int i = 0; i < G[v].size(); i ++) {
            edge & e = G[v][i];
            if(e.cap > 0 && level[e.to] < 0) {
                level[e.to] = level[v] + 1;
                que.push(e.to);
            }
        }
    }
}

int dfs(int v, int t, int f) {
    if(v == t) {
        return f;
    }
    for(int & i = iter[v]; i < G[v].size(); i ++) {
        edge & e = G[v][i];
        if(e.cap > 0 && level[e.to] > level[v]) {
            int d = dfs(e.to, t, min(f, e.cap));
            if(d > 0) {
                e.cap -= d;
                G[e.to][e.rev].cap += d;
                return d;
            }
        }
    }
    return 0;
}

int max_flow(int s, int t) {
    int flow = 0;
    while(true) {
        bfs(s);
        if(level[t] < 0) {
            return flow;
        }
        memset(iter, 0, sizeof(iter));
        int f;
        while((f = dfs(s, t, oo)) > 0) {
            flow += f;
        }
    }
}

double Dist(int x1, int y1, int x2, int y2) {
    return sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1));
}

int X[100], Y[100], N[100], D[100];
int M, Sum;
double max_d;

void Build() {
    for(int i = 0; i < V; i ++){
        G[i].clear();
    }
    int s = M * 2;
    for(int i = 0; i < M; i ++) {
        add_edge(s, i, N[i]);
        add_edge(i, M + i, D[i]);
    }
    for(int i = 0; i < M; i ++) {
        for(int j = i + 1; j < M; j ++) {
            if(Dist(X[i], Y[i], X[j], Y[j]) <= max_d) {
                add_edge(M + i, j, oo);
                add_edge(M + j, i, oo);
            }
        }
    }
}

int main() {
    int Num;
    scanf("%d", &Num);
    for(int num = 0; num < Num; num ++) {
        scanf("%d %lf", &M, &max_d);
        Sum = 0;
        for(int i = 0; i < M; i ++) {
            scanf("%d %d %d %d", X + i, Y + i, N + i, D + i);
            Sum += N[i];
        }
        V = M * 2 + 1;
        int s = 2 * M;
        int c= 0;
        for(int i = 0; i < M; i ++){
            int t = i;
            Build();
            if(max_flow(s, t) == Sum){
                printf("%d ", i);
                c++;
            }
        }
        if(c == 0){
            printf("-1");
        }
        puts("");
    }
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator