BZOJ 2100 Apple Delivery

Description

Bessie has two crisp red apples to deliver to two of her friends in the herd. Of course, she travels the C (1 <= C <= 200,000) cowpaths which are arranged as the usual graph which connects P (1 <= P <= 100,000) pastures conveniently numbered from 1..P: no cowpath leads from a pasture to itself, cowpaths are bidirectional, each cowpath has an associated distance, and, best of all, it is always possible to get from any pasture to any other pasture. Each cowpath connects two differing pastures P1_i (1 <= P1_i <= P) and P2_i (1 <= P2_i <= P) with a distance between them of D_i. The sum of all the distances D_i does not exceed 2,000,000,000. What is the minimum total distance Bessie must travel to deliver both apples by starting at pasture PB (1 <= PB <= P) and visiting pastures PA1 (1 <= PA1 <= P) and PA2 (1 <= PA2 <= P) in any order. All three of these pastures are distinct, of course. Consider this map of bracketed pasture numbers and cowpaths with distances:  If Bessie starts at pasture [5] and delivers apples to pastures [1] and [4], her best path is: 5 -> 6-> 7 -> 4* -> 3 -> 2 -> 1* with a total distance of 12.

一张P个点的无向图,C条正权路。
CLJ要从Pb点(家)出发,既要去Pa1点NOI赛场拿金牌,也要去Pa2点CMO赛场拿金牌。(途中不必回家)
可以先去NOI,也可以先去CMO。
当然神犇CLJ肯定会使总路程最小,输出最小值。

Input

* Line 1: Line 1 contains five space-separated integers: C, P, PB, PA1, and PA2 * Lines 2..C+1: Line i+1 describes cowpath i by naming two pastures it connects and the distance between them: P1_i, P2_i, D_i

Output

* Line 1: The shortest distance Bessie must travel to deliver both apples

Sample Input

9 7 5 1 4
5 1 7
6 7 2
4 7 2
5 6 1
5 2 4
4 3 2
1 2 3
3 2 2
2 6 3



Sample Output

12

一道要求经过两个点的最短路。实际就是从起点到p1或p2的最短路取min加上p1到p2的距离即可。脑筋急转弯(说什么呢人家叫观察)。


#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<iostream>
#include<cmath>
#include<queue>
#include<cstdlib>
#define pa pair<long long,int>
#define INF 1e9+7

using namespace std;

const int MAXN=100010;
const int MAXM=200010;
struct edge
{
	int to,val,next;	
};
edge e[MAXM*2];
int point[MAXN],cnt;
priority_queue< pa , vector<pa> , greater<pa> > q;
bool visit[MAXN];
int dis[MAXN],p; 
void addedge(int u,int v,int w)
{
	e[++cnt].next=point[u];
	e[cnt].to=v;
	e[cnt].val=w;
	point[u]=cnt;
}

void Dijkstra(int s)
{
	memset(visit,0,sizeof(visit));
	for (int i=1;i<=p;i++)
	{
		dis[i]=INF;
	}
	dis[s]=0;
	q.push(make_pair(0,s));
	while (!q.empty())
	{
		int x=q.top().second;
		q.pop();
		if (visit[x])
		{
			continue;
		}
		visit[x]=1;
		for (int i=point[x];i;i=e[i].next)
		{
			if (dis[e[i].to]>dis[x]+e[i].val)
			{
				dis[e[i].to]=dis[x]+e[i].val;
				q.push(make_pair(dis[e[i].to],e[i].to));
			}
		}
	}
}
int main()
{
	int c,s,t1,t2;
	scanf("%d%d%d%d%d",&c,&p,&s,&t1,&t2);
	for (int i=1;i<=c;i++)
	{
		int u,v,w;
		scanf("%d%d%d",&u,&v,&w);
		addedge(u,v,w);
		addedge(v,u,w);
	}
	int ans1=0,ans2=0;
	Dijkstra(s);
	ans1+=dis[t1];
	ans2+=dis[t2];
	Dijkstra(t2);
	ans2+=dis[t1];
	ans1+=dis[t1];
	cout<<min(ans1,ans2);
    return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值