http://www.cnblogs.com/scau20110726/archive/2013/04/28/3050178.htmlpoj 1724:ROADS(DFS + 剪枝) - Freecode# - 博客园
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.
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 :
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
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.
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
The code written for these kind of contest is usually messy, hard to read.
BFS+Priority Queue
http://www.cnblogs.com/scau20110726/archive/2013/04/28/3050178.html
判断一个元素能否入队,不再是看它的最短路估计值是否被更新,而是从当前点能到达的点,都可以放入队列,在优先队列中,每次取队中最短路估计值最小的元素出来去更新. 每次从队列中取元素,沿着取出来的点往下找,如果费用比K少再加入队列,否则不加,这样可以省时间。 如果标号为n的点出队了,那么其实算法结束了,因为之前的状态都没有更新出更小的值,在从现在开始,哪怕再怎么更新,都不会比现在更小了,所以直接跳出,输出即可. 由于边相对点比较少,属于稀疏图,所以具体实现时用邻接表会比较快。
http://www.java3z.com/cwbwebhome/article/article17/acm862.html
http://blog.csdn.net/just_water/article/details/7733337
int BFS() { priority_queue<kdq>q;//优先队列 kdq xx; xx.v=1; xx.c=0; xx.l=0; q.push(xx);//从1出发。 while(!q.empty()) { kdq temp=q.top(); q.pop(); if(temp.v==n)//找到n则跳出 return temp.l; for(int i=head[temp.v]; i>=0; i=next[i])//由该点出发遍历一遍 { if(temp.c+c[i]<=money)//如果pre的花费加上本次的花费小于money,则将这点入队。 { kdq qq; qq.c=temp.c+c[i];//到达此次的总花费 qq.l=temp.l+l[i];//到达此点的总路径 qq.v=v[i]; q.push(qq);//入队 } } } return -1; }
SPFA
http://www.cnblogs.com/naix-x/p/3175441.html
DFS+Pruning根据输入的有向图来进行DFS搜索,每次到达一个城市,都要计算新的经过的路径长度和剩下的路费(剪枝:1、如果当前经过的路径长度超过了之前记录的最小达到N的路径长度,那么直接返回上一层;2、如果剩下的路费<0了,则不能继续走,返回上一层),直到到达编号为N的城市,这个时候比较一下,确定当前的最小到达N的路径长度。
struct Node{
9 int city;
10 int len;
11 int toll;
12 Node* next;
13 }city[MAXN]; //邻接表
14
15 bool isv[MAXN];
16
17 int K,n,r,s;
18 int Min;
19
20 void dfs(int c,int l,int k)
21 {
22 //k为剩下的钱
23 if(k<0)
24 return ;
25 if(l>=Min) //如果当前走过的路的长度超过了记录的最小值,则退出
26 return ;
27 if(c==n && l<Min){ //到达n城市
28 Min = l;
29 return ;
30 }
31
32 Node* p = city[c].next;
33 while(p){
34 if(!isv[p->city]){ //没走过
35 isv[p->city] = true;
36 dfs(p->city,l + p->len,k - p->toll); //进入下一个城市
37 isv[p->city] = false;
38 }
39 p = p->next;
40 }
41 }
]Read full article from poj 1724:ROADS(DFS + 剪枝) - Freecode# - 博客园