Apple Delivery

#include <iostream>
#include <algorithm>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <vector>
#include <queue>

using namespace std;

const int N=200005;
int n,m,p0,p1,p2;
int best1[N],best2[N];

struct Node { int ed,dist;};
bool operator <(const Node &a,const Node &b) { return a.dist>b.dist;}
vector <Node> vec[N];

void dijkstra(int src,int best[])
{
	priority_queue <Node> Q;
	Node tmp={src,0};
	for(int i=1;i<=n;++i) {best[i]=-1;}
	best[src]=0;
	Q.push(tmp);
	while(!Q.empty())
	{
		tmp=Q.top();
		Q.pop();
		int ed=tmp.ed,dist=tmp.dist;
		if(dist!=best[tmp.ed]) continue;
		for(vector <Node>::iterator i=vec[ed].begin(); i!=vec[ed].end();i++)
		{
			if(best[i->ed]==-1 || best[i->ed]>best[ed]+i->dist)
			{
				best[i->ed]=best[ed]+i->dist;
				Node temp={i->ed, best[ed]+i->dist};
				Q.push(temp);
			}
		}
	}
}

int main ()
{
	int u,v,dist;
	scanf("%d%d%d%d%d",&m,&n,&p0,&p1,&p2);
	for(int i=1;i<=m;++i)
	{
		scanf("%d%d%d",&u,&v,&dist);
		Node tmp={v,dist};
		vec[u].push_back(tmp);
		tmp.ed=u;
		vec[v].push_back(tmp);
	}
	dijkstra(p0,best1);
	dijkstra(p1,best2);
	int ans1=best1[p1]+best2[p2];
	int ans2=best1[p2]+best2[p2];
	cout<<(ans1<ans2?ans1:ans2)<<endl;
	return 0;
}

posted on 2011-07-14 22:16  jaxi  阅读(171)  评论(0编辑  收藏  举报