hdu4289—Control(最大流)

来源:互联网 发布:linux的用户组是什么 编辑:程序博客网 时间:2024/05/23 00:43

题目链接:传送门

Control

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


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

Sample Output
3
 


解题思路:若想用最小的花费抓到恐怖分子,就要使在恐怖分子的必经之路上布置兵力的花费最少,想一下只有一条路的情况,假如S(3)—A(6)—B(2)—C(5)—T(4),此时无论在该条路径上的任意一个点布置兵力都能完成任务,但要使花费最小,就要在B出布置兵力,此时将点抽象成一条管子,点权看成容量,题目就变成了求该条路径上的最大流。建边:将一个点p分出另外一个点p',p和p'间建条边,容量为点权,若p与q点相连,则p'与q建边,q'与p建边,容量为无穷大。

#include <cstdio>#include <algorithm>#include <queue>#include <iostream>using namespace std;const int N = 420;const int M = 40900;//边的结构struct edge_t{    int node;    int c;//c为容量    edge_t* next;    edge_t* redge;//指向反向边}Edge[M*2];int ECnt;//图的邻接表edge_t* Ver[N];void init(){    ECnt = 0;    fill(Ver,Ver+N,(edge_t*)0);}//生成双向边void mkEdge(int a,int b,int c){    int t1 = ECnt++;    int t2 = ECnt++;    Edge[t1].node = b;    Edge[t1].c = c;    Edge[t1].next = Ver[a];    Edge[t1].redge = Edge + t2;    Ver[a] = Edge + t1;    Edge[t2].node = a;    Edge[t2].c = 0;    Edge[t2].next = Ver[b];    Edge[t2].redge = Edge + t1;    Ver[b] = Edge + t2;}int L[N];//层次图//建立残留网络从源s到汇t的层次图bool bfs(int s,int t){    fill(L,L+N,-1);    queue<int>q;    q.push(s);    L[s] = 0;    while( !q.empty() ){        int u = q.front();        q.pop();        //寻找还有残量的边        for(edge_t*p=Ver[u];p;p=p->next){            if ( p->c <= 0 ) continue;            int v = p->node;            if ( -1 != L[v] ) continue;            q.push(v);            L[v] = L[u] + 1;        }    }    return -1 != L[t];}//在层次图上搜索增广路径,本质上就是搜索可以增广的流量//这个流量是各层之间流量的最小值//u为当前节点,cf为当前层的最小流,t为汇点int dfs(int u,int e,int cf){    if ( u == e ) return cf;    int tf = 0;  //tf记录u往下一层的总可行流量    for(edge_t*p=Ver[u];p;p=p->next){        int v = p->node;        int c = p->c;        if ( L[u] + 1 == L[v] && c > 0 && cf > tf ){            int f = dfs(v,e,min(c,cf-tf));            if ( 0 == f ) continue;            p->c -= f;//正向边减去可行流量            p->redge->c += f;//反向边加上            tf += f;        }    }    if ( 0 == tf ) L[u] = -1;//修改层次图    return tf;}//Dinic算法,s为源,t为汇int Dinic(int s,int t){    int ret = 0;    while( bfs(s,t) ){//第一步建立分层图        int ans;        //第二步在分层图上查找一条增广路径的可行流量        while( ans = dfs(s,t,INT_MAX) )            ret += ans;    }    return ret;}int cost[N];void Build( int m , int n ){    int a,b;    for( int i = 0 ; i < m ; ++i ){        scanf("%d%d",&a,&b);        mkEdge(a+n,b,INT_MAX);        mkEdge(b+n,a,INT_MAX);    }    for( int i = 1 ; i <= n ; ++i ){        mkEdge(i,i+n,cost[i]);    }}int main(){    int n,m,s,e;    while( ~scanf("%d%d",&n,&m) ){        init();        scanf("%d%d",&s,&e);        for( int i = 1 ; i <= n ; ++i ){            scanf("%d",&cost[i]);        }        Build(m,n);        printf("%d\n",Dinic(s,e+n));    }    return 0;}


原创粉丝点击