zoukankan      html  css  js  c++  java
  • hdu4289 Control 最大流最小割

    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

    题意:有一些城市,城市之间有一些边,有通缉犯要从一个城市到另一个城市,为了抓捕通缉犯,你需要调遣兵力守在若干城市,以保证通缉犯无论从哪里走都一定会经过有你的部下看守的城市,问最少需要部署多少个城市。

    最小割性质的裸题,就是使原点和汇点不连通的最小割的大小,建边直接跑网络流,最大流=最小割。

      1 #include<stdio.h>
      2 #include<string.h>
      3 #include<vector>
      4 #include<queue>
      5 #include<algorithm>
      6 using namespace std;
      7 const int maxm=1000;
      8 const int INF=0x7fffffff;
      9 
     10 struct edge{
     11     int from,to,f;
     12     edge(int a,int b,int c):from(a),to(b),f(c){}
     13 };
     14 
     15 struct dinic{
     16     int s,t,m;
     17     vector<edge>e;
     18     vector<int>g[maxm];
     19     bool vis[maxm];
     20     int cur[maxm],d[maxm];
     21 
     22     void init(int n){
     23         for(int i=1;i<=n;i++)g[i].clear();
     24         e.clear();
     25     }
     26 
     27     void add(int a,int b,int c){
     28         e.push_back(edge(a,b,c));
     29         e.push_back(edge(b,a,0));
     30         m=e.size();
     31         g[a].push_back(m-2);
     32         g[b].push_back(m-1);
     33     }
     34 
     35     bool bfs(){
     36         memset(vis,0,sizeof(vis));
     37         queue<int>q;
     38         q.push(s);
     39         vis[s]=1;
     40         d[s]=0;
     41         while(!q.empty()){
     42             int u=q.front();
     43             q.pop();
     44             for(int i=0;i<g[u].size();i++){
     45                 edge tmp=e[g[u][i]];
     46                 if(!vis[tmp.to]&&tmp.f>0){
     47                     d[tmp.to]=d[u]+1;
     48                     vis[tmp.to]=1;
     49                     q.push(tmp.to);
     50                 }
     51             }
     52         }
     53         return vis[t];
     54     }
     55 
     56     int dfs(int x,int a){
     57         if(x==t||a==0)return a;
     58         int flow=0,f;
     59         for(int& i=cur[x];i<g[x].size();i++){
     60             edge& tmp=e[g[x][i]];
     61             if(d[tmp.to]==d[x]+1&&tmp.f>0){
     62                 f=dfs(tmp.to,min(a,tmp.f));
     63                 tmp.f-=f;
     64                 e[g[x][i]^1].f+=f;
     65                 flow+=f;
     66                 a-=f;
     67                 if(a==0)break;
     68             }
     69         }
     70         if(flow==0)d[x]=-1;
     71         return flow;
     72     }
     73 
     74     int mf(int s,int t){
     75         this->s=s;
     76         this->t=t;
     77         int flow=0;
     78         while(bfs()){
     79             memset(cur,0,sizeof(cur));
     80             flow+=dfs(s,INF);
     81         }
     82         return flow;
     83     }
     84 };
     85 
     86 int main(){
     87     int n,m;
     88     while(scanf("%d%d",&n,&m)!=EOF){
     89         dinic d;
     90         d.init(2*n+10);
     91         int a,b;
     92         scanf("%d%d",&a,&b);
     93         d.add(0,a,INF);
     94         d.add(b+n,2*n+1,INF);
     95         int i,j;
     96         for(i=1;i<=n;i++){
     97             scanf("%d",&a);
     98             d.add(i,i+n,a);
     99         }
    100         for(i=1;i<=m;i++){
    101             scanf("%d%d",&a,&b);
    102             d.add(a+n,b,INF);
    103             d.add(b+n,a,INF);
    104         }
    105         printf("%d
    ",d.mf(0,2*n+1));
    106     }
    107     return 0;
    108 }
    View Code
  • 相关阅读:
    面试后的一些思考
    NEW关键字的三种用法
    关于反射的初步介绍
    REF和OUT关键字的介绍
    关于WPF中TextBox行的选择与显示的问题
    资源字典——程序集之间的资源共享
    rsync 实现断点续传
    CentOS 7 安装 Git
    使用 docker 拉取镜像和创建容器-nginx
    docker安装 之 ---CentOS 7 系统脚本自动安装
  • 原文地址:https://www.cnblogs.com/cenariusxz/p/6598026.html
Copyright © 2011-2022 走看看