HDU 3491 Thieves(网络流+拆点)

Thieves

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)
Total Submission(s): 1686    Accepted Submission(s): 784


Problem Description
In the kingdom of Henryy, there are N (2 <= N <= 100) cities, with M (M <= 10000) two-direct ways connecting them.
A group of thieves from abroad plan to steal the metropolitan museum in city H (It has not been demolished). However, the brave, brilliant, bright police in the kingdom have known this plan long before, and they also plan to catch the thieves. The thieves are in the city S at present. The police try to catch them on their way from S to H. Although the thieves might travel this way by more than one group, our excellent police has already gather the statistics that the number of the people needed in city I (1<=I<=N) to arrest the thieves.
The police do not want to encounter the thieves in either city S or city H.
The police finish the task with the minimum number of people. Do you know the exact number?
 

Input
The first line contains an integer T (T <= 10), indicating the number of the test cases.
The first line of each test case has four integers: N, the number of the cities; M, the number of the roads; S (1<=S<=N), the label of city S; H (1<=T<=N, S≠H), the label of city H.
The second line contains N integers, indicating the number of people needed in each city. The sum of these N integers is less than 10000.
Then M lines followed, each containing two integers x and y, indicating that there is a two-direct roads between city x and y. Notices that no road between city S and H.
A blank line is followed after each test case.
 

Output
For each test case, output a single integer in a separate line, indicating the minimum number of people the police used.
 

Sample Input
 
 
1 5 5 1 5 1 6 6 11 1 1 2 1 3 2 4 3 4 4 5
 

Sample Output
 
 
11

题解:
把一个点拆成两个,连一条边,权值为该点的人数,
原有的边权值为正无穷。
代码:

#include<bits/stdc++.h>
using namespace std;
const int maxn=205;
const int inf=1e9;
struct node
{
    int to,cap,rev;
};
vector<node>G[maxn];
int a[maxn],level[maxn],iter[maxn];
void add(int from,int to,int cap)
{
    G[from].push_back((node){to,cap,G[to].size()});
    G[to].push_back((node){from,0,G[from].size()-1});
}
void bfs(int s)
{
    memset(level,-1,sizeof(level));
    queue<int>P;P.push(s);level[s]=0;
    while(!P.empty())
    {
        int v=P.front();P.pop();
        for(int i=0;i<G[v].size();i++)
        {
            node e=G[v][i];
            if(e.cap>0&&level[e.to]<0)
            {
                level[e.to]=level[v]+1;
                P.push(e.to);
            }
        }
    }
}
int dfs(int v,int t,int f)
{
    if(v==t)return f;
    for(int &i=iter[v];i<G[v].size();i++)
    {
        node &e=G[v][i];
        if(e.cap>0&&level[e.to]>level[v])
        {
            int d=dfs(e.to,t,min(f,e.cap));
            if(d>0)
            {
                e.cap-=d;
                G[e.to][e.rev].cap+=d;
                return d;
            }
        }
    }
    return 0;
}
int max_flow(int s,int t)
{
    int flow=0;
    while(1)
    {
        bfs(s);
        if(level[t]<0)return flow;
        memset(iter,0,sizeof(iter));
        int f;
        while((f=dfs(s,t,inf))>0)flow+=f;
    }
}
int main()
{
    int T;scanf("%d",&T);
    while(T--)
    {
        for(int i=0;i<maxn;i++)G[i].clear();
        int n,m,s,h;
        scanf("%d%d%d%d",&n,&m,&s,&h);
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&a[i]);
            add(i,i+n,a[i]);
            add(i+n,i,a[i]);
        }
        for(int i=1;i<=m;i++)
        {
            int x,y;scanf("%d%d",&x,&y);
            add(x+n,y,inf);
            add(y+n,x,inf);
        }
        printf("%d\n",max_flow(s+n,h));
    }
    return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值