POJ-1459 Power Network

题目大意:

这道题目意思不好理解(可能只是我理解太差...)

一个电网,里面有一些结点,代表电站,消费者,调度站。电站不消耗电能,消费者不产生电能,调度站产生的电能和消耗的电能均为0

现在给出一个电网,有n个结点,np个电站(u)z[表示u号节点是能产生最多z电能的电站],nc个消费者(u)z[表示u是最多消耗z电能的消费者],m条输电线(u,v)z[表示从u到z最多能输送z电能],现在问你最这个电网最多能消费多少电能。

解题思路:

这是标准的网络流模型= =

首先把电站跟源点连接,容量为z

再把想消费者与汇点连接,容量为z

再把m条输电线连接,容量为z

然后就是直接裸着写网络流就可以了= =

代码:

#include <queue>
#include <cstdio>
#include <cstring>
using namespace std;

typedef struct node{
    int v, cap, nxt;
    node(int a = 0, int b = 0, int c = 0){
        v = a; cap = b; nxt = c;
    }
}Edge;

const int maxm = 150;
const int maxn = 1e5 + 5;
const int INF = 0x3f3f3f3f;

int s, t, tot;
Edge edge[maxn];
int head[maxn], dis[maxm];

inline int Min(int a, int b){
    return (a < b ? a : b);
}
void add(int u, int v, int cap){
    edge[tot] = Edge(v, cap, head[u]);
    head[u] = tot++;
    edge[tot] = Edge(u, 0, head[v]);
    head[v] = tot++;
}
int bfs(){
    queue<int> q;
    while(!q.empty()) q.pop();
    memset(dis, 0, sizeof(dis));
    q.push(s); dis[s] = 1;
    while(!q.empty()){
        int x = q.front(); q.pop();
        for(int i = head[x]; ~i; i = edge[i].nxt){
            Edge &e = edge[i];
            if(e.cap && dis[e.v] == 0){
                dis[e.v] = dis[x] + 1;
                q.push(e.v);
            }
        }
    }
    return dis[t];
}
int dfs(int x, int f){
    if(x == t) return f;
    int sum = 0;
    for(int i = head[x]; ~i; i = edge[i].nxt){
        Edge &e = edge[i];
        if(e.cap && dis[e.v] == dis[x] + 1){
            int ret = dfs(e.v, Min(f, e.cap));
            sum += ret; f -= ret;
            e.cap -= ret; edge[i^1].cap += ret;
        }
    }
    return sum;
}
int dinic(){
    int ret = 0;
    while(bfs()) ret += dfs(s, INF);
    return ret;
}
int main(){
    int u, v, z, n, np, nc, m;
    while(~scanf("%d%d%d%d", &n, &np, &nc, &m)){
        tot = 0; s = n + 1; t = s + 1;
        memset(head, -1, sizeof(head));
        for(int i = 0; i < m; ++i){
            scanf(" (%d,%d)%d", &u, &v, &z);
            add(u, v, z);
        }
        for(int i = 0; i < np; ++i){
            scanf(" (%d)%d", &u, &z);
            add(s, u, z);
        }
        for(int i = 0; i < nc; ++i){
            scanf(" (%d)%d", &u, &z);
            add(u, t, z);
        }
        printf("%d\n", dinic());
    }
    return 0;
}


posted @ 2016-08-08 22:25  _Wilbert  阅读(116)  评论(0编辑  收藏  举报