2017Google Codejam round1b Problem C. Pony Express

来源:互联网 发布:美加净防晒霜 知乎 编辑:程序博客网 时间:2024/06/04 19:44

Problem

It's the year 1860, and the Pony Express is the fastest mail delivery system joining the East and West coasts of the United States. This system serves N different cities. In each city, there is one horse (as in the expression "one-horse town"); each horse travels at a certain constant speed and has a maximum total distance it can travel before it becomes too tired to continue.

The Pony Express rider starts off on the starting city's horse. Every time the rider reaches a city, they may continue to use their current horse or switch to that city's horse; switching is instantaneous. Horses never get a chance to rest, so whenever part of a horse's maximum total distance is "used up", it is used up forever! When the rider reaches the destination city, the mail is delivered.

The routes between cities were established via complicated negotiations between company owners, lawmakers, union delegates, and cousin Pete. That means that the distances between cities do not necessarily follow common sense: for instance, they do not necessarily comply with the triangle inequality, and the distance from city A to city B might be different from the distance from city B to city A!

You are a time traveling entrepreneur, and you have brought a fast computer from the future. A single computer is not enough for you to set up an e-mail service and make the Pony Express obsolete, but you can use it to make optimal routing plans for the Pony Express. Given all data about routes between cities and the horses in each city, and a list of pairs of starting and ending cities, can you quickly calculate the minimum time necessary for each delivery? (You should treat all of these deliveries as independent; using cities/horses on one route does not make them unavailable on other routes.)

Input

The first line of the input gives the number of test cases, TT test cases follow. Each test case is described as follows:

  • One line with two integers: N, the number of cities with horses, and Q, the number of pairs of stops we are interested in. Cities are numbered from 1 to N.
  • N lines, each containing two integers Ei, the maximum total distance, in kilometers, the horse in the i-th city can go and Si, the constant speed, in kilometers per hour, at which the horse travels.
  • N lines, each containing N integers. The j-th integer on the i-th of these lines, Dij, is -1 if there is no direct route from the i-th to the j-th city, and the length of that route in kilometers otherwise.
  • Q lines containing two integers Uk and Vk, the starting and destination point, respectively, of the k-th pair of cities we want to investigate.

Output

For each test case, output one line containing Case #x: y1 y2 ... yQ, where x is the test case number (starting from 1) and yk is the minimum time, in hours, to deliver a letter from cityUk to city Vk.

Each yk will be considered correct if it is within an absolute or relative error of 10-6 of the correct answer. See the FAQ for an explanation of what that means, and what formats of real numbers we accept.

Limits

1 ≤ T ≤ 100.
2 ≤ N ≤ 100.
1 ≤ Ei ≤ 109, for all i.
1 ≤ Si ≤ 1000, for all i.
-1 ≤ Dij ≤ 109, for all i, j.
Dii = -1, for all i. (There are no direct routes from a city to itself.)
Dij ≠ 0, for all i, j.
Uk ≠ Vk, for all k.
It is guaranteed that the delivery from Uk to Vk can be accomplished with the given horses, for all k.
Ul ≠ Um and/or Vl ≠ Vm, for all different l, m. (No ordered pair of cities to investigate is repeated within a test case.)

Small dataset

Dij = -1, for all i, j where i + 1 ≠ j. (The cities are in a single line; each route goes from one city to the next city in line.)
Q = 1.
U1 = 1.
V1 = N. (The only delivery to calculate is between the first and last cities in the line).

Large dataset

1 ≤ Q ≤ 100.
1 ≤ Uk ≤ N, for all k.
1 ≤ Vk ≤ N, for all k.

Sample


Input 
 
Output 
 
33 12 32 44 4-1 1 -1-1 -1 1-1 -1 -11 34 113 101 100010 85 5-1 1 -1 -1-1 -1 1 -1-1 -1 -1 10-1 -1 -1 -11 44 330 6010 100012 520 1-1 10 -1 3110 -1 10 -1-1 -1 -1 1015 6 -1 -12 43 13 2
Case #1: 0.583333333Case #2: 1.2Case #3: 0.51 8.01 8.0

Note that the last sample case would not appear in the Small dataset.

In Case #1 there are two options: use the horse in city 1 for the entire trip, or change horses in city 2. Both horses have enough endurance, so both options are viable. Since the horse in city 2 is faster, it is better to change, for a total time of 1/3 + 1/4.

In Case #2 there are two intermediate cities in which you can change horses. If you change horses in city 2, however, your new horse, while blazingly fast, will not have enough endurance, so you will be forced to change again in city 3. If you keep your horse, you will have the option to change horses (or not) in city 3. So, the three options, with their total times, are:

  1. Change horses in both city 2 and 3 (1/10 + 1/1000 + 10/8 = 1.351).
  2. Change horses just in city 3 (2/10 + 10/8 = 1.45).
  3. Never change horses (12/10 = 1.2).

In Case #3, there are lots of alternatives for each delivery. The optimal one for the first delivery (city 2 to city 4) is to go to city 1 in time 10/1000, change horses, and then go to cities 2, 3 and 4, in that order, using the horse from city 1, which takes time (10 + 10 + 10) / 60.

For the second delivery (city 3 to city 1) you have no choice but to first go to city 4 which takes time 10/5. Your relatively fast horse does not have enough endurance to get anywhere else, so you need to grab the horse in city 4. You could use it to get directly to city 1 in time 15, but that would be slower than riding it to city 2 in time 6 and then using the blazingly fast horse in city 2 to get to city 1 in just 10/1000 extra time.

In the third delivery (city 3 to city 2) of Case #3 it is optimal to use the first two steps of the previous one, for a total time of 10/5 + 6 = 8.



从一个点骑马去送快递 要求送到某个点  点与点之间有距离 每匹马都有一个最多行驶的距离

在行驶过程中可以更换已到点的马 

需要送q个邮件 每个邮件之间是独立的

先一次最短路 求出两点之间的最短距离 根据马最长行驶距离的限制 求出在此距离之下的时间 这代表中间不换马

然后再一次最短路 求出两点之间的最短距离 这代表中间可以换马的

#include <cstdio>#include <iostream>#include <cstring>#include <cmath>#include <algorithm>#include <string.h>#include <string>#include <vector>#include <queue>#define MEM(a,x) memset(a,x,sizeof a)#define eps 1e-8#define MOD 10009#define MAXN 10010#define MAXM 100010#define INF 99999999#define ll long long#define bug cout<<"here"<<endl#define fread freopen("C-large-practice.in","r",stdin)#define fwrite freopen("out.txt","w",stdout)using namespace std;int n,q;ll e[110],s[110];ll mp[110][110];int u,v;double ti[110][110];int main(){//    fread;//    fwrite;    int tc;    scanf("%d",&tc);    int cs=1;    while(tc--)    {        scanf("%d%d",&n,&q);        for(int i=0;i<n;i++)            scanf("%lld%lld",&e[i],&s[i]);        for(int i=0;i<n;i++)            for(int j=0;j<n;j++)            {                scanf("%lld",&mp[i][j]);                if(mp[i][j]==-1)                    mp[i][j]=1e12;                if(i==j)                    mp[i][j]=0;            }        for(int k=0;k<n;k++)        {            for(int i=0;i<n;i++)            {                for(int j=0;j<n;j++)                {                    mp[i][j]=min(mp[i][j],mp[i][k]+mp[k][j]);                }            }        }        for(int i=0;i<n;i++)        {            for(int j=0;j<n;j++)            {                if(mp[i][j]>e[i])                    ti[i][j]=1e12;                else                    ti[i][j]=(mp[i][j]*1.0)/(s[i]*1.0);            }        }        for(int k=0;k<n;k++)        {            for(int i=0;i<n;i++)                for(int j=0;j<n;j++)            {                ti[i][j]=min(ti[i][j],ti[i][k]+ti[k][j]);            }        }        printf("Case #%d:",cs++);        for(int i=0;i<q;i++)        {            scanf("%d%d",&u,&v);            printf(" %lf",ti[u-1][v-1]);        }        puts("");    }    return 0;}



     

0 0