当前位置: 首页 > 工具软件 > Delivery​ > 使用案例 >

Apple Delivery

后烨煜
2023-12-01

链接:https://ac.nowcoder.com/acm/problem/24755
来源:牛客网
 

题目描述

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 P1iP1_iP1i​ (1 <= P1iP1_iP1i​ <= P) and P2iP2_iP2i​ (1 <= P2iP2_iP2i​ <= P) with a distance between them of DiD_iDi​. The sum of all the distances DiD_iDi​ 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:
                3        2       2
           [1]-----[2]------[3]-----[4]
             \     / \              /
             7\   /4  \3           /2
               \ /     \          /
               [5]-----[6]------[7]
                    1       2
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.

 

输入描述:

* 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: P1i,P2i,DiP1_i, P2_i, D_iP1i​,P2i​,Di​

输出描述:

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

示例1

输入

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

输出

12

细节:1.必须使用优先队列+堆

#include <stdio.h>
#include <queue>
#include <map>
#include <vector>
#include <algorithm>
using namespace std;
const int maxv=100010;
const int INF=1000000000;
struct node{
	int v;
	int cost;
	node(int _v,int _cost):v(_v),cost(_cost){}//初始化 
};
typedef pair<int,int> P;//距离,顶点 
vector<node> Adj[maxv];
int d[maxv];
int n,m,p1,p2,d1;
void djs(int s){
	priority_queue<P,vector<P>,greater<P> > q;
	fill(d,d+maxv,INF);//初始化 
	d[s]=0;
	q.push(P(0,s));
	while(!q.empty()){
		P top=q.top();
		q.pop();
		int u=top.second;
		if(top.first>d[u]){//该点需要更新 
			continue;
		}
		for(int i=0;i<Adj[u].size();i++){
			node v=Adj[u][i];
			if(d[u]+v.cost<d[v.v]){
				d[v.v]=d[u]+v.cost;
				q.push(P(d[v.v],v.v)); 
			}
		} 
	}
}
int main(){
	int u,v,w;
	scanf("%d%d%d%d%d",&m,&n,&p1,&p2,&d1);
	for(int i=0;i<m;i++){//制表 
		scanf("%d%d%d",&u,&v,&w);
		Adj[u].push_back(node(v,w));
		Adj[v].push_back(node(u,w));
	}
	djs(p1);//p1为起点时
	int dis_1=d[p2];//(5->1)
	int dis_2=d[d1];//(5->4)
	djs(p2);//p2为起点时 
	int dis_3=d[d1];//(1->4)或者(4->1) 
	int t=min(dis_1,dis_2)+dis_3; 
	printf("%d\n",t);
	return 0;
}

 

 类似资料:

相关阅读

相关文章

相关问答