HDU4289 水题...

本文介绍了一种使用最小割算法来确定拦截恐怖分子的最佳城市的策略。通过构建特殊的图模型,利用ISAP算法求解最小割,从而找出成本最低且能够确保拦截到恐怖分子的城市集合。

 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

既然问的是弄死哪个点的最小割…
那明显不就是拆点嘛….
路全是inf
自己连自己是代价…

#include<iostream>
#include<queue>
#include<stack>
#include<vector>
#include<algorithm>
#include<cstdio>
#include<string>
#include<cstring>
using namespace std;
const int inf = 0x3fffffff;
template <int N, int M>
struct Isap
{
    int top;
    int d[N], pre[N], cur[N], gap[N];
    struct Vertex {
        int head;
    } V[N];
    struct Edge {
        int v, next;
        int c, f;
    } E[M];
    void init() {
        memset(V, -1, sizeof(V));
        top = 0;
    }
    void add_edge(int u, int v, int c) {
        E[top].v = v;
        E[top].c = c;
        E[top].f = 0;
        E[top].next = V[u].head;
        V[u].head = top++;
    }
    void add(int u, int v, int c) {
        add_edge(u, v, c);
        add_edge(v, u, 0);
    }
    void set_d(int t) {
        queue<int> Q;
        memset(d, -1, sizeof(d));
        memset(gap, 0, sizeof(gap));
        d[t] = 0;
        Q.push(t);
        while (!Q.empty()) {
            int v = Q.front(); Q.pop();
            ++gap[d[v]];
            for (int i = V[v].head; ~i; i = E[i].next) {
                int u = E[i].v;
                if (d[u] == -1) {
                    d[u] = d[v] + 1;
                    Q.push(u);
                }
            }
        }
    }
    int sap(int s, int t, int num) {
        set_d(t);
        int ans = 0, u = s;
        int flow = inf;
        memcpy(cur, V, sizeof(V));
        while (d[s] < num) {
            int &i = cur[u];
            for (; ~i; i = E[i].next) {
                int v = E[i].v;
                if (E[i].c > E[i].f && d[u] == d[v] + 1) {
                    u = v;
                    pre[v] = i;
                    flow = min(flow, E[i].c - E[i].f);
                    if (u == t) {
                        while (u != s) {
                            int j = pre[u];
                            E[j].f += flow;
                            E[j ^ 1].f -= flow;
                            u = E[j ^ 1].v;
                        }
                        ans += flow;
                        flow = inf;
                    }
                    break;
                }
            }
            if (i == -1) {
                if (--gap[d[u]] == 0)
                    break;
                int dmin = num - 1;
                cur[u] = V[u].head;
                for (int j = V[u].head; ~j; j = E[j].next)
                    if (E[j].c > E[j].f)
                        dmin = min(dmin, d[E[j].v]);
                d[u] = dmin + 1;
                ++gap[d[u]];
                if (u != s)
                    u = E[pre[u] ^ 1].v;
            }
        }
        return ans;
    }
};
Isap<100005, 200005> Sap;
int main()
{
    int n, m;
    while (cin >> n >> m)
    {
        Sap.init();
        int q, w;
        cin >> q >> w;
        int qq = q, ww = w;
        int st = 3 * n, zt = 3 * n + 1;
        Sap.add(st, q, inf);
        Sap.add(w+n, zt, inf);
        for (int a = 1; a <= n; a++)
        {
            cin >> q;
    //      if (a == qq || a == ww)continue;
            Sap.add(a, a + n, q);
        }
        for (int a = 1; a <= m; a++)
        {
            cin >> q >> w;
//          if (q != qq&&w != ww)
//          {
                Sap.add(q + n, w, inf);
                Sap.add(w + n, q, inf);
//          }
        }
        cout << Sap.sap(st, zt, 15 * m + 6 * n) << endl;
    }
}
### HDU 3056 Stock Algorithm Solution HDU 3056 是一个涉及动态规划(Dynamic Programming, DP)的经典问,目标是找到股票交易的最大利润。此问通常被归类为“股票买卖”系列问之一。以下是对此问的详细分析和代码实现。 #### 问描述 给定一个数组 `prices`,其中 `prices[i]` 表示第 `i` 天的股票价格。最多允许完成两笔交易(即最多买两次、卖两次),求能够获得的最大利润。注意:不能同时参与多笔交易(必须在再次购买前出售掉之前的股票)[^1]。 #### 动态规划解法 定义状态 `dp[i][k][0/1]`,其中: - `i` 表示第 `i` 天; - `k` 表示当前允许的最多交易次数; - `0/1` 表示当前是否持有股票(`0` 表示未持有,`1` 表示持有)。 状态转移方程如下: - 当前未持有股票:`dp[i][k][0] = max(dp[i-1][k][0], dp[i-1][k][1] + prices[i])` - 当前持有股票:`dp[i][k][1] = max(dp[i-1][k][1], dp[i-1][k-1][0] - prices[i])` 最终答案为 `dp[n-1][2][0]`,即最后一天完成至多两次交易且不持有股票时的最大利润。 #### 代码实现 以下是一个基于上述思路的 C++ 实现: ```cpp #include <iostream> #include <vector> #include <algorithm> using namespace std; int maxProfit(vector<int>& prices) { if (prices.empty()) return 0; int n = prices.size(); vector<vector<int>> dp(n, vector<int>(5, 0)); // dp[i][k][0/1] // 初始化 dp[0][0] = 0; // 第一次未持有 dp[0][1] = -prices[0]; // 第一次持有 dp[0][2] = 0; // 第二次未持有 dp[0][3] = -prices[0]; // 第二次持有 dp[0][4] = 0; // 最终结果 for (int i = 1; i < n; ++i) { dp[i][0] = 0; // 第一次未持有 dp[i][1] = max(dp[i-1][1], dp[i-1][0] - prices[i]); // 第一次持有 dp[i][2] = max(dp[i-1][2], dp[i-1][1] + prices[i]); // 第二次未持有 dp[i][3] = max(dp[i-1][3], dp[i-1][2] - prices[i]); // 第二次持有 dp[i][4] = max(dp[i-1][4], dp[i-1][3] + prices[i]); // 最终结果 } return dp[n-1][4]; } int main() { vector<int> prices = {3, 3, 5, 0, 0, 3, 1, 4}; cout << "Maximum Profit: " << maxProfit(prices) << endl; return 0; } ``` #### 算法复杂度 - 时间复杂度:`O(n)`,其中 `n` 是价格数组的长度。 - 空间复杂度:`O(n)`,可以进一步优化为 `O(1)` 如果只使用几个变量存储中间状态。 --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值