深搜+剪枝 POJ 1724 ROADS

POJ 1724 ROADS

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 12766   Accepted: 4722

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
#include<iostream>
using namespace std;
#include<cstdio>
#include<cstring>
#define N 105
#define R 10005
#define inf (1<<31)-1
struct Edge{
    int v,last,lenth,val;
}edge[R];
int topt=0,head[N],k,n,r,q;
bool vis[N]={0};
int ans;
void input()
{
    scanf("%d%d%d",&k,&n,&r);
    for(int i=1;i<=r;++i)
    {
        scanf("%d%d%d%d",&q,&edge[i].v,&edge[i].lenth,&edge[i].val);
        edge[i].last=head[q];
        head[q]=i;
    }
}
void dfs(int u,int cost,int len)
{
    if(cost>k||len>=ans) return;
    if(u==n)
    {
        ans=min(ans,len);
        return;
    }
    for(int l=head[u];l;l=edge[l].last)
    {
        if(vis[edge[l].v]) continue;
        vis[edge[l].v]=true;
        dfs(edge[l].v,cost+edge[l].val,len+edge[l].lenth);
        vis[edge[l].v]=false;
    }
}
int main()
{
    input();
    ans=inf;
    dfs(1,0,0);
    if(ans==inf) printf("-1");
    else printf("%d",ans);
    return 0;
}
时间: 2024-10-20 14:46:16

深搜+剪枝 POJ 1724 ROADS的相关文章

poj 1724:ROADS(DFS + 剪枝)

ROADS Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10777   Accepted: 3961 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

poj 1724 ROADS (bfs+优先队列)

题目链接 题意:在有费用k限制的条件下,求从1到n的最短距离,如果最短距离相同求费用最小的,边为有向边,其中可能有 多个相同的源点和目标点,但是距离和费用不同. 分析:用bfs和邻接表来把每一个边搜一下,因为用了优先队列,所以先到n的一定是最小的 . 1 #include <iostream> 2 #include <cstring> 3 #include <cstdlib> 4 #include <cmath> 5 #include <cstdio&

poj 1724 ROADS 解题报告

题目链接:http://poj.org/problem?id=1724 题目意思:给出一个含有N个点(编号从1~N).R条边的有向图.Bob 有 K 那么多的金钱,需要找一条从顶点1到顶点N的路径(每条边需要一定的花费),前提是这个总花费  <= K. 首先这里很感谢 yunyouxi0 ,也就是我们的ACM队长啦~~~,他一下子指出了我的错误——存储重边的错误.这条题卑鄙的地方是,有重边,discuss 中的数据过了也不一定会AC啦.大家不妨试试这组数据(队长深情奉献^_^) 2 2 2 1

POJ 1724 ROADS

点击打开链接 ROADS Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10202   Accepted: 3786 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 t

UVA1374 - Power Calculus(迭代深搜+剪枝)

题目链接 题意:给出x和正整数n,问最少需要几次乘除法 可以得到n = x^m 思路:其实是关于指数的操作,即从1到m最少的步数.我们可以先确定最少步数m,然后进行迭代,迭代的过程也就是判断通过相加减所得到的数可以在m次操作中等于n,如果符合,m即为最小步数,如果不符合,m++,进行下一次迭代.迭代过程中要注意剪枝,即剩余的次数如果每次都是取最大值相加还是比n小的话,就直接跳出. 代码: #include <iostream> #include <cstdio> #include

poj1011(深搜+剪枝)

题意:给m根木棍,将它们重新拼成n根一样长的木棍,并使得n尽量大(即每个新木棍尽量短). 解法:经典的搜索题目.从小到大枚举拼成的新木棍长度,每次枚举进行一次深搜.这题关键是如何剪枝. 1.当枚举的长度不能整除总长度的时候,剪枝:(这个很显然) 2.先将木棍从长到短排序,枚举时先尝试长的木棍.(先枚举长的可以使得搜索深度不至于过深) 3.深搜时,不要企图通过换掉一个新木棍的第一根来改变失败的局面(换掉第一根A,那么A也会在以后的新木棍中被使用,但是已经证明了A无法拼成了,所以不必再尝试下去了)

hdu 1518 Square(深搜+剪枝)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1518 题目大意:根据题目所给的几条边,来判断是否能构成正方形,一个很好的深搜应用,注意剪枝,以防超时! 1 #include <iostream> 2 #include <cstdio> 3 #include<algorithm> 4 #include <cstring> 5 using namespace std; 6 int ap[30],visit[30]

poj 1724 ROADS(dfs)

http://poj.org/problem?id=1724 大致题意:N个城市由R条单向路连通,每条路(S,D)之间有两个因素:路的长度L和路的花费T.现要从城市1到达城市N,求花费在K以内的最短路程. 思路:很明显的dfs(他们都说很明显的spfa...).不过dfs有几点注意的地方: 建立邻接表不能用vector存,要用链表的形式,采用头插法. dfs的时候,在递归节点v之前,要先预判断一下到达v之后总花费是否大于k,若大于K就跳过,不必再调用v节点,这样会省很多时间.对于路程的处理也同样

poj1190 生日蛋糕(深搜+剪枝)

题目链接:poj1190 生日蛋糕 解题思路: 深搜,枚举:每一层可能的高度和半径 确定搜索范围:底层蛋糕的最大可能半径和最大可能高度 搜索顺序:从底层往上搭蛋糕,在同一层尝试时,半径和高度都是从大到小试 剪枝: ①已建好的面积已经超过目前求得的最优表面积,或者预见到搭完后面积一定会超过目前最优表面积,则停止搭建(最优性剪枝) ②预见到再往上搭,高度已经无法安排,或者半径无法安排,则停止搭建(可行性剪枝) ③还没搭的那些层的体积,一定会超过还缺的体积,则停止搭建(可行性剪枝) ④还没搭的那些层的