(POJ 1724)ROADS <DFS + 简单剪枝>
2017-01-04 14:13
225 查看
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代码:
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 1611 The Suspects 初级->数据结构->简单并查集的应用
- poj 2362 Square(简单深搜+剪枝)
- poj 1011-小白算法练习 Sticks 从最简单的做起 dfs+剪枝
- Poj 1724 Roads(DFS 可行性剪枝 最优性剪枝 向量)
- 简单深搜剪枝_Poj_1088
- POJ 1724 ROADS【DFS+剪枝】
- POJ 1416Shredding Company(简单搜索技巧和剪枝)
- POJ 1416 DFS 加简单剪枝
- [POJ 1724]ROADS[SPFA][DFS剪枝]
- DFS(剪枝) POJ 1724 ROADS
- POJ 3281 <最大流,简单的拆点>
- Network Saboteur poj 2531 dfs 简单搜索技巧和剪枝
- POJ 2531 Network Saboteur(简单搜索技巧 + 剪枝)
- DFS + 剪枝 —— POJ 1011
- (poj 1724 ROADS)<二维最短路+DP>
- POJ 3449 Geometric Shapes <几何(简单相交判断)>
- (POJ 1159)Palindrome <最长公共子序列 / 滚动数组优化 / 记忆化搜索> 简单回文数
- ACM: 条件最短路 poj 1724 (没剪枝…
- poj 1724:ROADS(DFS + 剪枝)
- POJ 1416 Shredding Company [暴力搜索 简单剪枝]