2017 icpc 青岛站 K. Our Journey of Xian Ends 最小费用流

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/ccsu_cat/article/details/83351328

K. Our Journey of Xian Ends

题意:这英文题真是比六级阅读还难读,给你一个无向图,求西安-->上海-->青岛-->上海的最短路,每个城市有一个机场,但是上海有两个机场 虹桥机场和浦东机场,上海这两个机场可以互达(你可以从西安到浦东,然后再从虹桥到青岛,距离为0),每个机场只能下飞机和上飞机一次,且最后必须要到达上海的浦东以及过程中不能从浦东出发去任何地方。

思路:由于是无向图,肯定要将每个点拆成入点和出点,普通城市入点到出点费用为0,流量为1,我们发现,上海的虹桥必须要走两次,青岛也是走两次(进一次,出一次),西安和浦东只能走一次(进一次),上海-->青岛 肯定是虹桥到青岛,我们可以理解为,从青岛和西安两条路线走向虹桥,然后青岛再走一条路线到浦东,求总距离最小,思路清晰了,起点到西安流量为1,到青岛,流量为2,虹桥到终点,流量为2,浦东到终点,流量为1,这些点的入点到出点的流量也是一样,然后完成建图就ac。

#include<cstdio>
#include<cstring>
#include<map>
#include<vector>
#include<string>
#include<queue>
#include<algorithm>
using namespace std;
const int maxn=20005,inf=1e8;
map<string,int>mp;
struct Edge
{
	int from,to,cap,flow,cost;
	Edge(int a,int b,int c,int d,int e)
	{
		from=a,to=b,cap=c,flow=d,cost=e;
	}
};
struct MCMF{
	int n,m,s,t;
	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 add(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 bellman(int s,int t,int& flow,int& cost)
	{
		for(int i=0;i<=n;i++)d[i]=inf,inq[i]=0;
		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+=d[t]*a[t];
		int u=t;
		while(u!=s)
		{
			edges[p[u]].flow+=a[t];
			edges[p[u]^1].flow-=a[t];
			u=edges[p[u]].from;
		}
		return true;
	}
	int mincost(int s,int t)
	{
		int flow=0,cost=0;
		while(bellman(s,t,flow,cost));
		if(flow!=3)return -1;
		return cost;
	}
}solve;
char s1[4][10]={"Xian","Qingdao","Hongqiao","Pudong"};
struct node
{
	char s[15],t[15];
	int w;
}a[maxn];
int main()
{
	int T;
	scanf("%d",&T);
	while(T--)
	{
		mp.clear();
		for(int i=0;i<4;i++)mp[s1[i]]=i+1;
		int n=4,m,w;
		scanf("%d",&m);
		for(int i=1;i<=m;i++)
		{
			scanf("%s%s%d",a[i].s,a[i].t,&a[i].w);
			if(!mp[a[i].s])mp[a[i].s]=++n;
			if(!mp[a[i].t])mp[a[i].t]=++n;
		}
		int tot=n;
		n+=n;
		for(int i=1;i<=m;i++)
		{
			int u=mp[a[i].s];
			int v=mp[a[i].t];
			solve.add(u+tot,v,3,a[i].w);
			solve.add(v+tot,u,3,a[i].w);
		}
		int s=++n;
		int t=++n;
		solve.add(s,1,1,0);solve.add(1,1+tot,1,0);
		solve.add(s,2,2,0);solve.add(2,2+tot,2,0);
		solve.add(3+tot,t,2,0);solve.add(3,3+tot,2,0);
		solve.add(4+tot,t,1,0);solve.add(4,4+tot,1,0);
		for(int i=5;i<=tot;i++)solve.add(i,i+tot,1,0);
		solve.n=n;
		printf("%d\n",solve.mincost(s,t));
		solve.init(n);
	}
}

猜你喜欢

转载自blog.csdn.net/ccsu_cat/article/details/83351328