FZU 2243 Daxia like uber

 Problem Description

daxia兼职开uber,现在接到一个两人拼车单.已知如下信息:

1. 地图上共有n个点,m条有向行驶通道.

2. daxia位于点s;

3. 客人一位于点x1,目的地为y1;

4. 客人二位于点x2,目的地为y2.

请帮daxia计算送完两个乘客的最短行驶路程.

 Input

测试包含多组数据,每组数据第一行包含两个整数:n(1<=n<=1000),m(1<=m<=100000).

第二行包含五个整数:s,x1,y1,x2,y2(1<=s,x1,y1,x2,y2<=n).

接下来包含m行,每行三个整数:u,v,d(1<=u,v<=n,1<=d<=100).

 Output

每组数据输出一行一个整数表示送完两个乘客的最短行驶路程,数据保证肯定能送到.

 Sample Input

4 84 1 2 2 31 2 41 3 21 4 72 1 12 3 53 1 23 4 44 2 3

 Sample Output

11

 Hint

样例接送路线:(4)->2->(1)->(2)->1->(3)


五次最短路 

显然,此题是一道最短路问题,但问题的关键是,在整个行驶过程中,需要出现x1->y1,x2->y2这段路
也就是说只要有出现x1->y1,x2->y2这段路,其他的可以随便组合
那么行驶过程就有以下六种情况:
①s->x1->y1->x2->y2
②s->x1->x2->y1->y2
③s->x1->x2->y2->y1
④s->x2->y2->x1->y1
⑤s->x2->x1->y1->y2
⑥s->x2->x1->y2->y1
"a->b"表示从a到b的最短路
显然,要完成送客任务,只有这6种方案,我们只需要找一种路程最短的就可以了
而上述6种方案,只需要求5次最短路就能求得需要的最短路
因为只有5个不同起点,对于单源最短路,1次可以求得1个,故为5次
1. s->x1 s->x2    
2. x1->y1 x1->x2 x1->y2
3. x2->y2 x2->x1 x2->y1
4. y1->x2 y1->y2
5. y2->y1 y2->x
摘自: http://blog.csdn.net/queuelovestack/article/details/52273761


#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <queue>
#include <vector>
#include <cmath>
#include <stack>
#include <string>
#include <sstream>
#include <map>
#include <set>
#define pi acos(-1.0)
#define LL long long
#define ULL unsigned long long
#define inf 0x3f3f3f3f
#define INF 1e18
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define debug(a) printf("---%d---\n", a)
#define mem0(a) memset(a, 0, sizeof(a))
#define memi(a) memset(a, inf, sizeof(a))
#define mem1(a) memset(a, -1, sizeof(a))
using namespace std;
typedef pair<int, int> P;
const double eps = 1e-10;
const int maxn = 1e6 + 5;
const int mod = 1e8;

struct edge{
	int v, w;
	edge();
	edge(int v_, int w_){
		v = v_, w = w_;
	}
};
vector<edge>G[1005];
int dis[1005][1005], n, m;
void Dijkstra(int s)
{
	priority_queue<P, vector<P>, greater<P> > q;
	dis[s][s] = 0;
	q.push(P(dis[s][s], s));
	while (!q.empty())
	{
		P cur = q.top(); q.pop();
		int u = cur.second;
		int w = cur.first;
		if (dis[s][u] < w) continue;
		for (int i = 0; i < G[u].size(); i++){
			edge e = G[u][i];
			int v = e.v;
			if (dis[s][v] > dis[s][u] + e.w){
				dis[s][v] = dis[s][u] + e.w;
				q.push(P(dis[s][v], v));
			}
		}
	}
}
int main(void)
{
//	freopen("C:\\Users\\wave\\Desktop\\NULL.exe\\NULL\\in.txt","r", stdin);	
	int i, j, u, v, w;
	int x1, y1, x2, y2, s;
	while (cin >> n >> m){
		memset(dis, inf, sizeof(dis));
		memset(G, 0, sizeof(G));
		cin >> s >> x1 >> y1 >> x2 >> y2;
		for (i = 1; i <= m; i++){
			cin >> u >> v >> w;
			G[u].push_back(edge(v, w)); 
		}	
		Dijkstra(s);
		Dijkstra(x1);
		Dijkstra(x2);
		Dijkstra(y1);
		Dijkstra(y2);
		int ans = inf;
		ans = min(ans, dis[s][x1] + dis[x1][y1] + dis[y1][x2] + dis[x2][y2]);
		ans = min(ans, dis[s][x1] + dis[x1][x2] + dis[x2][y1] + dis[y1][y2]);
		ans = min(ans, dis[s][x1] + dis[x1][x2] + dis[x2][y2] + dis[y2][y1]);
		ans = min(ans, dis[s][x2] + dis[x2][y2] + dis[y2][x1] + dis[x1][y1]);
		ans = min(ans, dis[s][x2] + dis[x2][x1] + dis[x1][y1] + dis[y1][y2]);
		ans = min(ans, dis[s][x2] + dis[x2][x1] + dis[x1][y2] + dis[y2][y1]);
		cout << ans << endl;
	}
	
	return 0;
}





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值