uva 10330 Power Transmission

來源:互聯網
上載者:User

原題:
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 will 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 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’ is 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 two positive integers B and D. B is the number of regulators which are the entry point of the 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 are special and have infinite capacity. Next line will contain B +D integers each of which is an index of regulator. The first B integers are the index of regulators connected with Barisal. Regulators connected with Barisal are not connected with Dhaka. Input is terminated by EOF.
Output
For each test case show the maximum amount of power which can be 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

中文:
(來自lucky 貓)
DESA正在進行一項電力傳輸的計畫。在 Barisal 這個地方建立了一座發電廠,它的主要目的是提供電力給 Dhaka這座城市。由於 Dhaka 的人口數相當多,DESA 希望盡可能透過網路傳輸最大的電力給它。但是電力在傳輸時會因電阻而損失,所以他們想要使用變電裝置來達到不損失電力的目標。

每個變電裝置有不同的容量。這指的是假如一個變電裝置得到100單位的電,而它的容量只有80個單位,那麼就會損失20單位的電。並且 連接變電裝置之間的電線也是有一定的容量的,例如容量20單位的電線無法傳輸超過20單位的電。DESA 想要知道在沒有電力損失的情況下,最多可以傳輸的電力是多少。這就是你的任務。
Input

輸入含有多組測試資料。

每組測試資料的第一列,有 1 個正整數 N(1 <= N <= 100)代表變電裝置的數目(編號 1 到 N)。下一列有 N 個正整數代表這N個變電裝置的容量。接下來的一列有一個正整數 M,代表各變電裝置間連接電線的數目。再接下來的 M 列每列有3個正整數(i j C)。i, j 為變電裝置的編號,C 為連接 i, j 的電線的容量。電力能夠從 i 變電裝置傳輸到 j 變電裝置。再接下來的一列有2個整數B,D。B代表直接連接發電廠的變電裝置的數目,D代表直接連接到Dhaka的變電裝置的數目。這些連接的電線是特別的,他們的容量是無限大(上圖以藍粗線表示)。下一列有B+D個變電裝置的編號,前B個代表直接連接Barisal發電廠的變電裝置編號,剩下的D個為直接連接到Dhaka的變電裝置的編號。連接Barisal的變電裝置不會連接到Dhaka。

請參考Sample Input,上圖即為Sample Input第一組測試資料。

Output

對每一組測試資料輸出一列,最多可以從Barisal傳送多少電力到Dhaka。

#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];  //注意        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;}

解答:
比較裸的最大流問題,由於每個節點也有流量限制,所以需要把有流量限制的節點拆分成兩個節點,讓這兩個節點之間建立一條邊,這條邊作為流量限制即可。如圖

使用的Ford-Fulkerson演算法
也可以使用EK演算法或者更進階的Dinic演算法或者ISAP演算法,效果會更好

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.