poj_1724_深搜之二维不定长数组的使用

ROADS
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 12371 Accepted: 4568

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

/********************************

解题思路:找到所有可能的路,然后得到里面路径最短的一条。

由于对栈和队列不太熟,所以这一题怎么把数据存储下来对我来说有难度,网上查了查,可以用vector,感觉很方便.以下为代码

********************************/

#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <stack>
#include <queue>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <vector>
#include <bitset>
using namespace std;

#define INT_MAX 1 << 30

typedef long long ll;
int K,N,R;
int visit[200];
int minL[200][20000];
int minLeng;
int tolleng;
int tolcost;

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

vector<vector<post>> about(200);                            //邻接表,我不写200限定一级容器大小会出问题。。。

void dfs(int n)
{
    if (n == N)                                             //搜索结束条件判断                 
    {
        minLeng = min(minLeng,tolleng);
        return;
    }
    for (unsigned int i = 0; i < about[n].size(); i += 1)   //遍历搜索 
    {
        post b;
        b = about[n][i];
        if (!visit[b.d])                                    //判断及剪枝
        {
            if(tolcost + b.t > K)            
                continue ;
            if(tolleng + b.l >= minLeng)      
                continue ;
            if(tolleng + b.l >= minL[b.d][tolcost+b.t])  
                continue ;
                 
            visit[b.d] = 1;                                 //每次搜索”初始化“
            tolcost += b.t;
            tolleng += b.l;
            minL[b.d][tolcost] = tolleng;
            dfs(b.d);                                       //搜索
            visit[b.d] = 0;                   
            tolcost -= b.t;                                 //回溯
            tolleng -= b.l;
        }
    }
}    

int main()
{
    cin >> K >> N >> R;                     //打印输入      
    for (int i = 0; i < R; i += 1)      
    {
        post a;
        int s;
        cin >> s >> a.d >> a.l >> a.t;
        if(s != a.d)
            about[s].push_back(a);      
    }       
    memset(visit,0,sizeof(visit));          //初始化,直接变量定义时赋初始值也行
    for (int i = 1; i <= N; i += 1)
    {
        for (int j = 0; j <= K; j += 1)
        {
            minL[i][j] = (1 << 30);
        }
    }
    tolleng = 0;
    tolcost = 0;
    minLeng = 1 << 30;
    visit[1] = 1;
    dfs(1);                                 //搜索
    if(minLeng < (1 << 30))                 //输出
    {
        cout << minLeng << endl;
    }
    else
    {
        cout << "-1" << endl;
    }
    return 0;
}</span>


/********************************

经过一段时间的摸索,已经得到了自己解深搜题的套路,以此题为例,在此记录一下下:

#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <stack>
#include <queue>
#include
#include <cstring>
#include <cmath>
#include <vector>
#include <bitset>
#include <list>
using namespace std;

#define INT_MAX 1 << 30

typedef long long ll;
int n;
//变量的定义

int dfs()
{   
    //搜索结束条件判断
    //遍历搜索:1.判断及剪枝2.初始化-搜索-回溯
}

int main(int argc, char const* argv[])
{
    /数据的输入和存储
    //搜索的初始化
    //开始搜索
    //输出
    return 0;
}
********************************/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值