1. 程式人生 > >所有節點對最短路徑 超時 優先佇列 + dijkstra + 遍歷前驅子圖

所有節點對最短路徑 超時 優先佇列 + dijkstra + 遍歷前驅子圖

Long long ago,there is a knight called JayYe.He lives in a small country.This country is made up of n cities connected by n-1 roads(that means it's a tree).The king wants to reward JayYe because he beats the devil and save the princess.The king decide to give JayYe exactly K cities as his daughter's dowry. Here comes the question.Although JayYe beats the devil,his knee was injured.So he doesn't want to move too much,he wants his citys as close as possible,that is, JayYe wants the expected distance of his cities as small as possible. The expected distance is defined as the expected distance between node u and node v,both u and v are randomly choose from JayYe's K cities equiprobably(that means first choose u randomly from JayYe’s K cities,then choose v randomly from JayYe’s K cities,so the case u equals to v is possible). Suppose you are the king,please determine the K cities so that JayYe is happy. Because the author is lazy,you only need tell me the minimum expect distance.