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

Dinic+二分,1A留念

Posted by vjubge4 at 2019-06-01 20:26:04 on Problem 2455
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#include<queue>
using namespace std;
const int oo =0x3f3f3f3f;
int V, E, T;
struct edge{
    int to, cap, rev;
};
vector<edge> G[200];
int iter[200];
int level[200];
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;
        }
    }
}

int A[40000], B[40000], C[40000];

int Judge(int mid){
    for(int i =0; i < V; i ++){
        G[i].clear();
    }
    for(int i = 0; i < E; i ++){
        if(C[i] <= mid){
            add_edge(A[i], B[i], 1);
            add_edge(B[i], A[i], 1);
        }
    }
    return max_flow(0, V - 1);
}

int main(){
    scanf("%d %d %d", &V, &E, &T);
    int max_len = 0;
    for(int i =0; i < E; i ++){
        scanf("%d %d %d",  A + i, B + i, C + i);
        A[i] --;
        B[i] --;
        max_len = max(max_len, C[i]);
    }
    int lb = 0, ub = max_len;
    int ans =-1;
    while(ub >= lb){
        int mid = (ub + lb) >> 1;
        if(Judge(mid) >= T){
            ans = mid;
            ub = mid - 1;
        } else {
            lb = mid + 1;
        }
    }
    printf("%d\n", ans);
}

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