链接:https://ac.nowcoder.com/acm/problem/24819
来源:牛客网
题目描述
Bessie, always wishing to optimize her life, has realized that she really enjoys visiting F (1 <= F <= P) favorite pastures Fi of the P (1 <= P <= 500; 1 <= Fi <= P) total pastures (conveniently
numbered 1..P) that compose Farmer John's holdings.
Bessie knows that she can navigate the C (1 <= C <= 8,000) bidirectional cowpaths (conveniently numbered 1..C) that connect various pastures to travel to any pasture on the entire farm. Associated with each path Pi is a time Ti (1 <= Ti <= 892) to traverse that path (in either direction) and two path endpoints ai and bi (1 <= ai <= P; 1 <= bi <= P).
Bessie wants to find the number of the best pasture to sleep in so that when she awakes, the average time to travel to any of her F favorite pastures is minimized.
By way of example, consider a farm laid out as the map below shows, where *'d pasture numbers are favorites. The bracketed numbers are times to traverse the cowpaths. 1*--[4]--2--[2]--3 | | [3] [4] | | 4--[3]--5--[1]---6---[6]---7--[7]--8* | | | | [3] [2] [1] [3] | | | | 13* 9--[3]--10*--[1]--11*--[3]--12* The following table shows distances for potential 'best place' of pastures 4, 5, 6, 7, 9, 10, 11, and 12: * * * * * * Favorites * * * * * * Potential Pasture Pasture Pasture Pasture Pasture Pasture Average Best Pasture 1 8 10 11 12 13 Distance ------------ -- -- -- -- -- -- ----------- 4 7 16 5 6 9 3 46/6 = 7.67 5 10 13 2 3 6 6 40/6 = 6.67 6 11 12 1 2 5 7 38/6 = 6.33 7 16 7 4 3 6 12 48/6 = 8.00 9 12 14 3 4 7 8 48/6 = 8.00 10 12 11 0 1 4 8 36/6 = 6.00 ** BEST 11 13 10 1 0 3 9 36/6 = 6.00 12 16 13 4 3 0 12 48/6 = 8.00 Thus, presuming these choices were the best ones (a program would have to check all of them somehow), the best place to sleep is pasture 10.
输入描述:
* Line 1: Three space-separated integers: P, F, and C * Lines 2..F+1: Line i+2 contains a single integer: Fi * Lines F+2..C+F+1: Line i+F+1 describes cowpath i with three space-separated integers: ai, bi, and Ti
输出描述:
* Line 1: A single line with a single integer that is the best pasture in which to sleep. If more than one pasture is best, choose the smallest one.
示例1
输入
13 6 15 11 13 10 12 8 1 2 4 3 7 11 3 10 11 1 4 13 3 9 10 3 2 3 2 3 5 4 5 9 2 6 7 6 5 6 1 1 2 4 4 5 3 11 12 3 6 10 1 7 8 7
输出
10
#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 dis;
node(int _v,int _dis):v( _v),dis( _dis){}
};
int n,f,m;
vector<node> Adj[maxv];
int d[maxv],posion[maxv];
typedef pair<int,int> P;
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.dis<d[v.v]){
d[v.v]=d[u]+v.dis;
q.push(P(d[v.v],v.v));
}
}
}
}
int main(){
fill(posion,posion+maxv,INF);
int u,v,w;
scanf("%d%d%d",&n,&f,&m);
for(int i=0;i<f;i++){//休息点
scanf("%d",&posion[i]);
}
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));
}
queue<int> q1;
for(int i=1;i<=n;i++){//所有点
q1.push(i);
}
int m=1000000000,p=-1;
while(!q1.empty()){
int pos=q1.front();
q1.pop();
djs(pos);//最短距离
int sum=0;
for(int i=0;i<f;i++){
sum+=d[posion[i]];
}
int eva=sum;//平均最短
if(m>eva){//记录
m=eva;
p=pos;
}
}
printf("%d\n",p);
return 0;
}