ROADS POJ - 1724 约束最短路 暴搜 加剪枝
http://poj.org/problem?id=1724
题意:最短路的模板,不过每条边加上一个费用,要求总费用不超过k
题解:不能用dijkstra ,直接暴力,dfs维护len和cost。
普通的剪枝:如果当前的cost大于k直接跳出,如果当前的len大于minlen(目前的最优解),跳出。
另一个剪枝:维护花费一定费用 到达某个点 的最短路minL[v][cost],如果当前的len大于L,则跳出。
ac代码:
#define _CRT_SECURE_NO_WARNINGS
#include<cstring>
#include<cctype>
#include<cstdlib>
#include<cmath>
#include<cstdio>
#include<string>
#include<stack>
#include<ctime>
#include<list>
#include<set>
#include<map>
#include<queue>
#include<vector>
#include<sstream>
#include<iostream>
#include<functional>
#include<algorithm>
#include<memory.h>
//#define INF 0x3f3f3f3f
#define eps 1e-6
#define pi acos(-1.0)
#define e exp(1.0)
#define rep(i,t,n) for(int i =(t);i<=(n);++i)
#define per(i,n,t) for(int i =(n);i>=(t);--i)
#define mp make_pair
#define pb push_back
#define mmm(a,b) memset(a,b,sizeof(a))
//std::ios::sync_with_stdio(false);
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
void smain();
#define ONLINE_JUDGE
int main() {
ios::sync_with_stdio(false);
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
long _begin_time = clock();
#endif
smain();
#ifndef ONLINE_JUDGE
long _end_time = clock();
printf("time = %ld ms.", _end_time - _begin_time);
#endif
return ;
}
const int maxn = 1e4 + ;
const ll mod = 1e5 + ;
const ll INF = ()*(200000ll) + ; ll n, d;
ll k, r, s, l, t;
int minlen;
int cost, len;
int vis[];
int minL[][maxn];
ll a[maxn];
struct road {
int d, L, t;
};
vector<vector<road> >citymap();
//vector<int> a[maxn];
void Run() { }
void dfs(int s) {
if (s == n) { minlen = min(minlen, len); return;}
rep(i, , citymap[s].size() - ) {
road now = citymap[s][i];
if (vis[now.d])continue;
if (cost + now.t > k)continue; if (len + now.L >= minlen)continue;
if (minL[now.d][cost+now.t] <= len + now.L)continue;
len += now.L;
cost += now.t;
minL[now.d][cost] = len;
vis[now.d] = ;
dfs(now.d);
vis[now.d] = ;
len -= now.L;
cost -= now.t; } } void smain() {
cin >> k >> n >> r;
rep(i, , r) {
road R;
int s;
cin >> s >> R.d >> R.L >> R.t;
if (s != R.d)citymap[s].push_back(R); }
rep(i,,)
rep(j, , maxn - ) {
minL[i][j] = << ;
}
memset(vis, , sizeof(vis));
len = , cost = ;
vis[] = ;
minlen = ( << );
dfs();
minlen < << ? cout << minlen : cout << -; }
ROADS POJ - 1724 约束最短路 暴搜 加剪枝的更多相关文章
- hdu4848 DFS 暴搜+ 强剪枝
题意: 给你一个图,然后问你从1出发遍历所有的点的距离和是多少,这里的距离和是每一个点到1的距离的总和,不是选择一条遍历所有点的路径的总长度,时间限制是 8000ms. 思路: ...
- ROADS POJ - 1724(分层最短路)
就是在最短路的基础上 多加了一个时间的限制 , 多一个限制多一维就好了 记住 分层最短路要用dijistra !!! #include <iostream> #include < ...
- 【深搜加剪枝】【HDU1455】【Sticks】
题目大意:有一堆木棍 由几个相同长的木棍截出来的,求那几个相同长的木棍最短能有多短? 深搜+剪枝 具体看代码 #include <cstdio> #include <cstdlib& ...
- HDU 6196 happy happy happy 爆搜加剪枝
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6196 题意:给你长度为n的序列,爸爸和儿子玩一个游戏,儿子先手,儿子每次都选择最左边与最右边最大的那个 ...
- 深搜+剪枝 POJ 1724 ROADS
POJ 1724 ROADS Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 12766 Accepted: 4722 D ...
- POJ 1724 ROADS(bfs最短路)
n个点m条边的有向图,每条边有距离跟花费两个参数,求1->n花费在K以内的最短路. 直接优先队列bfs暴力搞就行了,100*10000个状态而已.节点扩充的时候,dp[i][j]表示到达第i点花 ...
- poj 3080 Blue Jeans(水题 暴搜)
题目:http://poj.org/problem?id=3080 水题,暴搜 #include <iostream> #include<cstdio> #include< ...
- POJ 1945 暴搜+打表 (Or 暴搜+判重)
思路: 呃呃 暴搜+打表 暴搜的程序::稳稳的TLE+MLE (但是我们可以用来打表) 然后我们就可以打表过了 hiahiahia 可以证明最小的那个数不会超过200(怎么证明的我也不知道),然后就直 ...
- POJ 1724 (分层图最短路)
### POJ 1724 题目链接 ### 题目大意: 给你 N 个点 ,M 条有向路,走每条路需要花费 C 元,这段路的长度为 L . 给你 K 元,问你能否从 1 走到 N 点且花费不超过 K 元 ...
随机推荐
- 【C语言】数组名传递给函数,数组的sizeof变为4的原因
C语言中,数组名作为参数传递给函数时,退化为指针,sizeof对指针操作结果应该是4.例子如下: #include<iostream> using namespace std; void ...
- android保持服务不休眠(持续运行)以及唤醒屏幕的方法
假设有这样一个应用场景,一个服务一直在默默的工作(比如即时地获取服务器的消息),即使在屏幕已经因为长时间无操作而关闭,或者用户按了电源键让屏幕关闭,手机进入休眠状态,他必须依然在工作中.一旦从服务器获 ...
- 如何在servlet刚启动时候获取服务器根目录?
public class InitServlet extends HttpServlet{ public static String root; @Override public void init( ...
- [k8s]jenkins配合kubernetes插件实现k8s集群构建的持续集成
另一个结合harbor自动构建镜像的思路: 即code+baseimage一体的方案 - 程序员将代码提交到代码仓库gitlab - 钩子触发jenkins master启动一次构建 - jenkin ...
- Fluent动网格【8】:网格节点运动案例
Fluent动网格中的DEFINE_GRID_MOTION宏允许用户定义网格节点的运动.本案例演示采用DEFINE_GRID_MOTION宏指定边界节点的运动. 案例动网格效果如图所示. 案例描述 本 ...
- AtomicInteger 源码阅读
Package java.util.concurrent.atomic 这是一个小工具包,它的实际作用是提供了很多个无阻塞的线程安全的变量操作工具. 无阻塞的线程安全:其含义就是不使用 synchro ...
- Go Revel - i18n(国际化)
##Messages `Messages`信息是对内容提供翻译的外部文本片段.revel提供了组织每一种语言文本片段的message文件.自动区域查找.基于cookie覆盖的消息嵌套和参数. 术语表: ...
- Jenkins这种构建工具,一般都是内部使用,所以外部基本上不能访问
类似于Jenkins这种构建工具,一般都是内部使用,所以外部基本上不能访问,也可以隔绝外部黑客的入侵等.直接暴露外部是非常不安全的,特别是没有什么安全验证,容易被别人入侵做一些非法的事情! 所以,希望 ...
- SSM框架搭建教程(从零开始,图文结合)
1.准备 IntelliJ IDEA Tomcat JDK Maven mysql spring.springmvc.mybatis 了解 现在假设如上条件你都具备,那么通过我这篇博客 你一定可以整合 ...
- [原]pomelo基础知识(一)
1.pomelo基本介绍 http://blog.gfdsa.net/2013/06/04/pomelo/pomelo_study_two/ 2.如何配置一个gate服务器 (1)首先 需要在game ...