zoukankan      html  css  js  c++  java
  • HDU 4289 Control (最小割 拆点)

    Control

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


    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
    题意:给一个无向图,有些不法分子要从vs点vt点,如今要抓住全部的不法分子阻止他们去vt,那么就要控制某一些城市等待他们,控制每一个城市花费不同,问最少花费是多少。

    解题:最小割,割断全部的通路,花费使得最少,这样就一定能抓住全部的不法分子。

    拆点,每一个点拆成一条有向边v->v ’ 边权为控制这个城市的花费,原图中的边u->v,则建成:u+n->v。v+n->u,边权都为INF。再跑一下最大流,就是ans。

    /*
    最大流:SAP算法,与ISAP的区别就是不用预处理
    */
    #include<stdio.h>
    #include<string.h>
    #include<queue>
    #include<algorithm>
    using namespace std;
    #define captype int
    
    const int MAXN = 100010;   //点的总数
    const int MAXM = 400010;    //边的总数
    const int INF = 1<<30;
    struct EDG{
        int to,next;
        captype cap,flow;
    } edg[MAXM];
    int eid,head[MAXN];
    int gap[MAXN];  //每种距离(或可觉得是高度)点的个数
    int dis[MAXN];  //每一个点到终点eNode 的最短距离
    int cur[MAXN];  //cur[u] 表示从u点出发可流经 cur[u] 号边
    int pre[MAXN];
    
    void init(){
        eid=0;
        memset(head,-1,sizeof(head));
    }
    //有向边 三个參数。无向边4个參数
    void addEdg(int u,int v,captype c,captype rc=0){
        edg[eid].to=v; edg[eid].next=head[u];
        edg[eid].cap=c; edg[eid].flow=0; head[u]=eid++;
    
        edg[eid].to=u; edg[eid].next=head[v];
        edg[eid].cap=rc; edg[eid].flow=0; head[v]=eid++;
    }
    captype maxFlow_sap(int sNode,int eNode, int n){//n是包含源点和汇点的总点个数。这个一定要注意
        memset(gap,0,sizeof(gap));
        memset(dis,0,sizeof(dis));
        memcpy(cur,head,sizeof(head));
        pre[sNode] = -1;
        gap[0]=n;
        captype ans=0;  //最大流
        int u=sNode;
        while(dis[sNode]<n){   //推断从sNode点有没有流向下一个相邻的点
            if(u==eNode){   //找到一条可增流的路
                captype Min=INF ;
                int inser;
                for(int i=pre[u]; i!=-1; i=pre[edg[i^1].to])    //从这条可增流的路找到最多可增的流量Min
                if(Min>edg[i].cap-edg[i].flow){
                    Min=edg[i].cap-edg[i].flow;
                    inser=i;
                }
                for(int i=pre[u]; i!=-1; i=pre[edg[i^1].to]){
                    edg[i].flow+=Min;
                    edg[i^1].flow-=Min;  //可回流的边的流量
                }
                ans+=Min;
                u=edg[inser^1].to;
                continue;
            }
            bool flag = false;  //推断是否能从u点出发可往相邻点流
            int v;
            for(int i=cur[u]; i!=-1; i=edg[i].next){
                v=edg[i].to;
                if(edg[i].cap-edg[i].flow>0 && dis[u]==dis[v]+1){
                    flag=true;
                    cur[u]=pre[v]=i;
                    break;
                }
            }
            if(flag){
                u=v;
                continue;
            }
            //假设上面没有找到一个可流的相邻点,则改变出发点u的距离(也可觉得是高度)为相邻可流点的最小距离+1
            int Mind= n;
            for(int i=head[u]; i!=-1; i=edg[i].next)
            if(edg[i].cap-edg[i].flow>0 && Mind>dis[edg[i].to]){
                Mind=dis[edg[i].to];
                cur[u]=i;
            }
            gap[dis[u]]--;
            if(gap[dis[u]]==0) return ans;  //当dis[u]这样的距离的点没有了,也就不可能从源点出发找到一条增广流路径
                                            //由于汇点到当前点的距离仅仅有一种。那么从源点到汇点必定经过当前点,然而当前点又没能找到可流向的点,那么必定断流
            dis[u]=Mind+1;//假设找到一个可流的相邻点,则距离为相邻点距离+1。假设找不到。则为n+1
            gap[dis[u]]++;
            if(u!=sNode) u=edg[pre[u]^1].to;  //退一条边
        }
        return ans;
    }
    int main()
    {
        int n,m,vs,vt,u,v,cost,ans;
        while(scanf("%d%d",&n,&m)>0)
        {
            scanf("%d%d",&vs,&vt);
            vt+=n;
            init();
            for(int i=1; i<=n; i++){
                scanf("%d",&cost);
                addEdg(i , i+n , cost);
            }
            while(m--){
                scanf("%d%d",&u,&v);
                addEdg(u+n , v , INF);
                addEdg(v+n , u , INF);
            }
            ans=maxFlow_sap(vs , vt , n*2);
            printf("%d
    ",ans);
        }
    }
    


  • 相关阅读:
    POJ 1987 Distance Statistics
    mongo 查询
    图解SSH原理_20190613
    Mongo 备份
    地理空间数据
    fiddler 4 抓取 https 设置
    2、动态元素的定位
    1、selenium 8大元素定位方式
    1、Fiddler 打断点 bpu
    2>/dev/null和>/dev/null 2>&1和2>&1>/dev/null
  • 原文地址:https://www.cnblogs.com/zhchoutai/p/6781237.html
Copyright © 2011-2022 走看看