题目描述
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 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
题目链接
http://acm.hdu.edu.cn/showproblem.php?pid=4289
解题思路
题意就是有恐怖分子要袭击城市,你可已在城市中设置一些SA来抓捕他们,每放置一个SA都需要相对应的金钱,但是由于你的预算问题不能在所有的城市中都放置SA,现在求的是抓捕全部的恐怖分子所花费的最少费用。
将每个城市拆成两个点,两点之间的权值就是安装SA的费用,然后将不同城市之间的流量设为无穷大,这样就能保证流量只受拆点边的限制。边建好之后就能跑一遍Dinic了。
AC代码
78MS/2556K/G++1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112#include <queue>
#include <cstdio>
#include <cstring>
#include <iostream>
#define inf 0x3f3f3f3f
using namespace std;
const int maxn = 100005;
struct Edge
{
int to, c, next;
} e[maxn * 4];
int src, sink, cnt;
int head[1000];
void adde(int a, int b, int c)
{
e[cnt].to = b;
e[cnt].c = c;
e[cnt].next = head[a];
head[a] = cnt++;
e[cnt].to = a;
e[cnt].c = 0;
e[cnt].next = head[b];
head[b] = cnt++;
}
int vis[1000], dis[1000];
int cur[1000];
bool BFS()
{
memset(dis, -1, sizeof(dis));
dis[src] = 0;
queue<int> que;
que.push(src);
while (!que.empty())
{
int j = que.front();
que.pop();
for (int k = head[j]; k != -1; k = e[k].next)
{
int i = e[k].to;
if (dis[i] == -1 && e[k].c)
{
dis[i] = dis[j] + 1 ;
que.push(i);
if (i == sink) return true;
}
}
}
return false;
}
int DFS(int x, int mx)
{
if (x == sink || mx == 0) return mx;
int f, flow = 0;
for (int& i = cur[x]; i != -1; i = e[i].next)
{
if (dis[x] + 1 == dis[e[i].to] && (f = DFS(e[i].to, min(mx, e[i].c))))
{
e[i].c -= f;
e[i ^ 1].c += f;
flow += f;
mx -= f;
if (!mx) break;
}
}
return flow;
}
int dinic()
{
int tmp = 0;
int maxflow = 0;
while (BFS())
{
for (int i = src; i <= sink; i++)
cur[i] = head[i];
while (tmp = DFS(src, inf))
maxflow += tmp;
}
return maxflow;
}
int main()
{
int n, m, s, t, x, y;
while (~scanf("%d%d", &n, &m))
{
cnt = 0;
memset(head, -1, sizeof(head));
scanf("%d%d", &s, &t);
src = 0;
sink = 2 * n + 1;
adde(src, s, inf);
adde(n + t, sink, inf);
for (int i = 1; i <= n; i++)
{
scanf("%d", &x);
adde(i, i + n, x);
adde(i + n, i, x);
}
for (int i = 1; i <= m; i++)
{
scanf("%d%d", &x, &y);
adde(x + n, y, inf);
adde(y + n, x, inf);
}
printf("%d\n", dinic());
}
return 0;
}