天天看點

[HDU] 3549 Flow Problem [最大流][Dinic][讀取優化]

Problem Description

Network flow is a well-known difficult problem for ACMers. Given a graph, your task is to find out the maximum flow for the weighted directed graph.

Input

The first line of input contains an integer T, denoting the number of test cases.

For each test case, the first line contains two integers N and M, denoting the number of vertexes and edges in the graph. (2 <= N <= 15, 0 <= M <= 1000)

Next M lines, each line contains three integers X, Y and C, there is an edge from X to Y and the capacity of it is C. (1 <= X, Y <= N, 1 <= C <= 1000)

Output

For each test cases, you should output the maximum flow from source 1 to sink N.

Sample Input

2

3 2

1 2 1

2 3 1

3 3

1 2 1

2 3 1

1 3 1

Sample Output

Case 1: 1

Case 2: 2

link

題解

使用Dinic的複雜度是 O(E×V^2)

發現對其進行讀取優化,效率立馬提升3倍,可見讀取優化對競賽的重要性

[HDU] 3549 Flow Problem [最大流][Dinic][讀取優化]
#include<cstdio>
#include<cstring>
#include<vector>
#include<queue>
#include<algorithm>
#define INF 0x3f3f3f3f
#define MAX_V 20
using namespace std;
typedef long long LL;

const int MAXS = **;
char buf[MAXS],bufout[MAXS],*ch,*chout;

void read(int &x){
    for(++ch;*ch<=;++ch);
    for(x=;*ch>='0';++ch) x=x*+*ch-'0';
}

void out(int x){
    if(!x) *(++chout)='0';
    else{
        char *ch0=chout,*ch1=chout+;
        while(x){
            *(++ch0)=x%+'0';
            x/=;
        }
        chout=ch0;
        while(ch1<=ch0) swap(*(ch1++),*(ch0--));
    }
    *(++chout)='\n';
}

void std_init(){
    ch=buf-;
    chout=bufout-;
    fread(buf,,MAXS,stdin);
}

void std_out(){
    fwrite(bufout,,chout-bufout+,stdout);
}

/*---------------------------------------------------------------*/
struct edge{int to,cap,rev;};

vector<edge> G[MAX_V];
int lever[MAX_V];
int iter[MAX_V];

void add_edge(int x,int y,int cost){
    G[x].push_back((edge){y,cost,G[y].size()});
    G[y].push_back((edge){x,,G[x].size()-});
}
void del_graph(int N){for(int i=;i<=N;i++) while(!G[i].empty()) G[i].pop_back();}

void bfs(int s){
    memset(lever,-,sizeof(lever));
    queue<int> que;
    lever[s]=;
    que.push(s);
    while(!que.empty()){
        int v=que.front();que.pop();
        for(int i=;i<G[v].size();i++){
            edge &e=G[v][i];
            if(lever[e.to]<&&e.cap>){
                lever[e.to]=lever[v]+;
                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>&&lever[v]<lever[e.to]){
            int d=dfs(e.to,t,min(f,e.cap));
            if(d>){
                e.cap-=d;
                G[e.to][e.rev].cap+=d;
                return d;
            }
        }
    }
    return ;
}

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

int main()
{
    std_init();
    int T,N,M;
    read(T);
    for(int t=;t<=T;t++){
        read(N);read(M);
        int from,to,cost;
        for(int i=;i<M;i++){
            read(from);read(to);read(cost);
            add_edge(from,to,cost);
        }
        printf("Case %d: %d\n",t,max_flow(,N));
        del_graph(N);
    }
    return ;
}
           

繼續閱讀