hdu4289 Control(最大流拆点)dinic模板

本文详细介绍了如何利用Dinic算法解决HDU4289 Control这个最大流问题,探讨了最大流问题中的拆点概念,并提供了具体的算法模板和实例解析,帮助读者深入理解并应用Dinic算法。
摘要由CSDN通过智能技术生成




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 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 65 35234121 55 42 32 44 32 1
 

Sample Output
 
  
3


#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <queue>
#pragma comment(linker, "/STACK:1024000000,1024000000")
using namespace std;
const int inf = 0x3f3f3f3f;
const int mod=609929123;
const int N=100220;
int n,f,d,m;
int S,T,R;//源点,汇点
struct node
{
    int v,cap,next;//领接表
}es[4*N];
int frist[N];
int dis[N];
int current[N];//当前弧优化
int addedge(int u,int v,int cap)
{
    node  e1={v,cap,frist[u]};
    es[R]=e1;
    frist[u]=R++;
    node e2={u,0,frist[v]};
    es[R]=e2;
    frist[v]=R++;
}
int bfs()
{
  queue<int>q;
  q.push(S);
  memset(dis,-1,sizeof(dis));
  dis[S]=0;
  while(!q.empty())
  {
      int h=q.front();
      if(h==T) return 1;
      q.pop();
      for(int i=frist[h];i!=-1;i=es[i].next)
      {
          int temp=es[i].v;
          if(dis[temp]==-1&&es[i].cap)
          {
              dis[temp]=dis[h]+1;
              q.push(temp);
          }
      }
  }
  return 0;
}
int dinic(int x,int maxflow)
{
  if(x==T) return maxflow;
  int flow,f=0;
  for(int &i=current[x];i!=-1;i=es[i].next)
  {
      int temp=es[i].v;
      if(dis[temp]==dis[x]+1&&es[i].cap)
      {
      flow=dinic(temp,min(maxflow-f,es[i].cap));
      es[i].cap-=flow;
      es[i^1].cap+=flow;
      f+=flow;
      if(f==maxflow) break;
      }
  }
  return f;
}
int DINIC()
{
 int ans=0;
 while(bfs())
 {
     int flow;
     memcpy(current,&frist,sizeof(frist));//复制
     while(flow=dinic(S,inf))
     ans+=flow;
 }
 return ans;
}
int main()
{
    int x,y,u,v,cap;
    while(~scanf("%d%d",&n,&m))
    {
          R=0;
        memset(frist,-1,sizeof(frist));
        scanf("%d%d",&x,&y);
         S=x,T=y+n;
         for(int i=1;i<=n;i++)
         {
             scanf("%d",&cap);
             addedge(i,i+n,cap);
         }
         for(int i=1;i<=m;i++)
         {
             scanf("%d%d",&u,&v);
             addedge(u+n,v,inf);
             addedge(v+n,u,inf);
         }
         printf("%d\n", DINIC());
    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值