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. 

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

 

  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         else
67             printf("%d\n",Min);
68     }
69     return 0;
70 }

 

Freecode : www.cnblogs.com/yym2013

转载于:https://www.cnblogs.com/yym2013/p/3891517.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值