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 ...
随机推荐
- BZOJ 3365: [Usaco2004 Feb]Distance Statistics 路程统计
Description 一棵树,统计距离不大于 \(k\) 的点对个数. Sol 点分治. 发现自己快把点分治忘干净了... 找重心使所有儿子的最大值尽量小,然后每次处理全部子树,再减去每个子树的贡献 ...
- 这些情况下onReume不应该是你的选择
面试Android程序员的时候问过以下几个基本问题,得到的回答经常不尽人意: 1, Activity A跳转到Activity B,Activity B完成后,Activity A要刷新一下自己的数据 ...
- idea修改默认快捷键
点击file ,选择settings. 输入keymap: 因为多数人使用的都是eclipse,比较容易上手,习惯了eclipse的键位,如 此就能更换. 也可以在对应的操作上,设置自己熟悉的键位.
- YUVviewerPlus使用教程
1.YUVviewerPlus用于播放yuv文件,点击Open File打开yuv文件 2.点击Play播放yuv文件
- MyEclipse 10 集成Maven
第一步:安装Maven,作者安装目录是:D:\Java\apache-maven-3.2.5 第二步:配置本地仓库 maven将每次应用过的项目.文件.jar都会存储到maven的仓库中(默认仓库位置 ...
- Find celebrity
Suppose you are at a party with n people (labeled from 0 to n - 1) and among them, there may exist o ...
- spring + myBatis 常见错误:SQL语法错误
在程序运行时,有时候会出现如下错误: 这个错误通常是你的sqlmapper.xml中sql语句语法有错误.所以请仔细查看你sql语句是否正确,比如{#id}这样写就会报上述错误,其实应该#{id}这样 ...
- Java集合中List的用法
List接口是Collection接口的子接口,List有一个重要的实现类--ArrayList类,List中的元素是有序排列的而且可重复,所以被称为是序列. List可以精确的控制每个元素的插入位置 ...
- ios 关于使用异步网络请求时block回调的内存注意
在一个controller中,使用 NSURLSessionDataTask *dataTask = [[NSURLSession sharedSession] dataTaskWithRequest ...
- HBase独立集群部署
HBase是分布式.面向列式存储的开源数据库,来源于Google的论文BigTable,HBase运行于Hadoop平台之上,不同于一般的关系数据库,是一个适合非结构化数据存储的分布式数据库 安装Hb ...