HDU 4411 Arrest(费用流)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4411


题意:有N + 1个城市,M条无向带权边,警局在0号点,其他N个点都有犯罪团伙需要抓,最多可以派出K支队伍去抓捕,到了某个城市可以选择抓或不抓,因为在抓第i个团伙前必须先抓住i - 1团伙,对于每支队伍在完成最终抓捕后需要回到警局,问抓捕所有团伙最少要走的总距离


思路:考虑题目所给的一些限制:

1.所有点都要被遍历,且抓捕只能发生一次,那就对于点i将其拆成两个点,然后i到i'连边容量为1,费用为-INF,以保证当前点一定发生过一次抓捕


2.K支队伍不一定用完,对于0号点连边到汇点,容量为K,费用为0,将多余的流量送入汇点


3.抓捕i前要先抓i - 1,对于i'点连边到j点(j > i),容量为1, 费用为两点间最短距离


再构建源点汇点,相应连边即可



#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <vector>
#include <utility>
#include <cmath>
#include <queue>
#include <set>
#include <map>
#include <climits>
#include <functional>
#include <deque>
#include <ctime>

#define lson l, mid, rt << 1
#define rson mid + 1, r, rt << 1 | 1
#pragma comment(linker, "/STACK:102400000,102400000")

using namespace std;

const int MAXN = 500;
const int MAXM = 1001000;
const int INF = 1e7;

typedef long long ll;

struct Edge
{
	int to, next, cap, flow, cost;
} edge[MAXM];

int head[MAXN], tol;
int pre[MAXN], dis[MAXN];
bool vis[MAXN];
int N;//节点总个数,节点编号从0~N-1

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

void addedge(int u, int v, int cap, int cost)
{
	edge[tol].to = v;
	edge[tol].cap = cap;
	edge[tol].cost = cost;
	edge[tol].flow = 0;
	edge[tol].next = head[u];
	head[u] = tol++;
	edge[tol].to = u;
	edge[tol].cap = 0;
	edge[tol].cost = -cost;
	edge[tol].flow = 0;
	edge[tol].next = head[v];
	head[v] = tol++;
}

bool spfa(int s, int t)
{
	queue<int>q;
	for (int i = 0; i < N; i++)
	{
		dis[i] = INF;
		vis[i] = false;
		pre[i] = -1;
	}
	dis[s] = 0;
	vis[s] = true;
	q.push(s);
	while (!q.empty())
	{
		int u = q.front();
		q.pop();
		vis[u] = false;
		for (int i = head[u]; i != -1; i = edge[i].next)
		{
			int v = edge[i].to;
			if (edge[i].cap > edge[i].flow &&
			        dis[v] > dis[u] + edge[i].cost )
			{
				dis[v] = dis[u] + edge[i].cost;
				pre[v] = i;
				if (!vis[v])
				{
					vis[v] = true;
					q.push(v);
				}
			}
		}
	}
	if (pre[t] == -1) return false;
	else return true;
}

//返回的是最大流,cost存的是最小费用
int minCostMaxflow(int s, int t, int &cost)
{
	int flow = 0;
	cost = 0;
	while (spfa(s, t))
	{
		int Min = INF;
		for (int i = pre[t]; i != -1; i = pre[edge[i ^ 1].to])
		{
			if (Min > edge[i].cap - edge[i].flow)
				Min = edge[i].cap - edge[i].flow;
		}
		for (int i = pre[t]; i != -1; i = pre[edge[i ^ 1].to])
		{
			edge[i].flow += Min;
			edge[i ^ 1].flow -= Min;
			cost += edge[i].cost * Min;
		}
		flow += Min;
	}
	return flow;
}

int g[MAXN][MAXN];

void floyd(int n)
{
	for (int k = 0; k <= n; k++)
		for (int i = 0; i <= n; i++)
			for (int j = 0; j <= n; j++)
				g[i][j] = min(g[i][j], g[i][k] + g[k][j]);
}

int main()
{
	int n, m, k;
	while (~scanf("%d%d%d", &n, &m, &k) && (n + m + k))
	{
		int s = n * 2 + 1, t = s + 1;
		init(t + 1);

		for (int i = 0; i <= n; i++)
			for (int j = 0; j <= n; j++)
				if (i == j) g[i][j] = 0;
				else
					g[i][j] = INF;

		for (int i = 0; i < m; i++)
		{
			int u, v, w;
			scanf("%d%d%d", &u, &v, &w);
			g[u][v] = g[v][u] = min(g[u][v], w);
		}

		floyd(n);

		// for (int i = 0; i < n; i++)
		// {	for (int j = 0; j < n; j++)
		// 		printf("%d ", g[i][j]);
		// 	printf("\n");
		// }

		addedge(s, 0, k, 0);
		addedge(0, t, k, 0);
		for (int i = 1; i <= n; i++)
		{
			addedge(0, i, 1, g[0][i]);
			addedge(i, i + n, 1, -INF);
			addedge(i + n, t, 1, g[i][0]);
		}

		for (int i = 1; i <= n; i++)
			for (int j = i + 1; j <= n; j++)
				addedge(i + n, j, 1, g[i][j]);

		int ans;
		minCostMaxflow(s, t, ans);
		cout << ans + n * INF << endl;
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值