poj 1724:ROADS(DFS + 剪枝)
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 10777 | Accepted: 3961 |
Description
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.
Input
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.
Output
If such path does not exist, only number -1 should be written to the output.
Sample Input
5
6
7
1 2 2 3
2 4 3 3
3 4 2 4
1 3 4 1
4 6 2 1
3 5 2 0
5 4 3 2
Sample Output
11
Source
DFS + 剪枝
题 意

思 路
代 码
#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std; #define MAXN 110 struct Node{
int city;
int len;
int toll;
Node* next;
}city[MAXN]; //邻接表 bool isv[MAXN]; int K,n,r,s;
int Min; void dfs(int c,int l,int k)
{
//k为剩下的钱
if(k<)
return ;
if(l>=Min) //如果当前走过的路的长度超过了记录的最小值,则退出
return ;
if(c==n && l<Min){ //到达n城市
Min = l;
return ;
} Node* p = city[c].next;
while(p){
if(!isv[p->city]){ //没走过
isv[p->city] = true;
dfs(p->city,l + p->len,k - p->toll); //进入下一个城市
isv[p->city] = false;
}
p = p->next;
}
} int main()
{
while(scanf("%d",&K)!=EOF){
scanf("%d",&n);
scanf("%d",&r); memset(city,NULL,sizeof(city));
memset(isv,,sizeof(isv));
Min = 0x7ffffff; while(r--){
scanf("%d",&s);
Node* p = (Node*)malloc(sizeof(Node));
scanf("%d%d%d",&p->city,&p->len,&p->toll);
p->next = city[s].next;
city[s].next = p;
} isv[] = true;
dfs(,,K); if(Min==0x7ffffff)
printf("-1\n");
else
printf("%d\n",Min);
}
return ;
}
Freecode : www.cnblogs.com/yym2013
poj 1724:ROADS(DFS + 剪枝)的更多相关文章
- 深搜+剪枝 POJ 1724 ROADS
POJ 1724 ROADS Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 12766 Accepted: 4722 D ...
- poj 1724 ROADS 很水的dfs
题意:给你N个城市和M条路和K块钱,每条路有话费,问你从1走到N的在K块钱内所能走的最短距离是多少 链接:http://poj.org/problem?id=1724 直接dfs搜一遍就是 代码: # ...
- DFS(剪枝) POJ 1724 ROADS
题目传送门 题意:问从1到n的最短路径,同时满足花费总值小于等于k 分析:深搜+剪枝,如果之前走过该点或者此时的路劲长度大于最小值就不进行搜索. /************************** ...
- poj 1724 ROADS 解题报告
题目链接:http://poj.org/problem?id=1724 题目意思:给出一个含有N个点(编号从1~N).R条边的有向图.Bob 有 K 那么多的金钱,需要找一条从顶点1到顶点N的路径(每 ...
- 数独问题的介绍及POJ 2676-Sudoku(dfs+剪枝)
知道是数独问题后犹豫了一下要不要做(好像很难的样纸==.),用dfs并剪枝,是一道挺规范的搜索题. 先介绍以下数独吧- 数独(Sudoku)是一种运用纸.笔进行演算的逻辑游戏.玩家需要根据9×9盘面上 ...
- POJ 1011 Sticks dfs,剪枝 难度:2
http://poj.org/problem?id=1011 要把所给的集合分成几个集合,每个集合相加之和ans相等,且ans最小,因为这个和ans只在[1,64*50]内,所以可以用dfs一试 首先 ...
- POJ 1011 - Sticks DFS+剪枝
POJ 1011 - Sticks 题意: 一把等长的木段被随机砍成 n 条小木条 已知他们各自的长度,问原来这些木段可能的最小长度是多少 分析: 1. 该长度必能被总长整除 ...
- POJ - 1190 生日蛋糕 dfs+剪枝
思路:说一下最重要的剪枝,如果当前已经使用了v的体积,为了让剩下的表面积最小,最好的办法就是让R尽量大,因为V = πR 2H,A' = 2πRH,A' = V / R * 2 ,最大的R一定是取当前 ...
- Sudoku POJ - 3076 (dfs+剪枝)
Description A Sudoku grid is a 16x16 grid of cells grouped in sixteen 4x4 squares, where some cells ...
随机推荐
- IOS系统ipa软件包在线安装
如果iis不支持ipa和plist下载,可以添加memi类型. ipa application/x-plist plist application/xml 下载用普通链接即可: <a h ...
- Colorful tree
cnbb 我被数组清零卡了一天.. 子树改色询问子树颜色数.. 先考虑颜色为x的节点对祖先答案的贡献,那么我们考虑把所有这些节点都搞出来,按dfs序排序,然后考虑每个节点a掌管的祖先是它和按dfs序的 ...
- [20160731]read a file and print it on the screen
//read a file and print it on the screen import java.io.*; public class MyPrintStreamTest2{ public s ...
- Android studio教程
Android studio教程: http://jingyan.baidu.com/season/44062
- java微信接口之——获取access_token
本文转自http://www.cnblogs.com/always-online/category/598553.html 一.微信获取access_token接口简介 1.请求:该请求是GET方式请 ...
- HTML5之sessionStorage
http://www.css88.com/archives/tag/sessionstorage http://blog.csdn.net/qxs965266509/article/details/1 ...
- 学习Selenium2Library的好例子
最近好几个人问我有没有好的例子可以帮助学习Selenium2Library怎么用.对于公司同事,可以把脚本直接给过去,其他人则不行了.所以一直想做一个好的学习样例,这个样例应该有如下特性: 能够非常好 ...
- Linux集群配置ntp时间同步服务
集群中时间不同步有可能会让大数据的应用程序运行混乱,造成不可预知的问题,比如Hbase,当时间差别过大时就会挂掉,所以在大数据集群中,ntp服务,应该作为一种基础的服务,以下在演示在CentOS 7. ...
- python模块名和文件名冲突解决
对于python初学者,很容易练习到一个随机数生成的程序,代码如下: #!/usr/bin/python import random print(random.randint(12,20)) 这个小程 ...
- ACM/ICPC 之 数据结构-线段树思想(POJ2182,含O(n^2)插入式解法)
这道题在一定程度上体现了线段树的一种用法,解决的问题是:对于总计n个元素的第i个元素,已知其在[1,i]上部分序列的排名,求第i个元素在所有n个元素中的排名. 当然这道题数据比较水,所以用O(n^2) ...