SRM675(div2) Level Two ShortestPathWithMagic

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: ShortestPathWithMagic
Method: getTime
Parameters: String[], int
Returns: double
Method 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好久都没写的都忘了!!!

#include
   
   
    
    
#include
    
    
     
     
#include
     
     
      
      
#include
      
      
       
       
using namespace std;

const int maxSize = 55;
double dp[55][55];
double a[55][55];

double min(double x, double y)
{
	return x
       
       
         dist, int k) { double ans = 0.0; int i, j, con; int len = dist.size(); for(i = 0; i < len; i++) for(j = 0; j < len; j++) { a[i][j] = dist[i][j] - '0'; dp[i][j] = a[i][j]; } Floyd_Warshall(dp, len); debug(len); for(int c = 0; c < k; c++) for(i = 0; i < len; i++) { for(j = 0; j < len; j++) { for(con = 0; con < len; con++) { dp[i][j] = min(dp[i][j], a[i][con]/2 + dp[con][j]); } } } return dp[0][1]; } }; 
       
      
      
     
     
    
    
   
   

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值