HDU4289 Control(网络流,最大流,拆点,ISAP算法)

Problem Description

 You, the head of Department of Security, recently received a
top-secret information that a group of terrorists is planning to
transport some WMD 1 from one city (the source) to another one (the
destination). You know their date, source and destination, and they
are using the highway network.   The highway network consists of
bidirectional highways, connecting two distinct city. A vehicle can
only enter/exit the highway network at cities only.   You may locate
some SA (special agents) in some selected cities, so that when the
terrorists enter a city under observation (that is, SA is in this
city), they would be caught immediately.   It is possible to locate SA
in all cities, but since controlling a city with SA may cost your
department a certain amount of money, which might vary from city to
city, and your budget might not be able to bear the full cost of
controlling all cities, you must identify a set of cities, that:   *
all traffic of the terrorists must pass at least one city of the set.
  * sum of cost of controlling all cities in the set is minimal.   You
may assume that it is always possible to get from source of the
terrorists to their destination.
———————————————————— 1 Weapon of Mass Destruction

Input

  There are several test cases.   The first line of a single test case
contains two integer N and M ( 2 <= N <= 200; 1 <= M <= 20000), the
number of cities and the number of highways. Cities are numbered from
1 to N.   The second line contains two integer S,D ( 1 <= S,D <= N),
the number of the source and the number of the destination.   The
following N lines contains costs. Of these lines the ith one contains
exactly one integer, the cost of locating SA in the ith city to put it
under observation. You may assume that the cost is positive and not
exceeding 107.   The followingM lines tells you about highway network.
Each of these lines contains two integers A and B, indicating a
bidirectional highway between A and B.   Please process until EOF (End
Of File).

Output

 For each test case you should output exactly one line, containing one
integer, the sum of cost of your selected set.   See samples for
detailed information.

Sample Input

5 6
5 3
5
2
3
4
12
1 5
5 4
2 3
2 4
4 3
2 1

Sample Output

3

思路

恐怖分子要把一个大型爆炸武器从一个城市运送到另一个城市,需要安排警力部署城市来阻止恐怖分子,把警力安排到不同的城市花费不同,现在给出安排警力到每个城市的花费,问最小花费多少可以使阻止恐怖分子。

其实就是有n个点和m条边,还有一个起点s,一个终点t,求使从S到T不连通的最小花费。

因为每个城市部署警力都要花费,所以把每一个城市拆成两个点,因为城市是双向的,所以要建立双向边,但是点已经拆了,所以建立双向边的时候要注意一下,假设第一个城市点的编号是1,2,第二个城市的点的编号是3,4,那么建边的时候要:1–>2,2–>3,3–>4,4–>1。这样才能建立双向边,建好边后直接跑最大流就可以了

代码

#include <cstdio>
#include <cstring>
#include <cctype>
#include <stdlib.h>
#include <string>
#include <map>
#include <iostream>
#include <stack>
#include <cmath>
#include <queue>
#include <vector>
#include <algorithm>
using namespace std;
typedef long long ll;
#define inf 0x3f3f3f3f
#define mem(a,b) memset(a,b,sizeof(a))
const int N=1000+20;
const int M=40000+20;
int top;
int h[N],pre[N],g[N];//h[i]记录每个节点的高度,pre[i]记录前驱,g[i]表示距离为i个节点数有多少个
int first[N];
struct node
{
    int v,next;
    int cap,flow;
} E[M*2];
void init()
{
    mem(first,-1);
    top=0;
}
void add_edge(int u,int v,int c)
{
    E[top].v=v;
    E[top].cap=c;
    E[top].flow=0;
    E[top].next=first[u];
    first[u]=top++;
}
void add(int u,int v,int c)
{
    add_edge(u,v,c);
    add_edge(v,u,0);
}

void set_h(int t)//标高函数,从终点向起点标高
{
    queue<int>q;
    mem(h,-1);
    mem(g,0);
    h[t]=0;
    q.push(t);
    while(!q.empty())
    {
        int v=q.front();
        q.pop();
        g[h[v]]++;//当前高度的个数+1
        for(int i=first[v]; ~i; i=E[i].next)
        {
            int u=E[i].v;
            if(h[u]==-1)//当前节点未标高
            {
                h[u]=h[v]+1;
                q.push(u);
            }
        }
    }
}

int Isap(int s,int t,int n)//isap算法
{
    set_h(t);
    int ans=0,u=s;
    int d;
    while(h[s]<n)//节点的高度小于顶点数
    {
        int i=first[u];
        if(u==s) d=inf;
        for(; ~i; i=E[i].next)
        {
            int v=E[i].v;
            if(E[i].cap>E[i].flow&&h[u]==h[v]+1)//容量大于流量且当前的高度等于要去的高度+1
            {
                u=v;
                pre[v]=i;
                d=min(d,E[i].cap-E[i].flow);//找最小增量
                if(u==t)//到达汇点
                {
                    while(u!=s)
                    {
                        int j=pre[u];//找到u的前驱
                        E[j].flow+=d;//正向流量+d
                        E[j^1].flow-=d;//反向流量-d
                        u=E[j^1].v;//向前搜索
                    }
                    ans+=d;
                    d=inf;
                }
                break;
            }
        }
        if(i==-1)//邻接边搜索完毕,无法行进
        {
            if(--g[h[u]]==0)//重要的优化,这个高度的节点只有一个,结束
                break;
            int hmin=n-1;//重贴标签的高度初始为最大
            for(int j=first[u]; ~j; j=E[j].next)
            {
                if(E[j].cap>E[j].flow)
                    hmin=min(hmin,h[E[j].v]);//取所有邻接点高度的最小值
            }
            h[u]=hmin+1;//重新标高
            g[h[u]]++;//标高后该高度的点数+1
            if(u!=s)//不是源点时,向前退一步,重新搜
                u=E[pre[u]^1].v;
        }
    }
    return ans;
}
int main()
{
    int n,m,st,ed,u,v,x;
    while(~scanf("%d%d",&n,&m))
    {
        init();
        scanf("%d%d",&st,&ed);
        for(int i=1; i<=n; i++)
        {
            scanf("%d",&x);
            add(2*i-1,2*i,x);
        }
        for(int i=1; i<=m; i++)
        {
            scanf("%d%d",&u,&v);
            add(2*u,2*v-1,inf);
            add(2*v,2*u-1,inf);
        }
        printf("%d\n",Isap(st*2-1,ed*2,2*n));
    }
    return 0;
}
这里写代码片
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值