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

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

输入大意:

分析:懂了题意就是一个模板。。。还不用拆点什么的,不过输入的时候不太明白为什么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, 0, sizeof(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, 0, sizeof(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;

}

时间: 2024-10-13 22:44:48

F - Power Network - poj 1459(简单最大流)的相关文章

Power Network (poj 1459 网络流)

Language: Default Power Network Time Limit: 2000MS   Memory Limit: 32768K Total Submissions: 23407   Accepted: 12267 Description A power network consists of nodes (power stations, consumers and dispatchers) connected by power transport lines. A node

POJ 1459 Power Network(网络流 最大流 多起点,多汇点)

Power Network Time Limit: 2000MS   Memory Limit: 32768K Total Submissions: 22987   Accepted: 12039 Description A power network consists of nodes (power stations, consumers and dispatchers) connected by power transport lines. A node u may be supplied

poj 1459 Power Network, 最大流,多源多汇

点击打开链接 多源多汇最大流,虚拟一个源点s'和一个汇点t',原来的源点.汇点向它们连边. #include<cstdiO> #include<cstring> #include<iostream> #include<algorithm> #include<queue> #include<vector> using namespace std; const int maxn = 500 + 5; const int INF = 100

POJ 1459 &amp; ZOJ 1734 Power Network (网络最大流)

http://poj.org/problem?id=1459 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1734 Power Network Time Limit: 2000MS   Memory Limit: 32768K Total Submissions: 22674   Accepted: 11880 Description A power network consists of nodes (power s

POJ 1459 Power Network(ISAP 裸最大流)

题目链接:http://poj.org/problem?id=1459 注意输入格式就行,还是ISAP #include <iostream> #include <cstdlib> #include <cstdio> #include <cstring> #include <queue> #include <algorithm> const int N = 210; const int maxn = 300; const int ma

POJ 1459 Power Network (网络流最大流基础 多源点多汇点 Edmonds_Karp算法)

Power Network Time Limit: 2000MS   Memory Limit: 32768K Total Submissions: 24056   Accepted: 12564 Description A power network consists of nodes (power stations, consumers and dispatchers) connected by power transport lines. A node u may be supplied

POJ 1459 Power Network 最大流

建模不难,就读入有点麻烦,无脑拍完dinic 1A happy- #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #include <climits> #include <string> #include <iostream> #include <map> #include <cstdlib> #i

POJ 1459 Power Network 经典网络流构图问题 最大流,EK算法

题目链接:POJ 1459 Power Network Power Network Time Limit: 2000MS   Memory Limit: 32768K Total Submissions: 23347   Accepted: 12231 Description A power network consists of nodes (power stations, consumers and dispatchers) connected by power transport line

POJ 1459 Power Network(多源点/汇点最大流问题)

题目链接:http://poj.org/problem?id=1459 题目: 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