1254 - Prison Break
Time Limit: 2 second(s)
Memory Limit: 32 MB
Michael Scofield has just broken out of the prison. Now he wants to go to a certain city for his next unfinished job. As you are the only programmer on his gang, he asked your help. As you know that the fuel prices vary in the cities, you have to write a code to help Scofield that instructs him where to take the fuel and which path to choose. Assume that his car uses one unit of fuel in one unit of distance. Now he gives you the starting city s where he starts his journey with his car, the destination city t and the capacity of the fuel tank of his car c, the code should find the route that uses the cheapest fuel cost. You can assume that Scofield's car starts with an empty fuel tank.
Input starts with an integer T (≤ 5), denoting the number of test cases.
Each case starts with a line containing two integers n (2 ≤ n ≤ 100) and m (0 ≤ m ≤ 1000) where n denotes the number of cities and m denotes the number of roads. The next line contains n space separated integers, each lies between 1 and 100. The ith integer in this line denotes the fuel price (per unit) in the ith city. Each of the next m lines contains three integers u v w (0 ≤ u, v < n, 1 ≤ w ≤ 100, u ≠ v) denoting that there is a road between city u and v whose length is w.
The next line contains an integer q (1 ≤ q ≤ 100) denoting the number of queries by Scofield. Each of the next q lines contains the request. Each request contains three integers: c s t (1 ≤ c ≤ 100, 0 ≤ s, t < n) where c denotes the capacity of the tank, s denotes the starting city and t denotes the destination city.
For each case, print the case number first. Then for each query print the cheapest trip from s to t using the car with the given capacity c or 'impossible' if there is no way of getting from s to t with the given car.
1
5 5
10 10 20 12 13
0 1 9
0 2 8
1 2 1
1 3 11
2 3 7
2
10 0 3
20 1 4
Case 1:
170
impossible
思路:最短路+贪心+dp;
dp[i][j]表示到第i个城市,油箱中还剩j油的最小费用;
用Dijkstra来维护,
开始超时了,后来网上看了人家的思路;我开始每到一个点就将所有的状态枚举了出来,这样会导致枚举对答案没有贡献的。
所以我们按贪心策略来枚举,因为,Dijkstra(Elog(n))的算法取出来的点是当前花费最小的情况,
所以如果这个点可以到达下一个点不加油就到更新下一个点,然后如果当前的油量没超过油箱的容量就+1,加入队列,就和广搜差不多的思想,然后,如果到达了目标点,那么这个一定是最小的就跳出,因这个是当前最小的。
1 #include
2 #include
3 #include
4 #include
5 #include
6 #include
7 #include
手机扫一扫
移动阅读更方便
你可能感兴趣的文章