SRM675(div2) Level Two ShortestPathWithMagic

来源:互联网 发布:使用java程序录音 编辑:程序博客网 时间:2024/06/07 16:19

SRM675(div2)  Level Two  ShortestPathWithMagic


   Problem Statement  
 Problem Statement for ShortestPathWithMagic

Problem Statement

 There are n cities, numbered from 0 to n-1. It is possible to travel directly between any two cities. Different pairs of cities may require different travel times. You are given the travel times as a String[] dist with n elements, each consisting of n digits. The digit dist[i][j] represents the time needed to travel between cities i and j in either direction.



You are also given an int k. You have k magic potions. The potion makes you twice as fast as you normally are. Each time you travel (directly) from some city to some other city, you may take one of the potions. If you do, that travel will only take half of what it normally would. You are not allowed to take more than one potion at the same time. You are not required to drink all potions you have. 



Compute and return the smallest amount of time in which you can travel from city 0 to city 1. 

Definition

 Class:ShortestPathWithMagicMethod:getTimeParameters:String[], intReturns:doubleMethod signature:double getTime(String[] dist, int k)(be sure your method is public)   

Notes

-The travel times given in dist don't have to satisfy the triangle inequality.-Your return value must have a relative or absolute error less than 1e-9 

Constraints

-dist will contain between 2 and 50 elements, inclusive.-Each string in dist will contain exactly |dist| characters.-Each character in dist will be between '0' and '9', inclusive.-For any valid i and j: dist[i][j] = dist[j][i].-For any valid i: dist[i][i] = 0.-k will be between 0 and 50, inclusive. 

Examples

0)  
{"094", "904", "440"}
1
Returns: 4.5
According to dist, you need:
  • 9 units of time to travel between cities 0 and 1
  • 4 units of time to travel between cities 0 and 2
  • 4 units of time to travel between cities 1 and 2
You have a single magic potion. The optimal solution is to drink the potion and to travel directly from city 0 to city 1. This trip will take 9/2 = 4.5 units of time.1)  
{"094", "904", "440"}
2
Returns: 4.0
The normal travel times are the same as in Example 0, but now you have two magic potions. This changes the optimal solution. Now it is better to travel from 0 to 2 and then from 2 to 1. For each segment of your trip you will drink one of the potions. Thus, each segment will only take 4/2 = 2 units of time.2)  
{"094", "904", "440"}
50
Returns: 4.0
You are not allowed to use more than one potion at the same time. The optimal solution remains the same as in Example 1, only now you will still have 48 magic potions when you reach city 1.3)  
{"094", "904", "440"}
0
Returns: 8.0

4)  
{"076237", "708937", "680641", "296059", "334508", "771980"}
1
Returns: 3.5
5)  
{"00", "00"}
50
Returns: 0.0

This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2010, TopCoder, Inc. All rights reserved.

This problem was used for: 
       Single Round Match 675 Round 1 - Division II, Level Two

题意:给你一个邻接表,求最短距离,但是有k次机会可以使边的距离减半,求从0带1的距离

先用Floyd求出任意两点间的最短距离

dp[k][i][j]表示有从i到j,有k次机会是距离减半的最短距离

n表示结点个数

a[i][j]表示从i到j的直接距离

然后用dp,转移方程dp[k][i][j] = min(dp[k][i][j], a[i][con]/2 + dp[k-1][con][j]);(0<=i,j,con<n )

dp好久都没写的都忘了!!!


0 0
原创粉丝点击