程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> hdu 4289 Control (最大流)

hdu 4289 Control (最大流)

編輯:C++入門知識

hdu 4289 Control (最大流)


hdu 4289 Control

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

題目大意:一開始以為是最小費最大流問題,但仔細看一下,發現還是一個最大流問題。

解題思路:需要拆點。因為城市本身帶有權值,所以需要拆成兩個點,a -> a’,權值就是該點的費用。之後的城市與城市之間的邊要建雙向邊,比如a和b兩個邊,要建a’ -> b和b’ -> a權值是INF。建完圖之後就是普通的最大流問題了。

#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;
}

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved