POJ - 1724(DFS)

Description
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.
Input
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.
Output
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.
Sample Input
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
Sample Output
11
Source
CEOI 1998

题意:
你现在有k元钱,你要从1号城市走到n号城市,城市之间的道路有长度也有价钱,现在问你,在总钱数不超过k的前提下的最短路径是多长

题解:
一道DFS的题,从1开始,遍历1所能到达的所有的点以及到达这些点所经过的路径长度和过路费,一直DFS下去,直到走到n
剪枝:
1、可行性剪枝:如果当前的总过路费比k大,则不可行,剪枝
2、最优性剪枝:如果当前的路径长度大于最优解,剪枝;如果到达当前状态时的路径长度比原来到达相同状态的路径长度要长,剪枝(由于有两个变量对路径有影响,分别是现在的钱数和现在到达的城市,所以用二维数组存路径,状态作为数组下标)

思考:
1、除了字符串或者整形数组初始为0以外,其他的都不要用memset,惨痛的教训
2、先全都判断完了再改全局变量
3、如果路太多则用vector存储
4、INF可以初始化为1 << 30
5、可以把min初始化为INF,如果最后值没变,则说明不可行,则输出-1

#include <iostream>
#include <cstdio>
#include <string>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <vector>
#include <map>
#include <set>
#include <queue>

using namespace std;

struct road
{
    int d, l, t;
};

int minlen, totalcost, totallen, visit[105], midl[105][15000];
int k, n, R;
vector<vector<road> > citymap(105);

void dfs(int now)
{
    if (now == n)
    {
        minlen = min (minlen, totallen);
        return;
    }
    int l = citymap[now].size();
    for (int i = 0; i < l; i++)
    {
        road r = citymap[now][i];
        if (!visit[r.d])
        {
            int cost = totalcost + r.t;
            if (cost > k) continue;//如果钱不够了,剪枝
            int ln = totallen + r.l;
            if (ln >= minlen) continue;//中间状态剪枝,如果现在的距离已经比最优解大,则剪枝
            if (midl[r.d][cost] <= ln) continue;//如果本次到达此状态时的路径比原来的最优的长,剪枝
            midl[r.d][cost] = ln;
            totalcost = cost;//先全都判断完了再改全局变量
            totallen = ln;
            visit[r.d] = 1;
            dfs (r.d);
            visit[r.d] = 0;
            totalcost -= r.t;
            totallen -= r.l;
        }
    }
}

int main()
{
#ifndef ONLINE_JUDGE
    freopen ("in.txt", "r", stdin);
#endif // ONLINE_JUDGE
    scanf ("%d%d%d", &k, &n, &R);
    citymap.clear();
    for (int i = 1; i <= R; i++)
    {
        int s;
        road r;
        scanf ("%d%d%d%d", &s, &r.d, &r.l, &r.t);
        if (s != r.d)
        {
            citymap[s].push_back(r);
        }
    }
    minlen = 1 << 30;
    totalcost = 0;
    totallen = 0;
    memset (visit, 0, sizeof(visit));//除了字符串或者整形数组初始为0以外,其他的都不要用memset,惨痛的教训
    for (int i = 0; i < 105; i++) {
        for (int j = 0; j < 15000; j++) {
            midl[i][j] = 1 << 30;
        }
    }
    visit[1] = 1;
    dfs (1);
    if (minlen < (1 <<30))
    {
        printf ("%d\n", minlen);
    }
    else
    {
        printf ("-1\n");
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值