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. ITIL与ITSM的联系与区别

    1.ITIL(IT Infrastructure Library)是CCTA(英国国家计算机和电信局)于20世纪80年代末开发的一套IT服务管理标准库,它把英国各个行业在IT管理方面的最佳实践归纳起来 ...

  2. Repository设计模式

    definition: 通过用来访问领域对象的一个类似集合的接口,在领域与数据映射层之间进行协调. advantage: 使用该模式的最大好处就是将领域模型从客户代码和数据映射层之间解耦出来. 理解内 ...

  3. LINQ查询数组里面是否包含某值

    #region linq to 数组            //定义数组,并初始化            string [] array = new string []{"Juan" ...

  4. Centos7 修改SSH 端口

    修改/etc/ssh/sshd_config vi /etc/ssh/sshd_config #Port 22 //这行去掉#号,防止配置不好以后不能远程登录,还得去机房修改,等修改以后的端口能使用以 ...

  5. 关于MySQL count(distinct) 逻辑的一个bug【转】

    本文来自:http://dinglin.iteye.com/blog/1976026#comments 背景 客户报告了一个count(distinct)语句返回结果错误,实际结果存在值,但是用cou ...

  6. C# Winform中如何获取文件名与文件路径

    获取文件名方法: 用System.IO.Path.GetFileName和System.IO.Path.GetFileNameWithoutExtension(无扩展名)的方法 获取文件路径方法: / ...

  7. 数据库SQL语句

    增删改查 --增加 create INSERT INTO table_name (列1, 列2,...) VALUES (值1, 值2,....) --删除 delete DELETE FROM 表名 ...

  8. nyoj138 找球号(二)_离散化

    找球号(二) 时间限制:1000 ms  |  内存限制:65535 KB 难度:5   描述 在某一国度里流行着一种游戏.游戏规则为:现有一堆球中,每个球上都有一个整数编号i(0<=i< ...

  9. App主界面Tab实现方法

    ViewPager + FragmentPagerAdapter 这里模仿下微信APP界面的实现 国际惯例,先看下效果图:   activity_main.xml 布局文件: <?xml ver ...

  10. 【python】datetime获取日期,前一天日期

    1.获取字符串型当前日期 2016-10-09格式 import datetime today = datetime.date.today() #datetime.date类型当前日期 str_tod ...