解题报告 之 HDU4289 Control

来源:互联网 发布:淘宝禁售商品有哪些 编辑:程序博客网 时间:2024/05/21 10:14

解题报告 之 HDU 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 WMD1 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 65 35234121 55 42 32 44 32 1
 

Sample Output
3

题目大意:有一个图,现在需要在一些城市节点上放置武装,不同城市的武装成本不同,现在有一个间谍从起点城市走到终点城市,则问一定能拦截间谍的最小花费是多少?

分析:一看就是最小割问题,转化为最大流问题。关于最大流和最小割等价的证明见《挑战》。这个题的重点在于两点:一是拆点,因为此题是关于节点代价的题,那么需要将一个节点拆成两个节点中间连一条路,以节点代价为这条新路的最大负载,二是将连通的节点之间加上一条负载为无限的边,注意是双向的(即实际要加上两条边),那么最后的答案就是求起点到终点的最大流。

于是乎上代码,建图比较容易出错。
#include<iostream>#include<cstdio>#include<cstring>#include<queue>#include<string>using namespace std;const int MAXN = 500;const int MAXM = 80010;const int INF = 0x3f3f3f3f;struct Edge{int to, next;int cap;};Edge edge[MAXM];int cnt, src, des;//cnt表示一共有几条边int head[MAXN];//head[i]表示节点i的第一条边的编号int level[MAXN];void addedge(int from, int to, int cap){edge[cnt].to = to;edge[cnt].cap = cap;edge[cnt].next = head[from];head[from] = cnt++;edge[cnt].to = from;edge[cnt].cap = 0;edge[cnt].next = head[to];head[to] = cnt++;}int bfs(){queue<int> q;while (!q.empty())q.pop();memset(level, -1, sizeof(level));level[src] = 0; //源点的深度为0q.push(src);while (!q.empty()){int u = q.front();q.pop();for (int i = head[u]; i != -1; i = edge[i].next){int v = edge[i].to;if (edge[i].cap > 0 && level[v] == -1) //该边未访问过且该边有流量{level[v] = level[u] + 1;q.push(v);}}}return level[des] != -1; //返回是否还存在连通路}int dfs(int u, int f){if (u == des)return f;int tem;for (int i = head[u]; i != -1; i = edge[i].next){int v = edge[i].to;if (edge[i].cap > 0 && level[v] == level[u] + 1){tem = dfs(v, min(f, edge[i].cap));if (tem > 0){edge[i].cap -= tem;edge[i ^ 1].cap += tem; //更新边与反向边的流量return tem;}}}level[u] = -1; //表示已经走过该点return 0;}int Dinic(){int ans = 0, tem;while (bfs())//如果还存在连通路{while (tem = dfs(src, INF)) //一直试图寻找增广路{ans += tem;}}return ans;}int main(){int n, m, s, t;while (cin >> n >> m >> s >> t){cnt = 0;memset(head, -1, sizeof head);int cost;for (int i = 1; i <= n; i++){cin >> cost;addedge(i, i + n, cost);}for (int i = 0; i < m; i++){int u, v;cin >> u >> v;addedge(u + n, v, INF);addedge(v + n, u, INF);}src = s;des = t + n;cout << Dinic() << endl;}return 0;}

随着英语竞赛结束,,准备要搞一波最大流了。。。同时祝愿我们省赛刁刁的。。。另外楼主写了一篇微小说(其实是英语作业)。。。欢迎大家捧场,见另一篇博客。

0 0
原创粉丝点击