Hdu 4289 Control (max Stream)

Source: Internet
Author: User

Hdu 4289 Control (max Stream)
Hdu 4289 Control

You, the head of Department of Security, recently pinned ed 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 wocould 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 10 7.
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 shocould 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 2 2 4 3 2 1

Sample Output

3

At first, I thought it was the biggest stream problem with the tip. But after a closer look, I found it was still the biggest stream problem. Solution: Remove points. Because the city itself has a permission value, you need to split it into two points, a-> A', and the permission value is the cost of the point. Then, two sides between the city and the city need to be built. For example, if a and B are two sides, a '-> B and B'-> a is set to INF. After creating a graph, it is a normal maximum flow problem.
#include 
  
   #include 
   
    #include #include 
    
     #include 
     
      #include 
      
       using namespace std;typedef long long ll;const int N = 1205;const int INF = 0x3f3f3f3f;int n, m, s, t;int G[N][N], F[N][N];void input() { memset(G, 0, sizeof(G)); int cos; for (int i = 1; i <= n; i++) { scanf(%d, &cos); G[i][i + n] = cos; } int a, b; for (int i = 0; i < m; i++) { scanf(%d %d, &a, &b); G[a + n][b] = INF; G[b + n][a] = INF; }}int maxFlow() { memset(F, 0, sizeof(F)); int a[N], pre[N]; int ans = 0; queue
       
         Q; while (1) { memset(a, 0, sizeof(a)); memset(pre, 0, sizeof(pre)); a[s] = INF; Q.push(s); while (!Q.empty()) { int u = Q.front(); Q.pop(); for (int v = 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 (int u = t + n; u != s; u = pre[u]) { F[pre[u]][u] += a[t]; F[u][pre[u]] -= a[t]; } } return ans;}int main() { while (scanf(%d %d, &n, &m) == 2) { scanf(%d %d, &s, &t); input(); printf(%d, maxFlow()); } return 0;}
       
      
     
    
   
  

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.