Admiral UVA - 1658

将题目抽象成为最小费用网络流的问题,其中对于每个中间节点i都分成两个节点i和j,同时建立一条从i到j的边,这条边的容量为1,花费为0,同时对于输入的每条边,都建立对应的边,边的容量也为1,同时花费为相应的权值,那么问题就转化为求出从开始点到终点的流量为2的最小花费,按照紫书的算法,结合BellmanFloyd即可求出结果,但是要注意一点我们要将流量的上限设置为2,在具体实现的时候注意细节方面的改动即可,具体实现见如下代码:

#include<iostream>
#include<vector>
#include<string>
#include<set>
#include<stack>
#include<queue>
#include<map>
#include<algorithm>
#include<cmath>
#include<iomanip>
#include<cstring>
#include<sstream>
#include<cstdio>
#include<deque>
#include<functional>
using namespace std;

class Edge{
public:
	int from, to, cap, flow, cost;
	Edge(int fr, int t, int ca, int fl, int c):from(fr),to(t),cap(ca),flow(fl),cost(c){}
};

class Solve{
public:
	int v, e;
	vector<Edge> edge;
	vector<int> G[2005];
	int flow_limit = 2;
	const int Inf = 0x3f3f3f3f;
	void addEdge(int from,int to,int cap,int cost){
		edge.push_back(Edge(from, to, cap, 0, cost));
		edge.push_back(Edge(to, from, 0, 0, -cost));
		int m = edge.size() - 1;
		G[from].push_back(m - 1);
		G[to].push_back(m);
	}

	bool BellmanFord(int start,int end,int& flow,int& cost){
		int inq[2005];
		int parent[2005];
		int price[2005];
		int Flow[2005];
		Flow[0] = Inf;
		memset(price, Inf, sizeof(price));
		price[0] = 0;
		memset(inq, 0, sizeof(inq));
		inq[start] = 1;
		queue<int> q;
		q.push(start);
		while (!q.empty()){
			int id = q.front();
			q.pop();
			inq[id] = 0;
			for (int i = 0; i < G[id].size(); i++){
				int ide = G[id][i];
				int to = edge[ide].to;
				if (edge[ide].cap > edge[ide].flow&&price[to] > price[id] + edge[ide].cost){
					Flow[to] = min(Flow[id], edge[ide].cap - edge[ide].flow);
					price[to] = price[id] + edge[ide].cost;
					parent[to] = ide;
					if (!inq[to]){
						q.push(to);
						inq[to] = 1;
					}
				}
			}
		}
		if (price[end] == Inf) return false;
		if (flow + Flow[end] > flow_limit) Flow[end] = flow_limit - flow;
		cost += Flow[end] * price[end];
		flow += Flow[end];
		for (int i = end; i != start; i = edge[parent[i]].from){
			edge[parent[i]].flow += Flow[end];
			edge[parent[i] ^ 1].flow -= Flow[end];
		}
		return true;
	}

	void MaxFlow(int start,int end){
		int cost = 0, flow=0;
		while (flow < flow_limit&&BellmanFord(start, end, flow, cost));
		cout << cost << endl;
	}

	void Init(){
		edge.clear();
		for (int i = 0; i < 2005; i++) G[i].clear();
		for (int i = 1; i <= v - 2; i++){
			int id2 = i + v - 1;
			addEdge(i, id2, 1, 0);
		}
		for (int i = 0; i < e; i++){
			int a, b, c;
			cin >> a >> b >> c;
			a--;
			if (a != 0 && a != v - 1) a += v - 1;
			b--;
			addEdge(a, b, 1, c);
		}
	}

	void Deal(){
		Init();
		MaxFlow(0,v-1);
	}
};

int main(){
	Solve a;
	while (cin >> a.v >> a.e){
		a.Deal();
	}
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值