poj1459 Power Network(Dinic多源多汇)

本文介绍了解决POJ 1459问题的一种方法,通过构建超级源点和超级汇点将多源多汇的最大流问题转化为单源单汇问题,并给出了详细的C++实现代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


http://poj.org/problem?id=1459

题意:给你n个节点,m个路的关系表示从s到t可以传送w的电量,np个发点站,nc个耗电站。求最大耗电量。


ps:看的别人的题意。。看不懂


思路:多源多汇其实不用一个一个来,建立一个超级源点连接所有发电站,路的容量就是该发电站可以发的电;建立一个超级汇点连接所有耗电站,路的容量就是该耗电站可以耗的电。这样就转化为了单源单汇,套模板即可。0为源点,往大平移一个单位,还有记得输入时候把空格吃掉。516ms,为毛感觉我的模板好慢的说。。


#include <stdio.h>
#include <algorithm>
#include <stdlib.h>
#include <string.h>
#include <iostream>
#include <queue>
#include <stack>
#include <ctype.h>

using namespace std;

typedef long long LL;

const int N = 1000005;
const int INF = 0x3f3f3f3f;

int head[N], dis[N], cur[N];
bool vis[N];
int n, m, cnt, np, nc;

struct Edge
{
    int to, cap, flow, next;
}edge[N];

void add(int u, int v, int w)
{
    edge[cnt] = (struct Edge){v, w, 0, head[u]};
    head[u] = cnt++;
    edge[cnt] = (struct Edge){u, 0, 0, head[v]};
    head[v] = cnt++;
}

bool bfs(int start, int endd)
{
    memset(dis, -1, sizeof(dis));
    memset(vis, false, sizeof(vis));
    queue<int>que;
    dis[start] = 0;
    vis[start] = true;
    que.push(start);
    while(!que.empty())
    {
        int u = que.front();
        que.pop();
        for(int i = head[u]; i != -1; i = edge[i].next)
        {
            Edge E = edge[i];
            if(!vis[E.to] && E.flow<E.cap)
            {
                dis[E.to] = dis[u]+1;
                vis[E.to] = true;
                if(E.to == endd) return true;
                que.push(E.to);
            }
        }
    }
    return false;
}


int dfs(int x, int res, int endd)
{
	if(x == endd || res == 0) return res;
	int flow = 0, f;
	for(int& i = cur[x]; i != -1; i = edge[i].next)
	{
		Edge E = edge[i];
		if(dis[E.to] == dis[x]+1)
		{
		    f = dfs(E.to, min(res, E.cap-E.flow), endd);
            if(f>0)
            {
                edge[i].flow += f;
                edge[i^1].flow -= f;
                flow += f;
                res -= f;
                if(res == 0) break;
            }
		}
	}
	return flow;
}


int max_flow(int start, int endd)
{
    int flow = 0;
    while(bfs(start, endd))
    {
        memcpy(cur, head, sizeof(head));
        flow += dfs(start, INF, endd);
    }
    return flow;
}

void init()
{
    cnt = 0;
    memset(head, -1, sizeof(head));
}

void getmap()
{
    int s, t, w;
    for(int i = 1; i <= m; i++)
    {
        scanf(" (%d,%d)%d", &s, &t, &w);
        add(s+1, t+1, w);
    }
    for(int i = 1; i <= np; i++)
    {
        scanf(" (%d)%d", &t, &w);
        add(0, t+1, w);
    }
    for(int i = 1; i <= nc; i++)
    {
        scanf(" (%d)%d", &s, &w);
        add(s+1, n+1, w);
    }
}

int main()
{
  //  freopen("in.txt", "r", stdin);
    while(~scanf("%d%d%d%d", &n, &np, &nc, &m))
    {
        init();
        getmap();
        int ans = max_flow(0, n+1);
        printf("%d\n", ans);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值