HDU 4289 Control(网络流,拆点)

本文介绍了一种利用最小割算法解决恐怖分子从一城市运输危险物品到另一城市的拦截问题的方法。通过将城市网络转化为有向图并进行点拆分,结合最大流算法计算出最小的拦截成本。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Control

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4716    Accepted Submission(s): 1953

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

Source

2012 ACM/ICPC Asia Regional Chengdu Online

Recommend

liuyiding   |   We have carefully selected several similar problems for you:  4288 4296 4295 4294 4293 

 

有N个城市,S是歹徒的初始位置,D是歹徒将要去的位置,警方不知道他们的具体位置,所以选择封锁城市来消灭歹徒,

不过封锁城市是需要一定的代价的,所以让你求阻断歹徒从S到D需要的最小代价。

这是最小割,但是最大流=最小割,所以可以转换成最大流问题,需要注意的是双向边,所以我们需要转化成有向边来使用

最大流来解决,然后是拆点,把一个城市拆成两个。

关于建图:这里将每个点拆成两个点(i ,i + n)然后i与i + n 的权值为该城市的代价,反向边为0;

两个城市之间:(u,v)则建立(u + n,v)  (v + n,u)权值为inf 这里他们的反向边都为0;

图片来自:https://blog.youkuaiyun.com/w144215160044/article/details/47604851

/*
N,M 城市的数量 高速公路的数量
城市从 1->N 编号,
S,D 源的编号和目的地的编号
一下N行包含成本
第i行只包含一个整数,即在第i个城市中定位SA的成本
一下M行 是高速公路网络的信息,

*/
#include <iostream>
#include <cstring>
#include <algorithm>
#include <string>
#include <cstdio>
#include <queue>
#include <stack>
using namespace std;
const int inf=0x3f3f3f3f;
const int maxn=110000;
struct Edge
{
    int to,w,next;
}edge[maxn];
int tot,head[maxn],level[maxn];
void add_edge(int u,int v,int w)
{
    edge[tot].to=v;
    edge[tot].w=w;
    edge[tot].next=head[u];
    head[u]=tot++;

    edge[tot].to=u;
    edge[tot].w=0;
    edge[tot].next=head[v];
    head[v]=tot++;
}
int BFS(int s,int t)
{
    memset(level,0,sizeof(level));
    queue<int > que;
    level[s]=1;
    que.push(s);
    while(!que.empty())
    {
        int u=que.front();
        que.pop();
        for(int i=head[u];i!=-1;i=edge[i].next)
        {
            int v=edge[i].to;
            if(level[v]==0&&edge[i].w>0)
            {
                level[v]=level[u]+1;
                if(v==t)
                    return 1;
                que.push(v);
            }
        }
    }return 0;
}
int DFS(int u,int t,int f)
{
    if(u==t)
        return f;
    int cost=0;
    for(int i=head[u];i!=-1;i=edge[i].next)
    {
        int v=edge[i].to;
        int w=edge[i].w;
        if(level[v]==level[u]+1&&w>0)
        {
            int d=DFS(v,t,min(w,f-cost));
            if(d>0)
            {
                edge[i].w-=d;
                edge[i^1].w+=d;
                cost+=d;
                if(cost==f)
                    break;
                else level[v]=-1;
            }
        }
    }return cost;
}
int Dinic(int s,int t)
{
    int maxflow=0;
    while(BFS(s,t))
    {
        maxflow+=DFS(s,t,inf);
    }
    return maxflow;
}
int main()
{
    int n,m;
    while(~scanf("%d%d",&n,&m))
    {
        int s,t,u,v,w;
        memset(head,-1,sizeof(head));
        tot=0;
        scanf("%d%d",&s,&t);
        t=t+n;
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&w);
            //拆点 把一个城市拆成两个,这两点的容量就为该点的消费量
            add_edge(i,i+n,w);
        }
        for(int i=1;i<=m;i++)
        {
            scanf("%d%d",&u,&v);//连接的两个城市(一定能走通 所以赋值inf)
            add_edge(u+n,v,inf);
            add_edge(v+n,u,inf);//不同城市之间的流量为 inf
        }
        printf("%d\n",Dinic(s,t));
    }return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值