• HDU 4289 Control (Dinic)


    Control

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


    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
     
    Recommend
    liuyiding
     

     题意:有N个城市 M条无向边 有一恐怖分子要从某一城市到另一城市 打算在某些城市安放一些SA 去抓住他 但若在某个城市安放SA需要一定费用 求要抓到恐怖分子 最少的费用是多少?

    思路:网络流问题。建一超级源点和汇点与原源点、汇点相连,然后把一个城市拆成两个点 边权为其费用 两相连城市间的边权为无穷大  求其最大流即可。

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<queue>
    
    using namespace std;
    
    const int VM=420;
    const int EM=500010;
    const int INF=0x3f3f3f3f;
    
    struct Edge{
        int to,nxt;
        int cap;
    }edge[EM];
    
    int n,m,src,des,cnt,head[VM];
    int dep[VM];
    
    void addedge(int cu,int cv,int cw){
        edge[cnt].to=cv;    edge[cnt].cap=cw;   edge[cnt].nxt=head[cu];
        head[cu]=cnt++;
        edge[cnt].to=cu;    edge[cnt].cap=0;    edge[cnt].nxt=head[cv];
        head[cv]=cnt++;
    }
    
    int BFS(){
        queue<int> q;
        while(!q.empty())
            q.pop();
        memset(dep,-1,sizeof(dep));
        dep[src]=0;
        q.push(src);
        while(!q.empty()){
            int u=q.front();
            q.pop();
            for(int i=head[u];i!=-1;i=edge[i].nxt){
                int v=edge[i].to;
                if(edge[i].cap>0 && dep[v]==-1){
                    dep[v]=dep[u]+1;
                    q.push(v);
                }
            }
        }
        return dep[des]!=-1;
    }
    
    int DFS(int u,int minx){
        if(u==des)
            return minx;
        int tmp;
        for(int i=head[u];i!=-1;i=edge[i].nxt){
            int v=edge[i].to;
            if(edge[i].cap>0 && dep[v]==dep[u]+1 && (tmp=DFS(v,min(minx,edge[i].cap)))){
                edge[i].cap-=tmp;
                edge[i^1].cap+=tmp;
                return tmp;
            }
        }
        dep[u]=-1;
        return 0;
    }
    
    int Dinic(){
        int ans=0,tmp;
        while(BFS()){
            while(1){
                tmp=DFS(src,INF);
                if(tmp==0)
                    break;
                ans+=tmp;
            }
        }
        return ans;
    }
    
    int main(){
    
        //freopen("input.txt","r",stdin);
    
        int s,t;
        while(~scanf("%d%d",&n,&m)){
            cnt=0;
            memset(head,-1,sizeof(head));
            scanf("%d%d",&s,&t);
            src=0, des=2*n+1;
            addedge(src,s,INF);
            addedge(n+t,des,INF);
            int u,v,w;
            for(int i=1;i<=n;i++){
                scanf("%d",&w);
                addedge(i,n+i,w);
                addedge(n+i,i,w);
            }
            for(int i=1;i<=m;i++){
                scanf("%d%d",&u,&v);
                addedge(n+u,v,INF);     //注意这里的建边,src--->s--->u(某条边)---->n+u(拆分u点后的另一点)---->v---->n+v(拆分v点后的另一点)---->u-----
                addedge(n+v,u,INF);     //所以,addedge(n+u,v,INF);仔细想想,这样才能保证 u 和 v 使连接着的
            }
            printf("%d\n",Dinic());
        }
        return 0;
    }
  • 相关阅读:
    Windows下使用CMake编译SuiteSparse成VS工程
    【设计模式
    【设计模式
    vue过滤和复杂过滤
    el-tooltip 自定义样式
    el-table加表单验证
    使用Go env命令设置Go的环境
    面试官:GET 和 POST 两种基本请求方法有什么区别?
    解决 Vue 重复点击相同路由报错的问题
    利用promise和装饰器封装一个缓存api请求的装饰器工具
  • 原文地址:https://www.cnblogs.com/jackge/p/3021169.html
Copyright © 2020-2023  润新知