UVA 1658 Admiral(拆点+费用流)

题意:给出一个n个点m条边的有向加权图,求1-N的两条不相交路径的权值和最小

思路: 把2到n-1的每个结点拆点,中间连一条容量为1的边,然后求1到n的流量为2的最小费用流就好了


#include<bits/stdc++.h>
using namespace std;
#define LL long long
const int maxn = 2005;
#define INF 1e9
struct Edge
{
	int from,to,cap,flow,cost;
	Edge(int u,int v,int c,int f,int w):from(u),to(v),cap(c),flow(f),cost(w){}
};
struct MCMF
{
	int n,m;
	vector<Edge>edges;
	vector<int>G[maxn];
	int inq[maxn];
	int d[maxn];
	int p[maxn];
	int a[maxn];
	void init(int n)
	{
		this->n=n;
		for (int i =0;i<n;i++)
			G[i].clear();
		edges.clear();
	}
	void AddEdge(int from,int to,int cap,int cost)
	{
		edges.push_back(Edge(from,to,cap,0,cost));
		edges.push_back(Edge(to,from,0,0,-cost));
		m = edges.size();
		G[from].push_back(m-2);
		G[to].push_back(m-1);
	}
	bool BellmanFord(int s,int t,int &flow,LL &cost)
	{
        for (int i = 0;i<n;i++)
			d[i]=INF;
		memset(inq,0,sizeof(inq));
		d[s]=0;
		inq[s]=1;
		p[s]=0;
		a[s]=INF;
		queue<int>q;
		q.push(s);
		while (!q.empty())
		{
			int u = q.front();
			q.pop();
			inq[u]=0;
			for (int i =0;i<G[u].size();i++){
				Edge&e = edges[G[u][i]];
				if (e.cap>e.flow&&d[e.to]>d[u]+e.cost){
					d[e.to]=d[u]+e.cost;
					p[e.to]=G[u][i];
					a[e.to]=min(a[u],e.cap-e.flow);
					if (!inq[e.to])
					{
						q.push(e.to);
						inq[e.to]=1;
					}
				}
			}
		}
		if (d[t]==INF)
			return false;
		flow+=a[t];
		cost+=(LL)d[t]*(LL)a[t];
		for (int u = t;u!=s;u=edges[p[u]].from){
			edges[p[u]].flow+=a[t];
			edges[p[u]^1].flow-=a[t];
		}
		return true;
	}
	int MincostMaxflow(int s,int t)
	{
		int flow = 0;
		LL cost = 0;
		while (BellmanFord(s,t,flow,cost));
		return cost;
	}
}mc;
int main()
{
    int n,m;
	while (scanf("%d%d",&n,&m)!=EOF)
	{
		mc.init(2*n+2);
		for (int i = 2;i<n;i++)
			mc.AddEdge(i,i+n,1,0);
		mc.AddEdge(1,n+1,2,0);
		mc.AddEdge(n,n*2,2,0);
		for (int i = 1;i<=m;i++)
		{
			int u,v,w;
			scanf("%d%d%d",&u,&v,&w);
			mc.AddEdge(u+n,v,1,w);
		}
		printf("%d\n",mc.MincostMaxflow(1,2*n));
	}
}



Michiel Adriaenszoon de Ruyter is the most famous admiral in Dutch history and is well known for his
role in the Anglo-Dutch Wars of the 17th century. De Ruyter personally commanded a flagship and
issued commands to allied warships during naval battles.
In De Ruyter’s time, graph theory had just been invented and the admiral used it to his great
advantage in planning his naval battles. Waypoints at sea are represented by vertices, and possible
passages from one waypoint to another are represented as directed edges. Given any two waypoints
W1 and W2 , there is at most one passage W1 → W2 . Each directed edge is marked with the number
of cannonballs that need to be fired in order to safely move a ship along that edge, sinking the enemy
ships encountered along the way.
One of De Ruyter’s most successful tactics was the De Ruyter Manoeuvre. Here, two warships start
at the same waypoint, and split up and fight their way through the enemy fleet, joining up again at a
destination waypoint. The manoeuvre prescribes that the two warships take disjunct routes, meaning
that they must not visit the same waypoint (other than the start and end-points), or use the same
passage during the battle.
Being Dutch, Admiral De Ruyter did not like to waste money; in 17th century naval warfare, this
meant firing as few expensive cannonballs as possible.
Figure 1: A particular instance of De Ruyter’s tactic, visualised as a graph. Two ships (‘red’
and ‘blue’) move from a shared starting point (1) to a shared endpoint (6). The red ship’s route is
1 → 3 → 6 (firing 33 canonballs along the way); the blue ship’s route is 1 → 2 → 5 → 4 → 6 (firing
53 canonballs along the way). In total, 86 canonballs are fired during the manoeuvre. Except for the
start- and end-point, no vertices or edges are visited by both ships.
Input
For each test case, the input consists of:
• A line containing two integers v (3 ≤ v ≤ 1000) and e (3 ≤ e ≤ 10000), the number of waypoints
and passages, respectively.
• Then, e lines follow: for each passage, a line containing three integers:
1. ai (1 ≤ ai ≤ v), the starting-point of a passage, which is represented by a waypoint;
2. bi (1 ≤ bi ≤ v) and (ai ̸= bi), the end-point of a passage, which is represented by a waypoint.
All passages are directed passages;
3. ci (1 ≤ ci ≤ 100), the number of cannonballs that are fired when travelling along this passage.
The starting waypoint is 1 and the destination waypoint is v. There are always at least two disjunct
routes from waypoint 1 to waypoint v.
Output
For each test case, the output consists of a single positive integer: the smallest possible sum of cannonballs
fired by both ships when reaching the destination waypoint.

Sample Input
6 11
1 2 23
1 3 12
1 4 99
2 5 17
2 6 73
3 5 3
3 6 21
4 6 8
5 2 33
5 4 5
6 5 20
Sample Output
86

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值