poj_1459 Power Network(多源多汇最大网络流)(EK / dinic / ISAP)

21 篇文章 0 订阅
2 篇文章 0 订阅
Power Network
Time Limit: 2000MS Memory Limit: 32768K
Total Submissions: 27462 Accepted: 14275

Description

A power network consists of nodes (power stations, consumers and dispatchers) connected by power transport lines. A node u may be supplied with an amount s(u) >= 0 of power, may produce an amount 0 <= p(u) <= p max(u) of power, may consume an amount 0 <= c(u) <= min(s(u),c max(u)) of power, and may deliver an amount d(u)=s(u)+p(u)-c(u) of power. The following restrictions apply: c(u)=0 for any power station, p(u)=0 for any consumer, and p(u)=c(u)=0 for any dispatcher. There is at most one power transport line (u,v) from a node u to a node v in the net; it transports an amount 0 <= l(u,v) <= l max(u,v) of power delivered by u to v. Let Con=Σ uc(u) be the power consumed in the net. The problem is to compute the maximum value of Con. 

An example is in figure 1. The label x/y of power station u shows that p(u)=x and p max(u)=y. The label x/y of consumer u shows that c(u)=x and c max(u)=y. The label x/y of power transport line (u,v) shows that l(u,v)=x and l max(u,v)=y. The power consumed is Con=6. Notice that there are other possible states of the network but the value of Con cannot exceed 6. 

Input

There are several data sets in the input. Each data set encodes a power network. It starts with four integers: 0 <= n <= 100 (nodes), 0 <= np <= n (power stations), 0 <= nc <= n (consumers), and 0 <= m <= n^2 (power transport lines). Follow m data triplets (u,v)z, where u and v are node identifiers (starting from 0) and 0 <= z <= 1000 is the value of l max(u,v). Follow np doublets (u)z, where u is the identifier of a power station and 0 <= z <= 10000 is the value of p max(u). The data set ends with nc doublets (u)z, where u is the identifier of a consumer and 0 <= z <= 10000 is the value of c max(u). All input numbers are integers. Except the (u,v)z triplets and the (u)z doublets, which do not contain white spaces, white spaces can occur freely in input. Input data terminate with an end of file and are correct.

Output

For each data set from the input, the program prints on the standard output the maximum amount of power that can be consumed in the corresponding network. Each result has an integral value and is printed from the beginning of a separate line.

Sample Input

2 1 1 2 (0,1)20 (1,0)10 (0)15 (1)20
7 2 3 13 (0,0)1 (0,1)2 (0,2)5 (1,0)1 (1,2)8 (2,3)1 (2,4)7
         (3,5)2 (3,6)5 (4,2)7 (4,3)5 (4,5)1 (6,0)5
         (0)5 (1)2 (3)2 (4)1 (5)4

Sample Output

15
6

Hint

The sample input contains two data sets. The first data set encodes a network with 2 nodes, power station 0 with pmax(0)=15 and consumer 1 with cmax(1)=20, and 2 power transport lines with lmax(0,1)=20 and lmax(1,0)=10. The maximum value of Con is 15. The second data set encodes the network from figure 1.

题意难读。
典型的多源多汇网络流,需要添加一个总源点和一个总汇点,然后就是一般的最大网络流了。
经典但效率较慢的Edmonds-Karp算法也能过。

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <stack>
#include <bitset>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <algorithm>
#define FOP freopen("data.txt","r",stdin)
#define inf 0x3f3f3f3f
#define maxn 110
#define mod 1000000007
#define PI acos(-1.0)
#define LL long long
using namespace std;

int n;
int ma[maxn][maxn];
int pre[maxn]; //记录前驱
int flow[maxn]; //记录从源点到当前节点实际还剩多少流量可用
int start, end;

int bfs()
{
    memset(pre, -1, sizeof(pre));
    queue<int> Q;
    Q.push(start), flow[start] = inf, pre[start] = 0;
    while(!Q.empty())
    {
        int t = Q.front();
        Q.pop();
        if(t == end) break;

        for(int i = 0; i <= n+1; i++)
        {
            if(i != start && pre[i] == -1 && ma[t][i])
            {
                flow[i] = flow[t] < ma[t][i] ? flow[t] : ma[t][i];
                pre[i] = t;
                Q.push(i);
            }
        }
    }
    if(pre[end] == -1) return -1;
    else return flow[end];
}

int Edmonds_Karp()
{
    int res = 0;
    int step, p, now;
    while((step = bfs()) != -1)
    {
        res += step;
        now = end;
        while(now != start)
        {
            p = pre[now];
            ma[now][p] += step;
            ma[p][now] -= step;
            now = p;
        }
    }
    return res;
}

int main()
{
    int np, nc, m;
    while(~scanf("%d%d%d%d", &n, &np, &nc, &m))
    {
        start = 0, end = n+1;
        memset(ma, 0, sizeof(ma));
        int a, b, c;
        while(m--)
        {
            while(getchar() != '(');
            scanf("%d,%d)%d", &a, &b, &c);
            ma[a+1][b+1] = c;
        }
        while(np--)
        {
            while(getchar() != '(');
            scanf("%d)%d", &b, &c);
            ma[start][b+1] = c;
        }
        while(nc--)
        {
            while(getchar() != '(');
            scanf("%d)%d", &a, &c);
            ma[a+1][end] = c;
        }
        printf("%d\n", Edmonds_Karp());
    }
    return 0;
}

积累一下Dinic算法模板(两种)

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <stack>
#include <bitset>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <algorithm>
#define FOP freopen("data.txt","r",stdin)
#define inf 0x3f3f3f3f
#define maxn 110
#define mod 1000000007
#define PI acos(-1.0)
#define LL long long
using namespace std;

int n,m,s,t;//输入的顶点数,输入的边数,源点,汇点
struct Edge
{
    int from, to, cap, flow;
};
vector<Edge> edges;//边集
vector<int> G[maxn];//顶点集
int d[maxn]; //从起点到i的距离
int cur[maxn]; //当前弧
bool vis[maxn];//bfs标记数组

void addedge(int u,int v,int c)
{
    Edge edge;
    edge.from = u, edge.to = v, edge.cap = c, edge.flow = 0;
    edges.push_back(edge);
    edge.from = v, edge.to = u, edge.cap = 0, edge.flow = 0;
    edges.push_back(edge);
    //edges.push_back((Edge){u,v,c,0});
    //edges.push_back((Edge){v,u,0,0});
    int x = edges.size();
    G[u].push_back(x-2);
    G[v].push_back(x-1);
}

bool BFS() //构建层次网络
{
    memset(vis,0,sizeof(vis));
    queue<int> Q;
    Q.push(s), d[s] = 0, vis[s] = 1;

    while(!Q.empty())
    {
        int x=Q.front();
        Q.pop();
        for(int i = 0; i < G[x].size(); i++)
        {
            Edge& e = edges[G[x][i]];
            if(!vis[e.to] && e.cap > e.flow) //只考虑残量网络中的弧
            {
                vis[e.to]=1;
                d[e.to]=d[x]+1;
                Q.push(e.to);
            }
        }
    }
    return vis[t];
}
int DFS(int x,int a) //寻找增广路
{
    if(x == t || a == 0) return a;
    int flow = 0, f;
    for(int& i = cur[x]; i < G[x].size(); i++) //从上次考虑的弧
    {
        Edge& e = edges[G[x][i]];
        if(d[x] + 1 == d[e.to] && (f = DFS(e.to, min(a,e.cap-e.flow))) >0)
        {
            e.flow += f;
            edges[G[x][i]^1].flow -= f;
            flow += f;
            a -= f;
            if(a == 0) break;
        }
    }
    return flow;
}
int Dinic()
{
    int flow = 0;
    while(BFS())
    {
        memset(cur, 0, sizeof(cur));
        flow += DFS(s, inf);
    }
    return flow;
}
int main()
{
    int np, nc;
    while(~scanf("%d%d%d%d", &n, &np, &nc, &m))
    {
        for(int i = 0; i <= n+1; i++) G[i].clear();
        s = 0, t = n+1;
        int a, b, c;
        while(m--)
        {
            while(getchar() != '(');
            scanf("%d,%d)%d", &a, &b, &c);
            addedge(a+1, b+1, c);
        }
        while(np--)
        {
            while(getchar() != '(');
            scanf("%d)%d", &b, &c);
            addedge(s, b+1, c);
        }
        while(nc--)
        {
            while(getchar() != '(');
            scanf("%d)%d", &a, &c);
            addedge(a+1, t, c);
        }
        printf("%d\n", Dinic());
    }
    return 0;
}

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <stack>
#include <bitset>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <algorithm>
#define FOP freopen("data.txt","r",stdin)
#define inf 0x3f3f3f3f
#define maxn 110
#define mod 1000000007
#define PI acos(-1.0)
#define LL long long
using namespace std;

struct Edge
{
    int a, b, c;
    int next;
}edge[21010];

int head[maxn];
int dist[maxn]; // 从起点到i的距离
int cur[maxn]; //当前弧下标
int vis[maxn]; //bfs使用
int cot;

int n, m, start, end;

void addEdge(int a, int b, int c)
{
    edge[cot].a = a, edge[cot].b = b, edge[cot].c = c, edge[cot].next = head[a], head[a] = cot++;
    edge[cot].a = b, edge[cot].b = a, edge[cot].c = 0, edge[cot].next = head[a=b], head[b] = cot++;//添加一条反向弧,容量为0
}

bool bfs()
{
    memset(vis, false, sizeof(vis));
    queue<int> Q;
    Q.push(start), dist[start] = 0, vis[start] = true;
    while(!Q.empty())
    {
        int a = Q.front(); Q.pop();
        for(int i = head[a]; i != -1; i = edge[i].next)
        {
            int b = edge[i].b, c = edge[i].c;
            if(!vis[b] && c)
            {
                vis[b] = true, dist[b] = dist[a] + 1, Q.push(b);
            }
        }
    }
    return vis[end];
}

int dfs(int a, int mif)
{
    if(a == end || mif == 0) return mif;
    int res = 0;
    for(int i = head[a]; i != -1; i = edge[i].next)
    {
        int b = edge[i].b, c = edge[i].c;
        if(dist[a] + 1 == dist[b] && c)
        {
            c = dfs(b, min(mif - res, c));
            edge[i].c -= c;
            edge[i^1].c += c;
            res += c;
            if(res == mif) return res;
        }
    }
    dist[a] = -1; //优化重要一步
    return res;
}

int Dinic()
{
    int ans = 0;
    while(bfs()) ans += dfs(start, inf);
    return ans;
}

int main()
{
    int np, nc;
    while(~scanf("%d%d%d%d", &n, &np, &nc, &m))
    {
        cot = 0;
        memset(head, -1, sizeof(head));

        start = 0, end = n+1;
        int a, b, c;
        while(m--)
        {
            while(getchar() != '(');
            scanf("%d,%d)%d", &a, &b, &c);
            addEdge(a+1, b+1, c);
        }
        while(np--)
        {
            while(getchar() != '(');
            scanf("%d)%d", &b, &c);
            addEdge(start, b+1, c);
        }
        while(nc--)
        {
            while(getchar() != '(');
            scanf("%d)%d", &a, &c);
            addEdge(a+1, end, c);
        }
        printf("%d\n", Dinic());
    }
    return 0;
}

ISAP模板

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <stack>
#include <bitset>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <algorithm>
#define FOP freopen("data.txt","r",stdin)
#define inf 0x3f3f3f3f
#define maxn 110
#define mod 1000000007
#define PI acos(-1.0)
#define LL long long
using namespace std;

struct Edge
{
    int from, to, cap, flow;
};
vector<Edge> edges;//边集
vector<int> G[maxn];//顶点集

int num_nodes, num_edges;
int s, t; //源点、汇点

int p[maxn]; //可增广路上的上一条弧的编号
int num[maxn]; //和t的最短距离等于i的节点数量
int cur[maxn]; //当前弧下标
int d[maxn]; //残量网络中节点i到汇点t的最短距离
bool vis[maxn]; //bfs使用

void addedge(int u,int v,int c)
{
    Edge edge;
    edge.from = u, edge.to = v, edge.cap = c, edge.flow = 0;
    edges.push_back(edge);
    edge.from = v, edge.to = u, edge.cap = 0, edge.flow = 0;
    edges.push_back(edge);
    //edges.push_back((Edge){u,v,c,0});
    //edges.push_back((Edge){v,u,0,0});
    num_edges = edges.size();
    G[u].push_back(num_edges-2);
    G[v].push_back(num_edges-1);
}

//预处理,反向bfs构造d数组
bool bfs()
{
    memset(vis, false, sizeof(vis));
    queue<int> Q;
    Q.push(t);
    vis[t] = true;
    d[t] = 0;
    while(!Q.empty())
    {
        int x = Q.front();
        Q.pop();
        for(int i = 0; i < G[x].size(); i++)
        {
            Edge& e = edges[G[x][i]^1];
            if(!vis[e.from] && e.cap > e.flow) //只考虑残量网络中的弧
            {
                vis[e.from] = true;
                d[e.from] = d[x] + 1;
                Q.push(e.from);
            }
        }
    }
    return vis[s];
}

int augment()
{
    int x = t, a = inf;
    //从汇点到源点通过p追踪增广路径,df为一路上最小的残量
    while(x != s)
    {
        Edge &e = edges[p[x]];
        a = min(a, e.cap - e.flow);
        x = edges[p[x]].from;
    }
    x = t;
    //从汇点到源点更新流量
    while(x != s)
    {
        edges[p[x]].flow += a;
        edges[p[x]^1].flow -= a;
        x = edges[p[x]].from;
    }
    return a;
}

int ISAP()
{
    int flow = 0;
    bfs();
    memset(num,0, sizeof(num));
    for(int i = 0; i < num_nodes; i++) num[d[i]]++;
    int x = s;
    memset(cur, 0, sizeof(cur));
    while(d[s] < num_nodes)
    {
        if(x == t)
        {
            flow += augment();
            x = s;
        }
        bool advanced = false;
        for(int i = cur[x]; i < G[x].size(); i++)
        {
            Edge& e = edges[G[x][i]];
            if(e.cap > e.flow && d[x] == d[e.to] + 1)
            {
                advanced = true;
                p[e.to] = G[x][i];
                cur[x] = i;
                x = e.to;
                break;
            }
        }
        if(!advanced) // retreat
        {
            int m = num_nodes - 1;
            for(int i = 0; i < G[x].size(); i++)
            {
                Edge& e = edges[G[x][i]];
                if(e.cap > e.flow) m = min(m, d[e.to]);
            }
            if(--num[d[x]] == 0) break; //gap优化
            num[d[x] = m+1]++;
            cur[x] = 0;
            if(x != s) x = edges[p[x]].from;
        }
    }
    return flow;
}

int n, m;//输入的顶点数,输入的边数

int main()
{
    int np, nc;
    while(~scanf("%d%d%d%d", &n, &np, &nc, &m))
    {
        edges.clear();
        for(int i = 0; i <= n+1; i++) G[i].clear();
        s = 0, t = n+1;
        int a, b, c;
        while(m--)
        {
            while(getchar() != '(');
            scanf("%d,%d)%d", &a, &b, &c);
            addedge(a+1, b+1, c);
        }
        while(np--)
        {
            while(getchar() != '(');
            scanf("%d)%d", &b, &c);
            addedge(s, b+1, c);
        }
        while(nc--)
        {
            while(getchar() != '(');
            scanf("%d)%d", &a, &c);
            addedge(a+1, t, c);
        }

        num_nodes = n+2;

        printf("%d\n", ISAP());
    }
    return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值