Thieves HDU - 3491(最小点割集)

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<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<queue>
#define INF 0x3f3f3f3f
using namespace std;
const int maxn=205;
const int maxx=40205;
int edge;
int to[maxx],flow[maxx],nex[maxx];
int head[maxn];

void addEdge(int v,int u,int cap)
{
    to[edge]=u,flow[edge]=cap,nex[edge]=head[v],head[v]=edge++;
    to[edge]=v,flow[edge]=0,nex[edge]=head[u],head[u]=edge++;
}
int vis[maxn];
int pre[maxn];
bool bfs(int s,int e)
{
    queue<int> que;
    pre[s]=-1;
    memset(vis,-1,sizeof(vis));
    que.push(s);
    vis[s]=0;
    while(!que.empty())
    {
        int u=que.front();
        que.pop();
        for(int i=head[u];~i;i=nex[i])
        {
            int v=to[i];
            if(vis[v]==-1&&flow[i])
            {
                vis[v]=vis[u]+1;
                if(v==e)
                    return true;
                que.push(v);
            }

        }
    }
    return false;
}
int dfs(int s,int t,int f)
{
    if(s==t||!f)
        return f;
    int r=0;
    for(int i=head[s];~i;i=nex[i])
    {
        int v=to[i];
        if(vis[v]==vis[s]+1&&flow[i])
        {
            int d=dfs(v,t,min(f,flow[i]));
            if(d>0)
            {
                flow[i]-=d;
                flow[i^1]+=d;
                r+=d;
                f-=d;
                if(!f)
                    break;
            }
        }
    }
    if(!r)
        vis[s]=INF;
    return r;
}
int maxFlow(int s ,int e)//然后直接调用这个即可
{
    int ans=0;
    while(bfs(s,e))
        ans+=dfs(s,e,INF);
    return ans;
}

void init()//记得每次使用前初始化
{
    memset(head,-1,sizeof(head));
    edge=0;
}
int main()
{
    int t;
    int x,y;
    int n,m;
    int s,h;
    scanf("%d",&t);
    while(t--)
    {
        init();
        scanf("%d%d%d%d",&n,&m,&s,&h);
        addEdge(s,n+s,INF);
        addEdge(h,n+h,INF);
        addEdge(0,s,INF);
        addEdge(n+h,n+n+1,INF);
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&x);
            if(i==s||i==h)
                continue;
            addEdge(i,n+i,x);
        }
        while(m--)
        {
            scanf("%d%d",&x,&y);
            addEdge(x+n,y,INF);
            addEdge(n+y,x,INF);
        }
        int ans=maxFlow(0,n+n+1);
        printf("%d\n",ans);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值