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

这道题也是一道SAP最大流,还是套模板就ok了

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cmath>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <vector>
#include <string.h>
#include <string>
typedef long long ll;
#define inf 0x3f3f3f3f
using namespace std;
#define maxn 5000
#define maxm 2500000

struct node {
	int from, to, next, cap;
}edge[maxm];
int tol;
int dep[maxn];
int gap[maxn];
int head[maxn];
int n;

void init() {
	tol = 0;
	memset(head, -1, sizeof(head));
}

void addedge(int u, int v, int w) {
	edge[tol].from = u;
	edge[tol].to = v;
	edge[tol].cap = w;
	edge[tol].next = head[u];
	head[u] = tol++;
	edge[tol].from = v;
	edge[tol].to = u;
	edge[tol].cap = 0;
	edge[tol].next = head[v];
	head[v] = tol++;
}
void BFS(int bg, int ed){
	memset(dep, -1, sizeof(dep));
	memset(gap, 0, sizeof(gap));
	gap[0] = 1;
	int que[maxn];
	int front, rear;
	front = rear = 0;
	dep[ed] = 0;
	que[rear++] = ed;
	while (front != rear){
		int u = que[front++];
		if (front == maxn)front = 0;
		for (int i = head[u]; i != -1; i = edge[i].next)	{
			int v = edge[i].to;
			if (edge[i].cap != 0 || dep[v] != -1)continue;
			que[rear++] = v;
			if (rear == maxn)rear = 0;
			dep[v] = dep[u] + 1;
			++gap[dep[v]];
		}
	}
}
int SAP(int bg, int ed){
	int res = 0;
	BFS(bg, ed);
	int cur[maxn];
	int S[maxn];
	int top = 0;
	memcpy(cur, head, sizeof(head));
	int u = bg;
	int i;
	while (dep[bg] < n){
		if (u == ed){
			int temp = inf;
			int inser;
			for (i = 0; i < top; i++)
				if (temp > edge[S[i]].cap){
					temp = edge[S[i]].cap;
					inser = i;
				}
			for (i = 0; i < top; i++){
				edge[S[i]].cap -= temp;
				edge[S[i] ^ 1].cap += temp;
			}
			res += temp;
			top = inser;
			u = edge[S[top]].from;
		}
		if (u != ed && gap[dep[u] - 1] == 0)//出现断层,无增广路
			break;
		for (i = cur[u]; i != -1; i = edge[i].next)
			if (edge[i].cap != 0 && dep[u] == dep[edge[i].to] + 1)
				break;
		if (i != -1){
			cur[u] = i;
			S[top++] = i;
			u = edge[i].to;
		}
		else{
			int mn = n;
			for (i = head[u]; i != -1; i = edge[i].next){
				if (edge[i].cap == 0)continue;
				if (mn > dep[edge[i].to]){
					mn = dep[edge[i].to];
					cur[u] = i;
				}
			}
			--gap[dep[u]];
			dep[u] = mn + 1;
			++gap[dep[u]];
			if (u != bg)
				u = edge[S[--top]].from;
		}

	}
	return res;
}

int main(){
	int N, M;
	int u, v;
	int bg;
	int ed;
	while (scanf("%d%d", &N, &M) != EOF){
		init();
		scanf("%d%d", &bg, &ed);
		bg = 2 * bg - 1;
		ed = 2 * ed;
		n = 2 * N;
		for (int i = 1; i <= N; i++){
			scanf("%d", &u);
			addedge(2 * i - 1, 2 * i, u);
			addedge(2 * i, 2 * i - 1, u);
		}
		while (M--){
			scanf("%d%d", &u, &v);
			addedge(2 * u, 2 * v - 1, inf);
			addedge(2 * v, 2 * u - 1, inf);//这里一定要注意
		}
		printf("%d\n", SAP(bg, ed));
	}
	return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值