DFS(剪枝) POJ 1724 ROADS
题意:问从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的更多相关文章
- 深搜+剪枝 POJ 1724 ROADS
POJ 1724 ROADS Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 12766 Accepted: 4722 D ...
- poj 1724:ROADS(DFS + 剪枝)
ROADS Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10777 Accepted: 3961 Descriptio ...
- DFS(剪枝) POJ 1011 Sticks
题目传送门 /* 题意:若干小木棍,是由多条相同长度的长木棍分割而成,问最小的原来长木棍的长度: DFS剪枝:剪枝搜索的好题!TLE好几次,终于剪枝完全! 剪枝主要在4和5:4 相同长度的木棍不再搜索 ...
- 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的路径(每 ...
- POJ 1724 ROADS(使用邻接表和优先队列的BFS求解最短路问题)
题目链接: https://cn.vjudge.net/problem/POJ-1724 N cities named with numbers 1 ... N are connected with ...
- POJ 1724 ROADS【最短路/搜索/DP】
一道写法多样的题,很具有启发性. 具体参考:http://www.cnblogs.com/scau20110726/archive/2013/04/28/3050178.html http://blo ...
- POJ 1724 ROADS(二维SPFA)
题目链接 用STL实现超时了,用普通队列500+,看到spfa,反应太迟钝了. #include <cstring> #include <cstdio> #include &l ...
- POJ 1724 Roads
题意:有R条路,每条路都有一定的路长和花费,问在总的花费小于一定的值的情况下,从1到N的最短路程 注意:这里两点之间单向边,且可能存在很多条路,所以只能用邻接表存储.思路:用dijks ...
随机推荐
- cf.295.B Two Buttons (bfs)
Two Buttons time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...
- unity3d 加密资源并缓存加载
原地址:http://www.cnblogs.com/88999660/archive/2013/04/10/3011912.html 首先要鄙视下unity3d的文档编写人员极度不负责任,到发帖为止 ...
- C语言中%d,%o,%f,%e,%x的意义
printf(格式控制,输出列表) 格式控制包括格式说明和格式字符. 格式说明由“%”和格式字符组成,如%d%f等.它的作用是将输出的数据转换为指定的格式输出.格式说明总是由“%”字符开始的.不同类型 ...
- POJ 3083
---恢复内容开始--- http://poj.org/problem?id=3083 题目大意就是给你要你从S走到E,且只有.代表的地方才是可以走的,有三种方式的走法. 一.是向左优先转,从S到E的 ...
- Python发布Django项目的pyc版脚本
import os import sys from py_compile import compile #print "argvs:",sys.argv if len(sys.ar ...
- tcp ip detatils
tcp ip detatils 8.关于TCP协议,下面哪种说法是错误的()A.TCP关闭连接过程中,两端的socket都会经过TIME_WAIT状态B.对一个Established状态的TCP连接, ...
- sockaddr struct 类型重定义
windows.h和winsock2.h有类型重定义我是知道的,本来就一个库来说没问题,把winsock2放到windows.h前或先定义WIN32_LEAN_AND_MEAN都能解决问题但现的出了问 ...
- java\c程序的内存分配
JAVA 文件编译执行与虚拟机(JVM)介绍 Java 虚拟机(JVM)是可运行Java代码的假想计算机.只要根据JVM规格描述将解释器移植到特定的计算机上,就能保证经过编译的任何Java代码能够在该 ...
- Android之canvas详解
首先说一下canvas类: Class Overview The Canvas class holds the "draw" calls. To draw something, y ...
- svn 创建
1.ps aux | grep svn 杀掉进程 2.svnadmin create /svnrepertory/SVNwangping 创建svn仓库; 3.修改3个文件 4.svnserve -d ...