深度优先搜索--算法(寻路问题 poj724)
2017-11-18 10:17
531 查看
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.
N个城市,编号1到N。城市间有R条单向道路。
每条道路连接两个城市,有长度和过路费两个属性。
Bob只有K块钱,他想从城市1走到城市N。问最短共需要走多长的路。如果到不了N,输出-1。
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#include <iostream> #include <vector> #include <list> #define NUM 110 using namespace std; struct Road { int e,L,t; }; //struct Roar *A;//定义一个指向结构体的指针数组; int K,N,R; vector<list<Road> > AA(NUM); bool visted[NUM] = {0}; int totleFree = 0; int totleLen = 0; int min_len = 1<<30;//记录最短的长度; int minL[NUM][10010]; void print(Road r) { cout <<r.e<<r.L<<r.t<<endl; } void DFS(int s) { if(s==N){ min_len = min(min_len,totleLen); // cout <<min_len<<endl; return ; } //邻边: for(list<Road>::iterator it = AA[s].begin();it!=AA[s].end();it++) if(!visted[it->e]){ if(totleFree+it->t<=K){//可行性剪枝; if(totleLen+it->L>=min_len)//如果已经能判断长度能大于最小的min_len就直接剪掉这个枝条; continue; if(totleLen+it->L>=minL[it->e][totleFree+it->t])//最优性剪枝; continue; minL[it->e][totleFree+it->t] =totleLen+it->L; totleLen+=it->L; totleFree +=it->t; visted[it->e] = true; // cout <<totleLen<<" "<<totleFree<<endl; DFS(it->e); // cout<<"list:"<<s<<endl; visted[it->e] = false; totleFree-=it->t; totleLen-=it->L; } } } int main() { cin>>K>>N>>R; for(int i = 1;i<=R;i++){ int First; Road r; cin>>First>>r.e>>r.L>>r.t; if(First!=r.e) AA[First].push_back(r); } for(int i = 0;i<NUM;i++) for(int j = 0;j<10010;j++) minL[i][j] = 1<<30; /* int i = 1; for(vector<list<Road>>::iterator itt = AA.begin();itt!=AA.end()&&i<=5;itt++,i++){ cout <<i<<endl; for_each(AA[i].begin(), AA[i].end(), print); } */ //开始深搜: //可以判断他是一个连通网; DFS(1); if(min_len!=1<<30) cout <<min_len<<endl; else{ cout <<"-1"<<endl; } return 0; }
相关文章推荐
- 深度优先搜索1-寻路问题(算法基础 第6周)
- 算法基础之python实现深度优先搜索的数独问题
- 深度优先搜索1-棋盘问题(算法基础 第6周)
- 迷宫寻路算法-js-深度优先搜索
- 深度优先搜索--算法(城堡问题 poj1164)
- 该深度优先搜索算法查找钥匙的程序之问题~~
- 机试算法讲解: 第44题 深度优先搜索之素数环问题
- 【游戏编程】AI-迷宫寻路算法-深度优先搜索和广度优先搜索
- AI-迷宫寻路算法-深度优先搜索和广度优先搜索
- 深度优先搜索1-城堡问题(算法基础 第6周)
- 【游戏编程】AI-迷宫寻路算法-深度优先搜索和广度优先搜索
- 深度优先搜索 和问题 简单函数递归 “加 还是不加”
- nyoj 部分和问题(深度优先搜索DFS)
- 广度优先搜索-迷宫问题(算法基础 第8周)
- 作业调度问题深度搜索定界算法
- 算法导论-图的搜索算法之深度优先搜索和广度优先搜索
- 【算法研究】搜索算法-深度优先搜索
- 深度优先搜索入门:POJ1164城堡问题(递归、用栈模拟递归)
- nyoj 部分和问题(深度优先搜索DFS)
- 作业调度问题深度搜索定界算法