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的路径长度。
  最后输出最小路径长度。
  

  代 码

 #include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std; #define MAXN 110 struct Node{
int city;
int len;
int toll;
Node* next;
}city[MAXN]; //邻接表 bool isv[MAXN]; int K,n,r,s;
int Min; void dfs(int c,int l,int k)
{
//k为剩下的钱
if(k<)
return ;
if(l>=Min) //如果当前走过的路的长度超过了记录的最小值,则退出
return ;
if(c==n && l<Min){ //到达n城市
Min = l;
return ;
} Node* p = city[c].next;
while(p){
if(!isv[p->city]){ //没走过
isv[p->city] = true;
dfs(p->city,l + p->len,k - p->toll); //进入下一个城市
isv[p->city] = false;
}
p = p->next;
}
} int main()
{
while(scanf("%d",&K)!=EOF){
scanf("%d",&n);
scanf("%d",&r); memset(city,NULL,sizeof(city));
memset(isv,,sizeof(isv));
Min = 0x7ffffff; while(r--){
scanf("%d",&s);
Node* p = (Node*)malloc(sizeof(Node));
scanf("%d%d%d",&p->city,&p->len,&p->toll);
p->next = city[s].next;
city[s].next = p;
} isv[] = true;
dfs(,,K); if(Min==0x7ffffff)
printf("-1\n");
else
printf("%d\n",Min);
}
return ;
}

Freecode : www.cnblogs.com/yym2013

poj 1724:ROADS(DFS + 剪枝)的更多相关文章

  1. 深搜+剪枝 POJ 1724 ROADS

    POJ 1724 ROADS Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 12766   Accepted: 4722 D ...

  2. poj 1724 ROADS 很水的dfs

    题意:给你N个城市和M条路和K块钱,每条路有话费,问你从1走到N的在K块钱内所能走的最短距离是多少 链接:http://poj.org/problem?id=1724 直接dfs搜一遍就是 代码: # ...

  3. DFS(剪枝) POJ 1724 ROADS

    题目传送门 题意:问从1到n的最短路径,同时满足花费总值小于等于k 分析:深搜+剪枝,如果之前走过该点或者此时的路劲长度大于最小值就不进行搜索. /************************** ...

  4. poj 1724 ROADS 解题报告

    题目链接:http://poj.org/problem?id=1724 题目意思:给出一个含有N个点(编号从1~N).R条边的有向图.Bob 有 K 那么多的金钱,需要找一条从顶点1到顶点N的路径(每 ...

  5. 数独问题的介绍及POJ 2676-Sudoku(dfs+剪枝)

    知道是数独问题后犹豫了一下要不要做(好像很难的样纸==.),用dfs并剪枝,是一道挺规范的搜索题. 先介绍以下数独吧- 数独(Sudoku)是一种运用纸.笔进行演算的逻辑游戏.玩家需要根据9×9盘面上 ...

  6. POJ 1011 Sticks dfs,剪枝 难度:2

    http://poj.org/problem?id=1011 要把所给的集合分成几个集合,每个集合相加之和ans相等,且ans最小,因为这个和ans只在[1,64*50]内,所以可以用dfs一试 首先 ...

  7. POJ 1011 - Sticks DFS+剪枝

    POJ 1011 - Sticks 题意:    一把等长的木段被随机砍成 n 条小木条    已知他们各自的长度,问原来这些木段可能的最小长度是多少 分析:    1. 该长度必能被总长整除    ...

  8. POJ - 1190 生日蛋糕 dfs+剪枝

    思路:说一下最重要的剪枝,如果当前已经使用了v的体积,为了让剩下的表面积最小,最好的办法就是让R尽量大,因为V = πR 2H,A' = 2πRH,A' = V / R * 2 ,最大的R一定是取当前 ...

  9. Sudoku POJ - 3076 (dfs+剪枝)

    Description A Sudoku grid is a 16x16 grid of cells grouped in sixteen 4x4 squares, where some cells ...

随机推荐

  1. 16 BasicHashTable基本哈希表类(三)——Live555源码阅读(一)基本组件类

    这是Live555源码阅读的第一部分,包括了时间类,延时队列类,处理程序描述类,哈希表类这四个大类. 本文由乌合之众 lym瞎编,欢迎转载 http://www.cnblogs.com/oloroso ...

  2. zabbix之php安装

    转载自: http://www.ttlsa.com/nginx/nginx-php-5_5/ php下载 https://pan.baidu.com/s/1qYGo8bE

  3. 9.5---所有字符串的排列组合(CC150)

    1,这个是自己写的.一直LTE. public static ArrayList<String> getPerms(String str) { if (str == null) { ret ...

  4. phpcms的评论改为留言板研究

    研究背景: phpcms里面默认是没有留言板的,之前我的博客里发过一个二次开发简介,里面有一个简单的留言板,包含前台提供表单,后台留言审核等功能,但是不提供用户登录等操作. 研究思路: phpcms里 ...

  5. 解析sql语句中left_join、inner_join中的on与where的区别

    以下是对在sql语句中left_join.inner_join中的on与where的区别进行了详细的分析介绍,需要的朋友可以参考下 table a(id, type):id     type ---- ...

  6. 关于新中新二代身份证读卡器DKQ-A16D的一些问题

    今天拿到了新中新DKQ-A16D,随机光盘里有以下文件: 我遇到的问题是,如果直接打开\二代征SDK开发包\DLL\测试程序\C#_2008\WindowsFormsApplication1\目录下的 ...

  7. HDU 4942 Game on S♂play(线段树、模拟、扩栈)

    比赛的时候想到这题的大概做法,但由于卡别的水题...就赛后做了... 题意:给一个二叉树,每个结点有一个w[i],有3种操作,0 x表示左旋x,1 x表示右旋x,3 x表示询问x结点的价值,其中,价值 ...

  8. 关于call 和 apply

    权威指南上的说法是:可以将call 和apply看做是某个对象的方法,通过调用方法的形式 间接调用函数:需要重点说明是 :通过call和apply 调用的 函数:具体用法--->如下: 1.先说 ...

  9. 手风琴特效 transition

    <!doctype html> <html> <head> <meta charset="utf-8"> <title> ...

  10. EL表达式中fn函数 (转载)

    JSTL 使用表达式来简化页面的代码,这对一些标准的方法,例如bean的getter/setter方法,请求参数或者context以及 session中的数据的访问非常方便,但是我们在实际应用中经常需 ...