首页 > 代码库 > poj 1724:ROADS(DFS + 剪枝)
poj 1724:ROADS(DFS + 剪枝)
ROADS
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 10777 | Accepted: 3961 |
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.
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 :
Notice that different roads may have the same source and destination cities.
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.
If such path does not exist, only number -1 should be written to the output.
Sample Input
5671 2 2 32 4 3 33 4 2 41 3 4 14 6 2 13 5 2 05 4 3 2
Sample Output
11
Source
CEOI 1998
DFS + 剪枝。
这道题用DFS就可以做,当然需要剪枝,否则会超时。
题意:
思路:
根据输入的有向图来进行DFS搜索,每次到达一个城市,都要计算新的经过的路径长度和剩下的路费(剪枝:1、如果当前经过的路径长度超过了之前记录的最小达到N的路径长度,那么直接返回上一层;2、如果剩下的路费<0了,则不能继续走,返回上一层),直到到达编号为N的城市,这个时候比较一下,确定当前的最小到达N的路径长度。
最后输出最小路径长度。
代码:
1 #include <iostream> 2 #include <stdio.h> 3 #include <string.h> 4 using namespace std; 5 6 #define MAXN 110 7 8 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 }42 43 int main()44 {45 while(scanf("%d",&K)!=EOF){46 scanf("%d",&n);47 scanf("%d",&r);48 49 memset(city,NULL,sizeof(city));50 memset(isv,0,sizeof(isv));51 Min = 0x7ffffff;52 53 while(r--){54 scanf("%d",&s);55 Node* p = (Node*)malloc(sizeof(Node));56 scanf("%d%d%d",&p->city,&p->len,&p->toll);57 p->next = city[s].next;58 city[s].next = p;59 }60 61 isv[1] = true;62 dfs(1,0,K);63 64 if(Min==0x7ffffff)65 printf("-1\n");66 else67 printf("%d\n",Min);68 }69 return 0;70 }
Freecode : www.cnblogs.com/yym2013
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。