1. 程式人生 > >poj 1459 Power Network 【圖論-網路流-最大流-EK】

poj 1459 Power Network 【圖論-網路流-最大流-EK】

                                Power Network
                Time Limit: 2000MS      Memory Limit: 32768K

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) <= pmax(u) of power, may consume an amount 0 <= c(u) <= min(s(u),cmax(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) <= lmax(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.

figure 1

An example is in figure 1. The label x/y of power station u shows that p(u)=x and pmax(u)=y. The label x/y of consumer u shows that c(u)=x and cmax(u)=y. The label x/y of power transport line (u,v) shows that l(u,v)=x and lmax(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 lmax(u,v). Follow np doublets (u)z, where u is the identifier of a power station and 0 <= z <= 10000 is the value of pmax(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 cmax(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.

題目大意:一個國家電網,總共有n個節點,其中有np個節點是發電站,一些是nc個節點是消耗站,其他的是中轉站,還有m條線路,用於連線各個節點,每條線路上規定有最大的電量流動。發電站只產生電量不消耗電量,消耗站只消耗不產能,中轉不消耗不產能,請問這個電路中最大的消耗量為多少?

解題思路:剛開始的時候被題中的兩個圖嚇到了,等看懂題意後,感覺很水
虛擬一個源點,讓所有的發電站與源點相連
虛擬一個匯點,讓所有的消耗站與匯點相連
根據輸入資料,把各個節點相連

此題坑點:如果使用scanf()進行讀取資料,讀取到左括號會報錯,應先把左括號以及之前的除數字外的字元,全部清除乾淨
如果使用cin 只需要挨個讀取即可

AC程式碼:

//EK AC

# include <iostream>
# include <cstdio>
# include <cstring>
# include <string>

using namespace std;

# define MAXN 505
# define INF 1e9 + 100

int map[MAXN][MAXN];
int que[MAXN];
int used[MAXN];
int pre[MAXN];

int min(int a, int b)
{
    return a > b ? b : a;
}

int Bfs(int s, int t)
{
    int head = 1;
    int tail = 1;
    memset(pre, -1, sizeof(pre));
    que[tail++] = s;
    pre[s] = -1;
    while (tail > head)
    {
        int u = que[head++];
        for (int i = 0; i <= t; i++)
        {
            if (pre[i] == -1 && map[u][i] > 0)
            {
                pre[i] = u;
                if (i == t)
                {
                    return 1;
                }
                que[tail++] = i;
            }
        }
    }
    return 0;
}

int Maxflow(int s, int t)
{
    int maxflow = 0;
    while (Bfs(s, t))
    {
        int i;
        int minflow = INF;
        for (i = t; i != s; i = pre[i])
        {
            minflow = min(minflow, map[pre[i]][i]);
        }
        for (i = t; i != s; i = pre[i])
        {
            map[pre[i]][i] -= minflow;
            map[i][pre[i]] += minflow;
        }
        maxflow += minflow;
    }
    return maxflow;
}

int main(void)
{
    int n, np, nc, m;
    while (~scanf("%d %d %d %d", &n, &np, &nc, &m))
    {
        memset(map, 0, sizeof(map));
        int s = 0;
        int t = n + 1;
        int i, j;
        int u, v, w;
        char ch;
        for (i = 1; i <= m; i++)
        {
            while ((ch = getchar()) != '(');//清除左括號以及之前的所有空格

            scanf("%d,%d)%d", &u, &v, &w);
            if (u == v) //這個加不加都無所謂
            {
                continue;
            }
            u++;
            v++;
            map[u][v] = w;
        }

        for (i = 1; i <= np; i++)
        {
            while ((ch = getchar()) != '(');

            scanf("%d)%d", &u, &w);
            u++;
            map[s][u] = w;
        }
        for (i = 1; i <= nc; i++)
        {
            while ((ch = getchar()) != '(');

            scanf("%d)%d", &u, &w);
            u++;
            map[u][t] = w;
        }
        printf("%d\n", Maxflow(s, t));
    }
    return 0;
}

超時演算法:但不知道為什麼Ford-Fulkerson超時
希望有大神能夠指點一下

//Ford-Fulkerson Time Limit 
//不知道為什麼超時
# include <iostream>
# include <cstdio>
# include <cstring>
# include <string>

using namespace std;

# define MAXN 505
# define INF 1e9 + 100

int map[MAXN][MAXN];
int used[MAXN];

int min(int a, int b)
{
    return a > b ? b : a;
}

int Dfs(int s, int t, int f)
{
    if (s == t)
    {
        return f;
    }
    for (int i = 0; i <= t; i++)
    {
        if (map[s][i] > 0 && !used[i])
        {
            used[i] = 1;
            int d = Dfs(i, t, min(f, map[s][i]));
            if (d > 0)
            {
                map[s][i] -= d;
                map[i][s] += d;
                return d;
            }
        }
    }
    return 0;
}

int Maxflow(int s, int t)
{
    int maxflow = 0;
    while (1)
    {
        memset(used, 0, sizeof(used));
        int f = Dfs(s, t, INF);
        if (!f)
        {
            return maxflow;
        }
        maxflow += f;
    }
}

int main(void)
{
    int n, np, nc, m;
    while (~scanf("%d %d %d %d", &n, &np, &nc, &m))
    {
        memset(map, 0, sizeof(map));
        int s = 0;
        int t = n + 1;
        int i, j;
        int u, v, w;
        char ch;
        for (i = 1; i <= m; i++)
        {
            while ((ch = getchar()) != '(');

            scanf("%d,%d)%d", &u, &v, &w);
            if (u == v) //去掉自環情況
            {
                continue;
            }
            u++;
            v++;
            map[u][v] = w;
        }

        for (i = 1; i <= np; i++)
        {
            while ((ch = getchar()) != '(');

            scanf("%d)%d", &u, &w);
            u++;
            map[s][u] = w;
        }
        for (i = 1; i <= nc; i++)
        {
            while ((ch = getchar()) != '(');

            scanf("%d)%d", &u, &w);
            u++;
            map[u][t] = w;
        }
        printf("%d\n", Maxflow(s, t));
    }
    return 0;
}