百练1724 ROADS
总时间限制: 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的更多相关文章
- ACM/ICPC 之 递归(POJ2663-完全覆盖+POJ1057(百练2775)-旧式文件结构图)
POJ2663-完全覆盖 题解见首注释 //简单递推-三个米诺牌(3*2)为一个单位打草稿得出规律 //题意-3*n块方格能被1*2的米诺牌以多少种情况完全覆盖 //Memory 132K Time: ...
- 深搜+剪枝 POJ 1724 ROADS
POJ 1724 ROADS Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 12766 Accepted: 4722 D ...
- 百练6255-单词反转-2016正式B题
百练 / 2016计算机学科夏令营上机考试 已经结束 题目 排名 状态 统计 提问 B:单词翻转 查看 提交 统计 提问 总时间限制: 1000ms 内存限制: 65536kB 描述 输入一个 ...
- 百练8216-分段函数-2016正式A题
百练 / 2016计算机学科夏令营上机考试 已经结束 题目 排名 状态 统计 提问 A:分段函数 查看 提交 统计 提问 总时间限制: 1000ms 内存限制: 65536kB 描述 编写程序 ...
- poj 1724:ROADS(DFS + 剪枝)
ROADS Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10777 Accepted: 3961 Descriptio ...
- POJ 1724 ROADS(使用邻接表和优先队列的BFS求解最短路问题)
题目链接: https://cn.vjudge.net/problem/POJ-1724 N cities named with numbers 1 ... N are connected with ...
- dp 加搜索 百练1088 滑雪
描述 Michael喜欢滑雪百这并不奇怪, 因为滑雪的确很刺激.可是为了获得速度,滑的区域必须向下倾斜,而且当你滑到坡底,你不得不再次走上坡或者等待升降机来载你.Michael想知道载一个区域中最长的 ...
- poj 1724 ROADS 很水的dfs
题意:给你N个城市和M条路和K块钱,每条路有话费,问你从1走到N的在K块钱内所能走的最短距离是多少 链接:http://poj.org/problem?id=1724 直接dfs搜一遍就是 代码: # ...
- poj 1724 ROADS 解题报告
题目链接:http://poj.org/problem?id=1724 题目意思:给出一个含有N个点(编号从1~N).R条边的有向图.Bob 有 K 那么多的金钱,需要找一条从顶点1到顶点N的路径(每 ...
随机推荐
- 深度学习Keras框架笔记之AutoEncoder类
深度学习Keras框架笔记之AutoEncoder类使用笔记 keras.layers.core.AutoEncoder(encoder, decoder,output_reconstruction= ...
- js Date对象和数字对象
<script type="text/javascript"> alert(new Date.toLocaleString()); </script> 以本 ...
- python高性能编程 读书笔记
GIL 确保 Python 进程一次只能执行一条指令 ====分析工具cProfile 分析函数耗时line_profiler 逐行分析 heapy 追踪 Python 内存中所有的对象— 这对于消 ...
- 解决Mac外接显示器字体模糊的问题
Mac外接显示器时,除非接的是Apple自家的显示器“ACD”,不然一般会遇到字体模糊发虚的问题.在终端中执行命令: defaults -currentHost write -globalDomain ...
- (1)WIFI信号确定距离
https://blog.csdn.net/PINGER0077/article/details/79482238 ESP8266不需要修改任何库 #include "ESP8266WiFi ...
- MySQL 详细解读undo log :insert undo,update undo
转自aobao.org/monthly/2015/04/01/ 本文是对整个Undo生命周期过程的阐述,代码分析基于当前最新的MySQL5.7版本.本文也可以作为了解整个Undo模块的代码导读.由于涉 ...
- 使用rrweb 进行web 操作录制以及回放
rrweb 是使用typescript 开发的web 操作录制以及回放框架,包含了比较完整的系统组件 rrweb-snapshot 进行dom 与操作实践的关联处理 rrweb 主要包含了record ...
- SIGIR2018 Paper Abstract Reading Notes (1)
1.A Click Sequence Model for Web Search(日志分析) 更好的理解用户行为对于推动信息检索系统来说是非常重要的.已有的研究工作仅仅关注于建模和预测一次交互行为,例如 ...
- Spyder汉化问题
首先感谢李增海大神,以下内容来源于http://www.lizenghai.com 必备条件 1.已安装Spyder 2.Spyder版本在3.X以上 Spyder安装: 1.anaconda下,co ...
- 一次修复linux的efi引导的集中方法总结记录
本文通过MetaWeblog自动发布,原文及更新链接:https://extendswind.top/posts/technical/grub_uefi_repair 起因:EFI分区被删除导致引导问 ...