总时间限制: 1000ms 内存限制: 65536kB

描述

N cities named with numbers 1 … N are connected with one-way roads. Each road has two parameters associated with it : the road length and the toll that needs to be paid for the road (expressed in the number of coins).
Bob and Alice used to live in the city 1. After noticing that Alice was cheating in the card game they liked to play, Bob broke up with her and decided to move away - to the city N. He wants to get there as quickly as possible, but he is short on cash.

We want to help Bob to find the shortest path from the city 1 to the city N that he can afford with the amount of money he has.

输入

The first line of the input contains the integer K, 0 ≤ K ≤ 10000, maximum number of coins that Bob can spend on his way.
The second line contains the integer N, 2 ≤ N ≤ 100, the total number of cities.

The third line contains the integer R, 1 ≤ R ≤ 10000, the total number of roads.

Each of the following R lines describes one road by specifying integers S, D, L and T separated by single blank characters :

  • S is the source city, 1 ≤ S ≤ N
  • D is the destination city, 1 ≤ D ≤ N
  • L is the road length, 1 ≤ L ≤ 100
  • T is the toll (expressed in the number of coins), 0 ≤ T ≤100

Notice that different roads may have the same source and destination cities.

输出

The first and the only line of the output should contain the total length of the shortest path from the city 1 to the city N whose total toll is less than or equal K coins.
If such path does not exist, only number -1 should be written to the output.

样例输入

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

样例输出

  1. 11

思路

初次尝试

从城市1开始深度优先遍历整个图, 找到所有能到达N的走法.

  1. #include <iostream>
  2. #include <cstring>
  3. #include <vector>
  4. #include <algorithm>
  5. using namespace std;
  6. int CoinsLeft;
  7. int CityNum;
  8. int RoadNum;
  9. typedef struct Road{
  10. int dstCity;
  11. int length;
  12. int toll;
  13. }Road;
  14. vector< vector<Road> > G(110); // 邻接表
  15. int currentLen;
  16. int currentCost;
  17. int minLen;
  18. int visited[110]; // 用来标记一个城市是否被走过
  19. void DFS(int srcCity) {
  20. if( srcCity == CityNum ) {
  21. minLen = min(minLen, currentLen);
  22. return;
  23. }
  24. for(int i = 0; i < G[srcCity].size(); i++) {
  25. Road r = G[srcCity][i];
  26. if( currentCost + r.toll > CoinsLeft )
  27. continue;
  28. if( !visited[r.dstCity] ) {
  29. currentLen += r.length;
  30. currentCost += r.toll;
  31. visited[r.dstCity] = 0x1;
  32. DFS(r.dstCity);
  33. visited[r.dstCity] = 0x0;
  34. currentLen -= r.length;
  35. currentCost -= r.toll;
  36. }
  37. }
  38. }
  39. int main() {
  40. /* Read data */
  41. cin >> CoinsLeft >> CityNum >> RoadNum;
  42. for(int i = 0; i < RoadNum; i++) {
  43. int srcCity;
  44. Road r;
  45. cin >> srcCity >> r.dstCity >> r.length >> r.toll;
  46. if( srcCity != r.dstCity )
  47. G[srcCity].push_back(r);
  48. }
  49. /* Initialize objects */
  50. memset(visited, 0x0, sizeof(visited));
  51. currentLen = 0;
  52. minLen = (1 << 30);
  53. currentCost = 0;
  54. /* Search path */
  55. visited[1] = 1;
  56. DFS(1);
  57. /* Display result */
  58. if( minLen < (1 << 30) )
  59. cout << minLen << endl;
  60. else
  61. cout << "-1" << endl;
  62. return 0;
  63. }

样例测试是对的, 但是在平台上超时了.

剪枝

在上面的代码中, 我们已经使用了可行性剪枝, 即判断盘缠够不够到目的地, 如果不够就continue.

但这个剪枝还不够好, 我们加入一个最优性剪枝, 即:

  • 如果当前已经找到的最优路径长度为L, 那么在继续搜索的过程中, 总长度已经大于等于L的走法, 就可以直接放弃, 不用走到底了.

在代码中是这样的:

  1. // 其他部分和上面一样
  2. void DFS(int srcCity) {
  3. if( srcCity == CityNum ) {
  4. minLen = min(minLen, currentLen);
  5. return;
  6. }
  7. for(int i = 0; i < G[srcCity].size(); i++) {
  8. Road r = G[srcCity][i];
  9. if( currentCost + r.toll > CoinsLeft ) // 可行性剪枝
  10. continue;
  11. if( !visited[r.dstCity] ) {
  12. if( currentLen + r.length >= minLen ) // 最优性剪枝1
  13. continue;
  14. currentLen += r.length;
  15. currentCost += r.toll;
  16. visited[r.dstCity] = 0x1;
  17. DFS(r.dstCity);
  18. visited[r.dstCity] = 0x0;
  19. currentLen -= r.length;
  20. currentCost -= r.toll;
  21. }
  22. }
  23. }

然后加上这个还是超时. 我们继续剪枝, 用空间换时间.

走到中间的某一个城市F, 花费了路费T. 可能通过其他路径也可以走到城市F, 但花费更少的钱.

  • 我们用midL[k][toll]表示: 走到城市 k 时总路费为 toll 的条件下, 最优路径的长度.
  • 若在后续的搜索中, 再次走到 k 时, 路费恰好为 toll, 且此时的路径长度已经超过midL[k][toll], 则不必再走下去了.

改进后完整代码如下:

  1. #include <iostream>
  2. #include <cstring>
  3. #include <vector>
  4. #include <algorithm>
  5. using namespace std;
  6. int CoinsLeft;
  7. int CityNum;
  8. int RoadNum;
  9. typedef struct Road{
  10. int dstCity;
  11. int length;
  12. int toll;
  13. }Road;
  14. vector< vector<Road> > G(110); // 邻接表
  15. int currentLen;
  16. int currentCost;
  17. int minLen;
  18. int visited[110]; // 用来标记一个城市是否被走过
  19. int minL[110][10010]; // minL[i][j]: 走到第i个城市, 花费盘缠为j的情况下, 值为最优路径长度
  20. void DFS(int srcCity) {
  21. if( srcCity == CityNum ) {
  22. minLen = min(minLen, currentLen);
  23. return;
  24. }
  25. for(int i = 0; i < G[srcCity].size(); i++) {
  26. Road r = G[srcCity][i];
  27. if( currentCost + r.toll > CoinsLeft ) // 可行性剪枝
  28. continue;
  29. if( !visited[r.dstCity] ) {
  30. if( currentLen + r.length >= minLen ) // 最优性剪枝1
  31. continue;
  32. if( currentLen + r.length >= minL[r.dstCity][currentCost + r.toll])
  33. continue; // 最优性剪枝2
  34. minL[r.dstCity][currentCost + r.toll] = currentLen + r.length;
  35. currentLen += r.length;
  36. currentCost += r.toll;
  37. visited[r.dstCity] = 0x1;
  38. DFS(r.dstCity);
  39. visited[r.dstCity] = 0x0;
  40. currentLen -= r.length;
  41. currentCost -= r.toll;
  42. }
  43. }
  44. }
  45. int main() {
  46. /* Read data */
  47. cin >> CoinsLeft >> CityNum >> RoadNum;
  48. for(int i = 0; i < RoadNum; i++) {
  49. int srcCity;
  50. Road r;
  51. cin >> srcCity >> r.dstCity >> r.length >> r.toll;
  52. if( srcCity != r.dstCity )
  53. G[srcCity].push_back(r);
  54. }
  55. /* Initialize objects */
  56. memset(visited, 0x0, sizeof(visited));
  57. currentLen = 0;
  58. minLen = (1 << 30);
  59. currentCost = 0;
  60. for(int i = 0; i < 110; i++) {
  61. for(int j = 0; j < 10010; j++) {
  62. minL[i][j] = (0x1 << 30);
  63. }
  64. }
  65. /* Search path */
  66. visited[1] = 1;
  67. DFS(1);
  68. /* Display result */
  69. if( minLen < (1 << 30) )
  70. cout << minLen << endl;
  71. else
  72. cout << "-1" << endl;
  73. return 0;
  74. }