欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程资源 > 编程问答 >内容正文

编程问答

HDU Problem - 4289 Control(最大流)

发布时间:2024/4/18 编程问答 48 豆豆
生活随笔 收集整理的这篇文章主要介绍了 HDU Problem - 4289 Control(最大流) 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

题目链接

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 6 5 3 5 2 3 4 12 1 5 5 4 2 3 2 4 4 3 2 1

Sample Output

3

AC

  • 题目要求截住所有的劫匪,所以求一个最小割,使整个图不连通即可
  • 拆点建图(因为双向边)
  • 将题目给的边,拆开建边,流量为inf
  • 将每个点拆开建边,流量为花费
#include <iostream> #include <stdio.h> #include <map> #include <vector> #include <queue> #include <algorithm> #include <cmath> #define N 405 #include <cstring> #define ll long long #define P pair<int, int> #define mk make_pair using namespace std; struct ac{int v, c, pre; }edge[20001 * 4]; int head[N], dis[N], curedge[N], cnt; int inf = 0x3f3f3f3f; int n, m; void addedge(int u, int v, int c) {edge[cnt].v = v;edge[cnt].c = c;edge[cnt].pre = head[u];head[u] = cnt++;swap(u, v);edge[cnt].v = v;edge[cnt].c = 0;edge[cnt].pre = head[u];head[u] = cnt++; } bool bfs(int s, int e) {queue<int> que;que.push(s);memset(dis, 0, sizeof(dis));dis[s] = 1;while (!que.empty()) {int t = que.front();que.pop();for (int i = head[t]; i != -1; i = edge[i].pre) {if (dis[edge[i].v] || edge[i].c == 0) continue;dis[edge[i].v] = dis[t] + 1;que.push(edge[i].v);} }return dis[e] != 0; } int dfs(int s, int e, int flow) {if (s == e) return flow;for (int &i = curedge[s]; i != -1; i = edge[i].pre) {if (dis[edge[i].v] == dis[s] + 1 && edge[i].c) {int d = dfs(edge[i].v, e, min(flow, edge[i].c));if (d > 0) {edge[i].c -= d;edge[i ^1].c += d;return d; }}}return 0; } int solve(int s, int e) {int sum = 0;while (bfs(s, e)) {for (int i = 0; i <= n * 2; ++i) {curedge[i] = head[i];}int d;while (d = dfs(s, e, inf)) {sum += d;}}return sum; }int main() { #ifndef ONLINE_JUDGEfreopen("in.txt", "r", stdin); #endifwhile (scanf("%d%d", &n, &m) != EOF) {cnt = 0;memset(head, -1, sizeof(head));int s, e;scanf("%d%d", &s, &e);// 自己到自己,流量为花费 for (int i = 1; i <= n; ++i) {int t; scanf("%d", &t);addedge(i, i + n, t);}// 建边 for (int i = 0; i < m; ++i) {int l, r;scanf("%d%d", &l, &r);addedge(l + n, r, inf);addedge(r + n, l, inf);}int ans = solve(s, e + n);printf("%d\n", ans);}return 0; }

总结

以上是生活随笔为你收集整理的HDU Problem - 4289 Control(最大流)的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。