码迷,mamicode.com
首页 > Web开发 > 详细

F - Power Network - poj 1459(简单最大流)

时间:2015-08-09 22:17:15      阅读:119      评论:0      收藏:0      [点我收藏+]

标签:

题目大意:题目说了一大堆,其实都是废话......让人有些不知所云,其实就是给了一些电厂,和一些消费点,然后里面有一些路线什么的,求出消费点可以最多消费的电量是多少。
输入大意:
技术分享

分析:懂了题意就是一个模板。。。还不用拆点什么的,不过输入的时候不太明白为什么scanf("(%d,%d)%d")这种输入不好使了,只得用字符串直接读的。

 

下面是AC代码。


#include<stdio.h>
#include<string.h>
#include<queue>
#include<stack>
#include<algorithm>
#include<math.h>
using namespace std;

const int MAXN = 205;
const int oo = 1e9+7;

int G[MAXN][MAXN], layer[MAXN];

void In(char s[])
{
    scanf("%s", s);

    for(int i=0; s[i]; i++)
    {
        if(s[i]==, || s[i]==( || s[i]==))
            s[i] =  ;
    }
}

bool bfs(int start, int End)
{
    int used[MAXN] = {0};
    queue<int> Q;Q.push(start);

    memset(layer, 0sizeof(layer));
    used[start] = layer[start] = 1;

    while(Q.size())
    {
        int u = Q.front();Q.pop();

        if(u == End)return true;

        for(int i=0; i<=End; i++)
        {
            if(!used[i] && G[u][i])
            {
                used[i] = true;
                layer[i] = layer[u] + 1;
                Q.push(i);
            }
        }
    }

    return false;
}
int dfs(int u, int MaxFlow, int End)
{
    if(u == End)return MaxFlow;

    int uFlow = 0;

    for(int i=0; i<=End; i++)
    {
        if(G[u][i] && layer[u] == layer[i]-1)
        {
            int flow = min(MaxFlow-uFlow, G[u][i]);
            flow = dfs(i, flow, End);
            G[u][i] -= flow;
            G[i][u] += flow;
            uFlow += flow;

            if(uFlow == MaxFlow)
                break;
        }
    }

    return uFlow;
}
int dinic(int start, int End)
{
    int MaxFlow = 0;

    while(bfs(start, End) == true)
        MaxFlow += dfs(start, oo, End);

    return MaxFlow;
}

int main()
{
    int N, NP, NC, M;

    while(scanf("%d%d%d%d", &N, &NP, &NC, &M) != EOF)
    {
        int i, u, v, flow, start=N, End=start+1;
        char s[100];

        memset(G, 0sizeof(G));

        while(M--)
        {///线路
            In(s);
            sscanf(s, "%d%d%d", &u, &v, &flow);

            G[u][v] += flow;
        }

        for(i=1; i<=NP; i++)
        {///输入发电站,与源点相连
            In(s);
            sscanf(s, "%d%d", &u, &flow);
            G[start][u] = flow;
        }
        for(i=1; i<=NC; i++)
        {///消费点,与汇点相连
            In(s);
            sscanf(s, "%d%d", &u, &flow);
            G[u][End] = flow;
        }

        printf("%d\n", dinic(start, End));
    }

    return 0; 

}

 

F - Power Network - poj 1459(简单最大流)

标签:

原文地址:http://www.cnblogs.com/liuxin13/p/4716432.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!