总时间限制: 1000ms    内存限制: 65536kB

描述

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.

输入

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.

输出

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.

样例输入


样例输出


解题思路

从城市1开始深度优先遍历整个图,找到所有能过到达 N 的走法, 选一个最优的。

优化:
1) 如果当前已经找到的最优路径长度为L ,那么在继续搜索的过程中,总长度已经大于L的走法,就可以直接放弃,不用走到底了 。
2) 用midL[k][m] 表示:走到城市k时总过路费为m的条件下,最优路径的长度。若在后续的搜索中,再次走到k时,如果总路费恰好为m,且此时的路径长度已经超过midL[k][m],则不必再走下去了。

AC代码

#include<iostream>
#include<vector>
#include<algorithm>
#include<cstring> using namespace std; int K, N, R, S, D, L, T;//Bob的金额,目标城市编号,道路总数,路的起点和终点,长度和过路费 struct Road
{
int d, L, t;//终点,长度和过路费
}; vector<vector<Road> >cityMap();//邻接表,cityMap[i]是以城市i为起点的道路集合
int minLen = << ;//当前找到的最优路径的长度
int totalLen;//当前走过的总路径长度
int totalCost;//当前总花销
int visited[];//城市是否已经走过的标记
int minL[][];//minL[i][j]表示从1到i点的,花销为j的最短路的长度 void Dfs(int s)//从s开始向N行走
{
if (s == N)//已经达到目标点
{
minLen = min(minLen, totalLen);
return;
}
for (int i = ; i < cityMap[s].size(); i++)//遍历s连接的道路
{
int d = cityMap[s][i].d;//s的第i条道路的目的地
if (!visited[d])//还没有访问过d
{
int cost = totalCost + cityMap[s][i].t;
if (cost > K)continue;//如果花销已经大于Bob手里的钱那就没戏了
//如果当前总路径大于已知的可行最小路径,或者相同总开销情况下,当前总路径大于d城市已知的最小路径,一样没戏
if (totalLen + cityMap[s][i].L >= minLen || totalLen + cityMap[s][i].L >= minL[d][cost])continue;
totalLen += cityMap[s][i].L;
totalCost += cityMap[s][i].t;
minL[d][cost] = totalLen;
visited[d] = ;//重重考验之后d被接纳为最优路径上一点
Dfs(d);//递归解法,继续遍历d
visited[d] = ;
totalCost -= cityMap[s][i].t;
totalLen -= cityMap[s][i].L;//在遍历d之后没有找到目标结点,返回途中把d结点删除
}
}
} int main()
{
cin >> K >> N >> R;
for (int i = ; i < R; i++)
{
int s;
Road r;
cin >> s >> r.d >> r.L >> r.t;
if (s != r.d) cityMap[s].push_back(r);
}
for (int i = ; i < ; i++)
for (int j = ; j < ; j++)
minL[i][j] = << ;
memset(visited, , sizeof(visited));
totalLen = ;
totalCost = ;
visited[] = ;//1城市为起点,已访问
minLen = << ;
Dfs();//启动深搜
if (minLen < ( << )) cout << minLen << endl;
else cout << - << endl;
return ;
}

百练1724 ROADS的更多相关文章

  1. ACM/ICPC 之 递归(POJ2663-完全覆盖+POJ1057(百练2775)-旧式文件结构图)

    POJ2663-完全覆盖 题解见首注释 //简单递推-三个米诺牌(3*2)为一个单位打草稿得出规律 //题意-3*n块方格能被1*2的米诺牌以多少种情况完全覆盖 //Memory 132K Time: ...

  2. 深搜+剪枝 POJ 1724 ROADS

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

  3. 百练6255-单词反转-2016正式B题

    百练 / 2016计算机学科夏令营上机考试 已经结束 题目 排名 状态 统计 提问   B:单词翻转 查看 提交 统计 提问 总时间限制:  1000ms 内存限制:  65536kB 描述 输入一个 ...

  4. 百练8216-分段函数-2016正式A题

    百练 / 2016计算机学科夏令营上机考试 已经结束 题目 排名 状态 统计 提问   A:分段函数 查看 提交 统计 提问 总时间限制:  1000ms 内存限制:  65536kB 描述 编写程序 ...

  5. poj 1724:ROADS(DFS + 剪枝)

    ROADS Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10777   Accepted: 3961 Descriptio ...

  6. POJ 1724 ROADS(使用邻接表和优先队列的BFS求解最短路问题)

    题目链接: https://cn.vjudge.net/problem/POJ-1724 N cities named with numbers 1 ... N are connected with ...

  7. dp 加搜索 百练1088 滑雪

    描述 Michael喜欢滑雪百这并不奇怪, 因为滑雪的确很刺激.可是为了获得速度,滑的区域必须向下倾斜,而且当你滑到坡底,你不得不再次走上坡或者等待升降机来载你.Michael想知道载一个区域中最长的 ...

  8. poj 1724 ROADS 很水的dfs

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

  9. poj 1724 ROADS 解题报告

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

随机推荐

  1. 项目Alpha冲刺——集合

    作业描述 课程: 软件工程1916|W(福州大学) 作业要求: 项目Alpha冲刺(团队) 团队名称: 火鸡堂 作业目标: 完成项目Alpha冲刺 团队信息 队名:火鸡堂 队员学号 队员姓名 博客地址 ...

  2. c#2.0锐利体验《泛型编程》读书笔记

    1.c#泛型及机制 Class Stack<T> { } T 其实为type的缩小,不过也可为其他字符代替T ,被称为“泛型类型”  T为晚绑定的,在编译的时候还不能确定T的确切类型. 2 ...

  3. MSSQL行车列规则

    行转列,是SQL中经常会遇到的一个问题,并且分为静态转换和动态转换,所谓静态转换即在转换的行数已知或固定:动态转换则为转换的行数不固定. 转换的方法一般采用case when语句或pivot(MSSQ ...

  4. edgedb 开发环境运行

    以下是一篇来自官方的edgedb 开发环境搭建说明,实际上我以前自己也摸索过一个,基本方法一样,一些是官方的做一个 简单的记录 预备工具 GNU make version 3.80 or newer; ...

  5. 2019-8-26 LinkedHashMap 转 List [java.util.LinkedHashMap cannot be cast to com.zq.dataservice.bean.Index]

    java.util.LinkedHashMap cannot be cast to com.zq.dataservice.bean.Index 上述错误是在做一个趋势投资demo时遇到的. 说的是链式 ...

  6. IIS 站点配置文件

    IIS 站点配置文件  C:/Windows/System32/inetsrv/config/applicationHost.config 配置文件示例: <system.application ...

  7. Phalcon框架的编译安装 内存不足的解决办法

    对症解决 有两种解决方法,一种是提升ECS系统内存.但是却要真金白银跟阿里云去购买的.另一种,则是手动创建swap交换文件.下面来介绍第二种方法. 第一步:首先确定系统是否已经开启swap交换分区: ...

  8. c博客作业01--顺序分支结构

    0.展示PTA总分 1.本章学习总结 1.1 学习内容总结 1.运算符需注意的要点 '/'的左右两边如果均为整型数,其结果也为整型:'%'的左右两边只能为整型数: 优先级:逻辑运算符<关系运算符 ...

  9. el-table里面的列需要对比两个返回参数

    需求是这样的--- 已发布时间超过30分钟,显示黄色,超过一个钟显示红色 现在后台返回的时间的格式是2018-10-22 11:23:23的格式 做法是: 第一步: 先将后台返回的格式转化为时间戳,然 ...

  10. OpenFOAM——三角腔驱流

    本算例来自<ANSYS Fluid Dynamics Verification Manual>中的VMFL011: Laminar Flow in a Triangular Cavity ...