UVA 10330 Power Transmission

Source: Internet
Author: User
Tags ini

Original question:
DESA is taking a new project to transfer power. Power is generated by the newly established plant in Barisal. The main aim of this project is to transfer Power in Dhaka. As Dhaka is a megacity with almost 10 million people DESA wants to transfer maximum amount of power through the network. But as always occurs in case of power transmission it is tough to resist loss. So they want to use some regulators whose main aim are to divert power through several outlets without any loss. Each such regulator has different capacity. It means if a regulator gets 100 unit power and it’s capacity is 80 unit then remaining 20 unit power will be lost. Moreover each unidirectional link( Connectors among regulators) has a certain capacity. A link with capacity 20 unit cannot transfer
power more than 20 unit. Each regulator can distribute the input power among the outgoing links so that no link capacity is overflown. DESA wants to know the maximum amount of power which can be transmitted throughout the network so that no power loss occurs. That is the job you have to do.


(Do not try to mix the above description with the real power transmission.)
Input
The input would start with a postive integer N (1≤n≤100) indicates the number of regulators. The next few lines contain n positive integers indicating the capacity of each regulator from 1 to N. The next line contains another positive an integer M which is the number of links available among the regulators. Following M lines contain 3 positive integers (i j C) each. ' I ' and ' J ' are the Regulator Index (1≤i,j≤n) and C is the capacity of the link. Power can transfer from I ' th regulator to j ' th
Regulator. The next line contains another, positive integers b and D. B is the number of regulators which being the entry point of T He network. Power generated in Barisal must enter in the network through these entry points. Simmilarly D is the number of regulators connected to Dhaka. These links is special and has infinite capacity. Next line would contain B +d integers each of the which are an index of regulator. The first B integers is the index of regulators connected with Barisal. Regulators connected with Barisal is not connected with Dhaka. Input is terminated by EOF.
Output
For each test case show the maximum amount of power which can is transferred to Dhaka from Barisal.
Use a separate line for each test case.
Sample Input
4
10 20 30 40
6
1 2 5
1 3 10
1 4 13
2 3 5
2 4 7
3 4 20
3 1
1 2 3 4
2
50 100
1
1 2 100
1 1
1 2
Sample Output
37
50



English:
(From Lucky Cat)
DESA is in the line of a power-passing design. A new electric power factory was built in Barisal, the main purpose of which was to provide electricity to the city of Dhaka. Because of the Dhaka's population, DESA hopes to pass the biggest power to the network as much as possible. But the power is lost due to electrical resistance during the transport, so they want to use the change device to achieve the goal of not losing power.



Each change device has a different capacity. This means that if a change device gets 100 units of electricity, and its capacity is only 80 units, then it loses 20 units of electricity. And there is a certain capacity for the connection between the electrical devices, such as a 20-unit power line that does not transmit over 20 units of electricity. DESA wants to know how much power can be transferred in the case of no power loss. This is your task.
Input



Input contains multiple test materials.



In the first column of each test, there are 1 positive integers n (1 <= n <= 100) representing the number of devices (1 to N). The next column has n positive integers representing the capacity of these n-change devices. The next column has a positive integer M, which represents the number of connection wires for each of the change devices. The next M-column has 3 positive integers per column (i J C). I, J is the number of the device for the change, and C is the capacity of the connection I, J. Power can be changed from I to the electrical device to the J-Change device. The next column has 2 integer b,d. b represents the number of the device directly connected to the device, and D represents a direct connection to the Dhaka's changing device. These connected wires are special, their capacity is unlimited (the image is indicated by a blue line). The next column has the b+d of the change device, and the first B represents a direct connection to the Barisal device's change device, and the remaining d is directly connected to the Dhaka's changing device. The change device connected to the Barisal does not connect to the Dhaka.



Please refer to sample input, which is the first test of sample input.



Output



For each group of tests, the maximum amount of power to Dhaka can be sent from Barisal.


#include <bits / stdc ++. h>
using namespace std;
const int N = 210;
const int INF = 0x3f3f3f3f;
const int _max = 900000;
int n, m, x, y;
struct Node
{
    int to;
    int cap;
    int rev;
};

vector <Node> v [N];
bool used [N];

void add_Node (int from, int to, int cap)
{
    v [from] .push_back ((Node) {to, cap, v [to] .size ()});
    v [to] .push_back ((Node) {from, 0, v [from] .size ()-1});
}

int dfs (int s, int t, int f)
{
    if (s == t)
        return f;
    used [s] = true;
    for (int i = 0; i <v [s] .size (); i ++)
    {
        Node & tmp = v [s] [i]; // note
        if (used [tmp.to] == false && tmp.cap> 0)
        {
            int d = dfs (tmp.to, t, min (f, tmp.cap));
            if (d> 0)
            {
                tmp.cap- = d;
                v [tmp.to] [tmp.rev] .cap + = d;
                return d;
            }
        }
    }
    return 0;
}

int max_flow (int s, int t)
{
    int flow = 0;
    for (;;)
    {
        memset (used, false, sizeof (used));
        int f = dfs (s, t, INF);
        if (f == 0)
            return flow;
        flow + = f;
    }
}
void ini ()
{
    memset (used, false, sizeof (used));
    for (int i = 0; i <= 205; i ++)
        v [i] .clear ();
}
int main ()
{
    ios :: sync_with_stdio (false);
    while (cin >> n)
    {
        ini ();
        for (int i = 1; i <= n; i ++)
        {
            int c;
            cin >> c;
            add_Node (i * 2-1, i * 2, c);
        }
        cin >> m;
        for (int i = 1; i <= m; i ++)
        {
            int f, t, c;
            cin >> f >> t >> c;
            f = f * 2;
            t = t * 2-1;
            add_Node (f, t, c);
        }
        cin >> x >> y;
        for (int i = 1; i <= x; i ++)
        {
            int t;
            cin >> t;
            add_Node (0, t * 2-1, _max);
        }
        for (int i = 1; i <= y; i ++)
        {
            int f;
            cin >> f;
            add_Node (f * 2, n * 2 + 1, _max);
        }
        int ans = max_flow (0, n * 2 + 1);
        cout << ans << endl;
    }
    return 0;
}


Answer:
Compare bare maximum flow problem, because each node also has the traffic limit, therefore need to divide the node which has the traffic limit to divide into two nodes, let these two nodes establish an edge, this edge as traffic limit can.





The Ford-fulkerson algorithm used
You can also use the EK algorithm or a more advanced dinic or ISAP algorithm for better results


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.