【最小割最大流Dinic】 HDU - 4289 I - Control

I - Control  HDU - 4289 

  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 10 7. 
  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走到d
每个城市可以放间谍用来抓住他们,每个花费会给出
问最少花多少钱可以抓住匪徒
最小割问题
虽然是双向边,但是不能直接在add_edge里改
一定要首尾相连
所以最后求得是s->d+200

#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
const int MAX_V=2005;
const int st=0;
const int en=2000;
const int INF=0x3f3f3f3f;

struct edge
{
    int to,cap,rev; //用于表示边的结构体(终点,容量,反向边)
};

vector <edge> G[MAX_V]; //图的邻接表表示
int level[MAX_V];   //顶点到源点的距离标号
int iter[MAX_V];    //当前弧,在其之前的边已经没有用了
char a[222];

void add_edge(int from,int to,int cap)
{
    G[from].push_back((edge){to,cap,G[to].size()});
    G[to].push_back((edge){from,0,G[from].size()-1});
}

void bfs(int s)  //通过bfs计算从源点出发的距离标号
{
    memset(level,-1,sizeof(level));
    queue <int> q;
    level[s]=0;
    q.push(s);
    while(!q.empty())
    {
        int v=q.front();q.pop();
        for(int i=0;i<G[v].size();i++)
        {
            edge &e=G[v][i];
            if(e.cap>0&&level[e.to]<0)
            {
                level[e.to]=level[v]+1;
                q.push(e.to);
            }
        }
    }
}

int dfs(int v,int t,int f)  //通过dfs寻找增广路
{
    if(v==t) return f;
    for(int &i=iter[v];i<G[v].size();i++)
    {
        edge &e=G[v][i];
        if(e.cap>0&&level[v]<level[e.to])
        {
            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;
    for(;;)
    {
        bfs(s); //计算层次图
        if(level[t]<0) return flow; //找不到s-t路径
        memset(iter,0,sizeof(iter)); //初始化当前弧
        int f;
        while((f=dfs(s,t,INF))>0)  //更新最大流
            flow+=f;
    }
    return flow;
}

int main()
{
    int n,m,s,d;
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        for(int i=0;i<=MAX_V;i++) G[i].clear();
        scanf("%d%d",&s,&d);
        for(int i=1;i<=n;i++)
        {
            int w;
            scanf("%d",&w);
            add_edge(i,i+200,w);
            add_edge(i+200,i,w);
        }
        for(int i=1;i<=m;i++)
        {
            int u,v;
            scanf("%d%d",&u,&v);
            add_edge(u+200,v,INF);
            add_edge(v+200,u,INF);
        }
        printf("%d\n",max_flow(s,d+200));
    }
    return 0;
}





 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值