HDU 4289 Control
You, the head of Department of Security, recently received a top-secret information that a group of terrorists are 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 is using the highway network.
The highway network consists of bidirectional highways, connecting and distinct city. A vehicle can only enter/exit the highway network at cities.
Locate some SA (special agents) in some selected cities, so, then the terrorists enter a city under Observatio N (that is, SA was in this city), they would be caught immediately.
It is possible-locate SA in all cities, but since controlling a city with SA could cost your department a certain amount of money, which might vary from city to city, and your budget might not being able to bear the full cost of controlling all C Ities, you must identify a set of cities, which:
* 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 could assume that it's always possible-get from source of the terrorists to their destination.
1 Weapon of Mass destruction
Input
There is several test cases.
The first line of a single test case contains-N and M (2 <= n <=; 1 <= M <= 20000), the Number of cities and the number of highways. Cities is numbered from 1 to N.
The second line contains the 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 Observati On. May assume, the cost are positive and not exceeding 10 7.
The FOLLOWINGM lines tells you about highway network. Each of these lines contains, integers a and B, indicating a bidirectional highway between A and B.
Please proces s until EOF (End of File).
Output
For each test case you should output exactly one line, containing one integer, the sum of the 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
First thought is the tip of the biggest flow problem, but a closer look at the discovery is still a maximum flow problem. Problem solving idea: need to split. Since the city itself has the right value, it needs to be split into two points, a-a ', and the weight is the cost of that point. After the city and the edge of the city to build two-way side, such as a and b two sides, to build a '-B and B ', a weight value is INF. After the completion of the diagram is the normal maximum flow problem.
#include <cstdio>#include <cstring>#include <algorithm>#include <cmath>#include <cstdlib>#include <queue>using namespace STD;typedef Long LongllConst intN =1205;Const intINF =0x3f3f3f3f;intN, M, S, t;intG[n][n], f[n][n];voidInput () {memsetG0,sizeof(G));int Cos; for(inti =1; I <= N; i++) {scanf("%d", &Cos); G[i][i + N] =Cos; }intA, B; for(inti =0; I < m; i++) {scanf("%d%d", &a, &b); G[a + n][b] = INF; G[b + n][a] = INF; }}intMaxflow () {memsetF0,sizeof(F));intA[n], pre[n];intAns =0; Queue<int>Q; while(1) {memsetA0,sizeof(a));memset(Pre,0,sizeof(pre)); A[s] = INF; Q.push (s); while(! Q.empty ()) {intU = Q.front (); Q.pop (); for(intv =0; V < n *2+1; v++) {if(!a[v] && g[u][v] > F[u][v]) {Pre[v] = u; Q.push (v); A[v] = min (A[u], G[u][v]-f[u][v]); } } }if(A[t + N] = =0) Break; Ans + = a[t + n]; for(intu = t + N; U! = S; u = pre[u]) {F[pre[u]][u] + = a[t]; F[u][pre[u]]-= a[t]; } }returnAns;}intMain () { while(scanf("%d%d", &n, &m) = =2) {scanf("%d%d", &s, &t); Input ();printf("%d\n", Maxflow ()); }return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
HDU 4289 Control (Max Stream)