CodeForces 189D(最短路+dp)

我不是女神ヾ 2022-06-02 05:09 241阅读 0赞

问题描述:

PMP is getting a warrior. He is practicing a lot, but the results are not acceptable yet. This time instead of programming contests, he decided to compete in a car racing to increase the spirit of victory. He decides to choose a competition that also exhibits algorithmic features.

AlgoRace is a special league of car racing where different teams compete in a country of n cities. Cities are numbered 1 through n. Every two distinct cities in the country are connected with one bidirectional road. Each competing team should introduce one driver and a set of cars.

The competition is held in r rounds. In i-th round, drivers will start at city siand finish at city ti. Drivers are allowed to change their cars at most ki times. Changing cars can take place in any city in no time. One car can be used multiple times in one round, but total number of changes should not exceed ki. Drivers can freely choose their path to destination.

PMP has prepared m type of purpose-built cars. Beside for PMP’s driving skills, depending on properties of the car and the road, a car traverses each road in each direction in different times.

PMP Warriors wants to devise best strategies of choosing car and roads in each round to maximize the chance of winning the cup. For each round they want to find the minimum time required to finish it.

Input

The first line contains three space-separated integers n, m, r (2 ≤ n ≤ 60, 1 ≤ m ≤ 60, 1 ≤ r ≤ 105) — the number of cities, the number of different types of cars and the number of rounds in the competition, correspondingly.

Next m sets of n × n matrices of integers between 0 to 106 (inclusive) will follow — describing the time one car requires to traverse different roads. The k-th integer in j-th line of the i-th set is the time that i-th car requires to traverse the road from j-th city to k-th city. These matrices are not necessarily symmetric, but their diagonal is always zero.

Next r lines contain description of the rounds. The i-th of these lines contains space-separated integers si, ti, ki (1 ≤ si, ti ≤ n, si ≠ ti, 0 ≤ ki ≤ 1000) — the number of starting city, finishing city and the number of possible car changes in i-th round, correspondingly.

Output

For each round you should print the minimum required time to complete the round in a single line.

Input

  1. 4 2 3
  2. 0 1 5 6
  3. 2 0 3 6
  4. 1 3 0 1
  5. 6 6 7 0
  6. 0 3 5 6
  7. 2 0 1 6
  8. 1 3 0 2
  9. 6 6 7 0
  10. 1 4 2
  11. 1 4 1
  12. 1 4 3

Output

  1. 3
  2. 4
  3. 3

Input

  1. 4 2 3
  2. 0 7 3 3
  3. 8 0 10 5
  4. 1 1 0 4
  5. 8 9 2 0
  6. 0 3 3 9
  7. 7 0 4 9
  8. 3 8 0 4
  9. 4 8 9 0
  10. 2 3 3
  11. 2 1 3
  12. 1 2 2

Output

  1. 4
  2. 5
  3. 3

题目题意:题目给了好几种车的,每种车从a到b的距离都不同,r次查询从s到e最多换车k车,问每次需要的最短时间

题目分析:先最短路跑出每种车俩点之间的最短时间,然后保存从i到j的最短时间(不同车之间比较)。dp[k][i][j]保存的是换车小于等于k次从i到j的最短时间.

代码如下:

  1. #include<iostream>
  2. #include<cmath>
  3. #include<cstring>
  4. #include<cstdio>
  5. using namespace std;
  6. const int inf=0x3f3f3f3f;
  7. const int maxn=65;
  8. int G[maxn][maxn][maxn],dp[maxn][maxn][maxn];
  9. int main()
  10. {
  11. int n,m,r;
  12. while (scanf("%d%d%d",&n,&m,&r)!=EOF) {
  13. for (int k=1;k<=m;k++) {
  14. for (int i=1;i<=n;i++) {
  15. for (int j=1;j<=n;j++) {
  16. scanf("%d",&G[k][i][j]);
  17. }
  18. }
  19. }
  20. for (int l=1;l<=m;l++) {
  21. for (int k=1;k<=n;k++) {
  22. for (int i=1;i<=n;i++) {
  23. for (int j=1;j<=n;j++) {
  24. G[l][i][j]=min(G[l][i][j],G[l][i][k]+G[l][k][j]);
  25. }
  26. }
  27. }
  28. }
  29. for (int k=0;k<=n;k++) {
  30. for (int i=1;i<=n;i++) {
  31. for (int j=1;j<=n;j++) {
  32. dp[k][i][j]=inf;
  33. }
  34. }
  35. }
  36. for (int l=1;l<=m;l++) {
  37. for (int k=1;k<=n;k++) {
  38. for (int i=1;i<=n;i++) {
  39. for (int j=1;j<=n;j++) {
  40. dp[0][i][j]=min(dp[0][i][j],G[l][i][k]+G[l][k][j]);//保存a到b的最短时间
  41. }
  42. }
  43. }
  44. }
  45. for (int l=1;l<=n;l++) {
  46. for (int k=1;k<=n;k++) {
  47. for (int i=1;i<=n;i++) {
  48. for (int j=1;j<=n;j++) {
  49. dp[l][i][j]=min(dp[l][i][j],dp[l-1][i][k]+dp[0][k][j]);
  50. }
  51. }
  52. }
  53. }
  54. while (r--) {
  55. int s,e,k;
  56. scanf("%d%d%d",&s,&e,&k);
  57. int ans=inf;
  58. k=min(k,n);
  59. for (int i=0;i<=k;i++) ans=min(ans,dp[i][s][e]);
  60. printf("%d\n",ans);
  61. }
  62. }
  63. return 0;
  64. }

发表评论

表情:
评论列表 (有 0 条评论,241人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Codeforce 189B——Counting Rhombi

    题意:给定一个矩形的长和宽,求这个矩形里有多少个菱形(可重叠)。 思路:规律题。小学3年级的练习题,直接找有多少的偶数对角线(横纵相乘),两重循环,暴力即可。