题目传送门

题意:问从1到n的最短路径,同时满足花费总值小于等于k

分析:深搜+剪枝,如果之前走过该点或者此时的路劲长度大于最小值就不进行搜索。

/************************************************
* Author :Running_Time
* Created Time :2015/11/11 星期三 10:14:14
* File Name :POJ_1724.cpp
************************************************/ //#include <bits/stdc++.h>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <cmath>
using namespace std; #define lson l, mid, rt << 1
#define rson mid + 1, r, rt << 1 | 1
typedef long long ll;
const int N = 1e2 + 10;
const int E = 1e4 + 10;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
const double EPS = 1e-10;
const double PI = acos (-1.0);
int k, n, m;
struct Edge {
int v, w, len, nex;
Edge () {}
Edge (int v, int w, int len, int nex) : v (v), w (w), len (len), nex (nex) {}
}edge[E];
int head[N], d[N];
bool vis[N];
int e; void init(void) {
memset (head, -1, sizeof (head));
e = 0;
} void add_edge(int u, int v, int w, int len) {
edge[e] = Edge (v, w, len, head[u]);
head[u] = e++;
} int ans;
void DFS(int u, int L, int cost) {
if (u == n) {
if (L < ans) ans = L;
return ;
}
for (int i=head[u]; ~i; i=edge[i].nex) {
Edge &r = edge[i];
if (cost - r.w < 0 || L + r.len > ans) continue;
if (vis[r.v]) continue;
vis[r.v] = true;
DFS (r.v, L + r.len, cost - r.w);
vis[r.v] = false;
}
} int main(void) {
while (scanf ("%d%d%d", &k, &n, &m) == 3) {
init ();
for (int u, v, w, len, i=1; i<=m; ++i) {
scanf ("%d%d%d%d", &u, &v, &len, &w);
add_edge(u, v, w, len);
}
ans = INF;
memset (vis, false, sizeof (vis));
vis[1] = true;
DFS (1, 0, k);
if (ans == INF) ans = -1;
printf ("%d\n", ans);
} //cout << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n"; return 0;
}

  

DFS(剪枝) POJ 1724 ROADS的更多相关文章

  1. 深搜+剪枝 POJ 1724 ROADS

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

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

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

  3. DFS(剪枝) POJ 1011 Sticks

    题目传送门 /* 题意:若干小木棍,是由多条相同长度的长木棍分割而成,问最小的原来长木棍的长度: DFS剪枝:剪枝搜索的好题!TLE好几次,终于剪枝完全! 剪枝主要在4和5:4 相同长度的木棍不再搜索 ...

  4. poj 1724 ROADS 很水的dfs

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

  5. poj 1724 ROADS 解题报告

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

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

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

  7. POJ 1724 ROADS【最短路/搜索/DP】

    一道写法多样的题,很具有启发性. 具体参考:http://www.cnblogs.com/scau20110726/archive/2013/04/28/3050178.html http://blo ...

  8. POJ 1724 ROADS(二维SPFA)

    题目链接 用STL实现超时了,用普通队列500+,看到spfa,反应太迟钝了. #include <cstring> #include <cstdio> #include &l ...

  9. POJ 1724 Roads

    题意:有R条路,每条路都有一定的路长和花费,问在总的花费小于一定的值的情况下,从1到N的最短路程         注意:这里两点之间单向边,且可能存在很多条路,所以只能用邻接表存储.思路:用dijks ...

随机推荐

  1. DCMTK开源库的学习笔记4:利用ini配置文件对dcm影像进行归档

    转:http://blog.csdn.net/zssureqh/article/details/8846337 背景介绍: 医学影像PACS工作站的服务端需要对大量的dcm文件进行归档,写入数据库处理 ...

  2. System.SysUtils.TMarshaller 与 System.TMarshal

    转自:http://www.cnblogs.com/del/archive/2013/06/10/3130974.html TMarshaller(结构) 基于 TMarshal(是有一大堆的 cla ...

  3. 利用nginx泛域名解析配置二级域名和多域名

    利用nginx泛域名解析配置二级域名和多域名 网站的目录结构为 html ├── bbs └── www html为nginx的安装目录下默认的存放源代码的路径. bbs为论坛程序源代码路径 www为 ...

  4. 深入学习微框架:Spring Boot - NO

    http://blog.csdn.net/hengyunabc/article/details/50120001 Our primary goals are: Provide a radically ...

  5. ffplay 2.5.3 媒体播放器

    下载地址 http://pan.baidu.com/s/1bnlMYB1 一定要解压到 D:\ffmpeg\ 目录下 双击 OpenWith_FFPlay.reg 注册ffplay 在视频文件名上面, ...

  6. css样式自适应,支持数字

    td加上style="word-break: break-all;word-wrap: break-word;"样式即可

  7. java操作AJAX

    1,get方式的AJAX function sendAjaxReq() { //1,创建ajax引擎 XMLHttpRequest对象 var req = new XMLHttpRequest() | ...

  8. [Git] Git 文件归档, include submodule

      git archive命令,可以对任意提交对应的目录树建立归档. $ git archive -o latest.zip HEAD  基于最新提交建立归档文件latest.zip $ git ar ...

  9. GPU基本概念详解

    §1 个 multiprocessor <-> 1个instruction unit  <-> 8 个processor  <-> 在一个warp中执行  < ...

  10. Girls and Boys(poj 1466)

    题目描述: 给出一系列男女配对意愿信息.求一个集合中的最大人数,满足这个集合中两两的人不能配对. /* 二分图的最大独立集 因为没有给出具体的男生和女生,所以可以将数据扩大一倍,即n个男生,n个女生, ...