百练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的路径(每 ...
随机推荐
- Alpha冲刺(6/10)——2019.4.29
所属课程 软件工程1916|W(福州大学) 作业要求 Alpha冲刺(6/10)--2019.4.29 团队名称 待就业六人组 1.团队信息 团队名称:待就业六人组 团队描述:同舟共济扬帆起,乘风破浪 ...
- php数组打乱顺序
shuffle() PHP shuffle() 函数随机排列数组单元的顺序(将数组打乱).本函数为数组中的单元赋予新的键名,这将删除原有的键名而不仅是重新排序. 语法: bool shuffle ( ...
- windows 10 下使用Navicat for oracle 数据库还原
一.前期准备 1.安装windows 10系统 2.安装oracle 11g 数据库 3.安装PLsql(也不需要) 4.安装sqlplus(这个必须有) 5.使用下面这个东西新建数据库(不懂创建的话 ...
- flask + websocket实现简单的单聊和群聊
单聊 from flask import Flask,request,render_template from geventwebsocket.handler import WebSocketHand ...
- java接口的成员变量的修饰符
前言:c++学的java都忘记了不少 interface(接口)可将其想象为一个"纯"抽象类.它允许创建者规定一个类的基本形式:方法名.自变量列表以及返回类型,但不实现方法主体 接 ...
- LeetCode 439. Ternary Expression Parser
原题链接在这里:https://leetcode.com/problems/ternary-expression-parser/description/ 题目: Given a string repr ...
- RSDS pdb格式
本描述了“RSDS”或“DS”类型的pdb(程序数据库)文件的格式,这些文件是由Miscrosoft的link.exe从版本7及更高版本发出的. 什么是PDB文件? 如果选择了/DEBUG选项或/DE ...
- circus 做为批处理的守护进程
circus 是集成了zeromq,使用python编写的一个进程以及socket 管理工具,使用circus 的进程管理,我们可以用来进行批任务的 处理,同时又能保证任务的准确 项目使用docker ...
- edgedb 开发环境运行
以下是一篇来自官方的edgedb 开发环境搭建说明,实际上我以前自己也摸索过一个,基本方法一样,一些是官方的做一个 简单的记录 预备工具 GNU make version 3.80 or newer; ...
- 4-微信小程序开发(小程序默认页面函数说明)
https://www.cnblogs.com/yangfengwu/p/11601299.html 源码下载链接: 或者 首先说一下,怎么让自己的一个项目更改名字成为一个新的项目 然后用软件导入项目 ...