1724:ROADS,考点:深搜剪枝

原题:http://bailian.openjudge.cn/practice/1724/

描述

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. 

样例输入

5
6
7
1 2 2 3
2 4 3 3
3 4 2 4
1 3 4 1
4 6 2 1
3 5 2 0
5 4 3 2

样例输出

11 

解法

 思路:深搜,递归的边界是第N个城市,由于每条路都是单向的,不用担心同一条路走回来的问题。但是数据大,要剪枝。

  • 已经经过的城市就不要再去了(因为无后效性,每个城市的所有路径情况与什么时候经过这个城市无关),
  • 最优剪枝:如果已经大于等于最短长度,就可以不再搜索了
  • 合理性剪枝:如果花费已经超过拥有的K元,也不用搜了
  • 最后记住需要回溯。以及递归边界要记得return(不要太high了,递归边界都不return)

坑:

  • 解题思路方面:注意题目中是K元能到达的最小花费的最短路,可能会有更短的路径但是花费超过K。不能直接算最短路径的最小花费与K进行比较来判断。
  • 内存方面:这道题数据挺大的,无论是顶点还是每个顶点对应的边集都应该用vector来存,不然会MLE。
  • 时间方面:原来在递归中,中间结果作为参数以及回溯位置的设置会影响时间。

第一个版本,把中间结果作为了参数,回溯写在了递归的最后,超时了。

#include <iostream>
#include <vector>
#include <cstring>
#define INF 1<<30
using namespace std;
int K, N, R;
struct edge {
    int len;
    int pay;
    int end;
    edge(int e,int l,int p):end(e),len(l),pay(p){}
};
int minpay;
int minlen;
vector<vector<edge> >V;
int minL[101][10005];
bool visited[101];
void dfs(int city,int len,int pay) {
    if (city == N) {
        if (len < minlen)
            minlen = len;
        if (len == minlen) {
            if (pay < minpay)
                minpay = pay;
        }
        return;
    }
    if (minL[city][pay] == -1)
        minL[city][pay] = len;
    visited[city] = true;
    int edgenum = V[city].size();
    for (int i = 0; i < edgenum; i++) {
        edge temp = V[city][i];
        if (visited[temp.end])//重复的剪枝
            continue;
        if (len + temp.len >= minlen)//最优剪枝
            continue;
        if (pay + temp.pay > K)//合理性剪枝
            continue;
        if (minL[temp.end][pay + temp.pay] != -1 && len + temp.len >= minL[temp.end][pay + temp.pay])
            continue;
        dfs(temp.end, len + temp.len, pay + temp.pay);
    }
    visited[city] = false;//回溯
}
int main()
{
    memset(minL, -1, sizeof(minL));
    memset(visited, 0, sizeof(visited));
    cin >> K >> N >> R;
    V.resize(N + 1);
    for (int i = 0; i < R; i++) {
        int s, e, l, p;
        cin >> s >> e >> l >> p;
        if(s!=e)
            V[s].push_back(edge(e, l, p));
    }
    minpay = INF;
    minlen = INF;
    dfs(1, 0, 0);
    if (minlen == INF)
        cout << -1 << endl;
    else
        cout << minlen << endl;
    return 0;
}

第二个版本,把中间结果作为全局变量,需要进行回溯,回溯的位置在循环内部。这个版本的代码AC了

 1 #include <iostream>
 2 #include <vector>
 3 #include <cstring>
 4 #define INF 1<<30
 5 using namespace std;
 6 int K, N, R;
 7 struct edge {
 8     int len;
 9     int pay;
10     int end;
11     edge(int e,int l,int p):end(e),len(l),pay(p){}
12 };
13 int minpay;
14 int minlen;
15 vector<vector<edge> >V;
16 int minL[101][10005];
17 bool visited[101];
18 int totallen, totalpay;
19 void dfs(int city) {
20     if (city == N) {
21         if (totallen < minlen)
22             minlen = totallen;
23         if (totallen == minlen) {
24             if (totalpay < minpay)
25                 minpay = totalpay;
26         }
27         return;
28     }
29     int edgenum = V[city].size();
30     for (int i = 0; i < edgenum; i++) {
31         edge temp = V[city][i];
32         if (visited[temp.end])//重复的剪枝
33             continue;
34         if (totallen + temp.len >= minlen)//最优剪枝
35             continue;
36         if (totalpay + temp.pay > K)//合理性剪枝
37             continue;
38         if (minL[temp.end][totalpay + temp.pay] != -1 && totallen + temp.len >= minL[temp.end][totalpay + temp.pay])
39             continue;
40         totallen += temp.len;
41         totalpay += temp.pay;
42         minL[temp.end][totalpay] = totallen;
43         visited[temp.end] = true;
44         dfs(temp.end);
45         visited[temp.end] = false;
46         totallen -= temp.len;
47         totalpay -= temp.pay;
48     }
49 }
50 int main()
51 {
52     memset(minL, -1, sizeof(minL));
53     memset(visited, 0, sizeof(visited));
54     cin >> K >> N >> R;
55     V.resize(N + 1);
56     for (int i = 0; i < R; i++) {
57         int s, e, l, p;
58         cin >> s >> e >> l >> p;
59         if(s!=e)
60             V[s].push_back(edge(e, l, p));
61     }
62     minpay = INF;
63     minlen = INF;
64     totallen = 0;
65     totalpay = 0;
66     dfs(1);
67     if (minlen == INF)
68         cout << -1 << endl;
69     else
70         cout << minlen << endl;
71     return 0;
72 }

 

posted @ 2021-07-13 19:56  永远是个小孩子  阅读(38)  评论(0编辑  收藏  举报