总时间限制: 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. python开发笔记-DataFrame的使用

    今天详细做下关于DataFrame的使用,以便以后自己可以翻阅查看 DataFrame的基本特征: 1.是一个表格型数据结构 2.含有一组有序的列 3.大致可看成共享同一个index的Series集合 ...

  2. 【AirTest自学】AirTest工具介绍和入门学习(一)

    ==================================================================================================== ...

  3. 爬虫 - 解析库之Beautiful Soup

    了解Beautiful Soup 中文文档: Beautiful Soup 是一个可以从HTML或XML文件中提取数据的Python库.它能够通过你喜欢的转换器实现惯用的文档导航,查找,修改文档的方式 ...

  4. Making Huge Palindromes LightOJ - 1258

    题目链接:LightOJ - 1258 1258 - Making Huge Palindromes   PDF (English) Statistics Forum Time Limit: 1 se ...

  5. 海康相机开发(1) SDK安装和开发

    1.1 安装包获取 从官网下载最新版本的MVS安装包,支持Windows xp.Windows 7.Windows 8.Windows 10的32和64位系统.安装过程默认即可. 官网下载链接:htt ...

  6. .net web开发——文件的上传和下载

    以ASP.NET Core WebAPI 作后端 API ,用 Vue 构建前端页面,用 Axios 从前端访问后端 API ,包括文件的上传和下载. 准备文件上传的API #region 文件上传  ...

  7. C++对象内存布局,this指针,对象作为参数,作为返回值

    class TestClass { public: void setNum(int num) { m_num1 = num; } int getNum() { return m_num1; } pri ...

  8. 加密hashlib模块

    目录 hashlib和hmac模块: hashlib模块: -hash: 特点: 大致流程: 注意: hmac模块: 特点: 注意:hmac模块只接受二进制数据的加密 hashlib和hmac模块: ...

  9. 【转】Java 8新特性(四):新的时间和日期API

    Java 8另一个新增的重要特性就是引入了新的时间和日期API,它们被包含在java.time包中.借助新的时间和日期API可以以更简洁的方法处理时间和日期. 在介绍本篇文章内容之前,我们先来讨论Ja ...

  10. Mac下Pycharm中升级pip失败,通过终端升级pip

    使用 Pycharm 使,需要下载相关的第三方包,结果提示安装失败,提示要升级 pip 版本,但是通过 Pycharm 重新安装却失败,原因可能是出在通过 Pycharm 时升级 pip 是没有权限的 ...