uva 10330 Power Transmission

原题:
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算法,效果会更好

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值