【最短路】FOJ 2243 Daxia like uber

题目链接:

  http://acm.fzu.edu.cn/problem.php?pid=2243

题目大意:

  给一张N个点M条边的有向图,从s出发,把在x1的人送到y1,在x2的人送到y2用的最短距离。

题目思路:

  【最短路】

  首先就两个乘客需要送,手写可以得到6种先后次序。 

  s>x1>x2>y1>y2;

  s>x1>y1>x2>y2;

  s>x1>x2>y2>y1;

  第一个和第二个对调可得剩下三种情况。

  所以可以对s,x1,x2,y1,y2求5次单源最短路,最后累加求和取最小。


//
//by coolxxx
//#include<bits/stdc++.h>
#include<iostream>
#include<algorithm>
#include<string>
#include<iomanip>
#include<map>
#include<memory.h>
#include<time.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
//#include<stdbool.h>
#include<math.h>
#define min(a,b) ((a)<(b)?(a):(b))
#define max(a,b) ((a)>(b)?(a):(b))
#define abs(a) ((a)>0?(a):(-(a)))
#define lowbit(a) (a&(-a))
#define sqr(a) ((a)*(a))
#define swap(a,b) ((a)^=(b),(b)^=(a),(a)^=(b))
#define mem(a,b) memset(a,b,sizeof(a))
#define eps (1e-8)
#define J 10
#define mod 1000000007
#define MAX 0x7f7f7f7f
#define PI 3.14159265358979323
#define N 1004
#define M 100004
using namespace std;
typedef long long LL;
int cas,cass;
int n,m,lll,ans;
struct xxx
{
	int next,to,dis;
}a[M];
int c[N][N];
int last[N],d[N],q[N];
bool u[N];
void add(int x,int y,int z)
{
	a[++lll].next=last[x];
	a[lll].to=y;
	a[lll].dis=z;
	last[x]=lll;
}
void spfa(int s)
{
	mem(d,12);mem(u,0);
	int i,l=0,r=1,now,to;
	q[1]=s;d[s]=0;
	while(l!=r)
	{
		now=q[l=(l+1)%N];
		u[now]=0;
		for(i=last[now];i;i=a[i].next)
		{
			to=a[i].to;
			if(d[to]>d[now]+a[i].dis)
			{
				d[to]=d[now]+a[i].dis;
				if(!u[to])
				{
					u[to]=1;
					q[r=(r+1)%N]=to;
				}
			}
		}
	}
}
int main()
{
	#ifndef ONLINE_JUDGE
	freopen("1.txt","r",stdin);
//	freopen("2.txt","w",stdout);
	#endif
	int i,j,k;
	int x,y,z,s,x1,y1,x2,y2;
//	for(scanf("%d",&cas);cas;cas--)
//	for(scanf("%d",&cas),cass=1;cass<=cas;cass++)
//	while(~scanf("%s",s))
	while(~scanf("%d",&n))
	{
		lll=0;
		mem(last,0);mem(c,0);
		scanf("%d",&m);
		scanf("%d%d%d%d%d",&s,&x1,&y1,&x2,&y2);
		for(i=1;i<=m;i++)
		{
			scanf("%d%d%d",&x,&y,&z);
			add(x,y,z);
		}
		spfa(s);
		c[s][x1]=d[x1];c[s][x2]=d[x2];c[s][y1]=d[y1];c[s][y2]=d[y2];
		spfa(x1);
		c[x1][x2]=d[x2];c[x1][y1]=d[y1];c[x1][y2]=d[y2];
		spfa(x2);
		c[x2][x1]=d[x1];c[x2][y2]=d[y2];c[x2][y1]=d[y1];
		spfa(y1);
		c[y1][x2]=d[x2];c[y1][y2]=d[y2];
		spfa(y2);
		c[y2][y1]=d[y1];c[y2][x1]=d[x1];
		ans=c[s][x1]+c[x1][x2]+c[x2][y1]+c[y1][y2];
		ans=min(ans,c[s][x1]+c[x1][y1]+c[y1][x2]+c[x2][y2]);
		ans=min(ans,c[s][x1]+c[x1][x2]+c[x2][y2]+c[y2][y1]);
		ans=min(ans,c[s][x2]+c[x2][x1]+c[x1][y1]+c[y1][y2]);
		ans=min(ans,c[s][x2]+c[x2][y2]+c[y2][x1]+c[x1][y1]);
		ans=min(ans,c[s][x2]+c[x2][x1]+c[x1][y2]+c[y2][y1]);
		printf("%d\n",ans);
	}
	return 0;
}
/*
//

//
*/


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值