(POJ 1724)ROADS <DFS + 简单剪枝>
来源:互联网 发布:淘宝小二布置作业短信 编辑:程序博客网 时间:2024/05/08 11:29
ROADS
Description
N cities named with numbers 1 … N are connected with one-way roads. Each road has two parameters associated with it : the road length and the toll that needs to be paid for the road (expressed in the number of coins).
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 first line of the input contains the integer K, 0 <= K <= 10000, maximum number of coins that Bob can spend on his way.
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
The first and the only line of the output should contain the total length of the shortest path from the city 1 to the city N whose total toll is less than or equal K coins.
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
CEOI 1998
题意:
n个城市之间,有r条单向边,每条边有长度l和费用t.现在要从1号城市去n号城市,且只有k元钱。问你在满足钱数的情况下,最短路径长度为多少?
分析:
直接进行DFS搜索出所有的路径,不过记得用当前长度,和当前费用进行剪枝即可。1A
AC代码:
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <cmath>using namespace std;const int maxn = 10010;struct edge{ int v,l,t,next;}edges[maxn];int k,n,r,e,ans;bool vis[maxn];int head[maxn];void addedge(int u,int v,int l,int t){ edges[e].v = v; edges[e].l = l; edges[e].t = t; edges[e].next = head[u]; head[u] = e++;}void dfs(int u,int len,int cost){ if(cost > k) return; //剪枝 if(len >= ans) return; //剪枝 if(u == n) { if(len < ans) ans = len; return; } for(int i=head[u];i!=-1;i=edges[i].next) { int v = edges[i].v; if(!vis[v]) { vis[v] = true; dfs(v,len+edges[i].l,cost+edges[i].t); vis[v] = false; } }}int main(){ int u,v,l,t; while(scanf("%d%d%d",&k,&n,&r)!=EOF) { e = 0; memset(head,-1,sizeof(head)); for(int i=0;i<r;i++) { scanf("%d%d%d%d",&u,&v,&l,&t); addedge(u,v,l,t); } ans = 99999999; memset(vis,false,sizeof(vis)); vis[1] = true; dfs(1,0,0); if(ans < 99999999) printf("%d\n",ans); else printf("-1\n"); } return 0;}
- (POJ 1724)ROADS <DFS + 简单剪枝>
- POJ 1724 ROADS(DFS剪枝)
- ROADS poj 1724(dfs + 剪枝)
- POJ 1724 ROADS DFS+剪枝
- POJ 1724 ROADS【DFS+剪枝】
- [POJ 1724]ROADS[SPFA][DFS剪枝]
- Poj 1724 Roads(DFS 可行性剪枝 最优性剪枝 向量)
- POJ - 1724 ROADS(bfs+剪枝)
- poj 1724 ROADS(dfs)
- POJ 1724 ROADS (DFS)
- POJ 1724 ROADS (搜索+剪枝)
- poj-1724-ROADS(dfs)
- poj 1724 dfs+剪枝
- POJ 1724 ROADS 启发式DFS
- POJ-2078(dfs + 剪枝)
- poj 3411 Paid Roads 简单的dfs
- POJ 3411 Paid Roads 简单DFS
- POJ 1416 DFS 加简单剪枝
- matlab如何在文中定义函数
- JSP九大内置对象详解
- 【算法和数据结构】1.2.4–排序算法之归并排序和快速排序(C++实现)
- js之数组,对象,类数组对象
- android和ios GoogleMap画导航线路图 路径规划(Directions)
- (POJ 1724)ROADS <DFS + 简单剪枝>
- iMessage 扩展开发调试的时候
- Sublime Text install package
- mysql 'performance_schema'.'session_variables' 问题处理
- yum国内镜像配置
- win7下caffe的编译(包括caffe_python接口配置)以及python27下numpy、matplotlib、scipy等的安装
- Android Animation动画总结
- 快速多目标检测——YOLO9000
- JavaBeans规范