(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;}
1 0
原创粉丝点击