poj 1724 ROADS 搜索

本文介绍了一个寻找最短路径的问题,具体是在限定花费下从城市1到城市N的最短路径。通过三种不同的算法实现——深度优先搜索(两种变体)和广度优先搜索,详细展示了如何解决此类问题。

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


题意:针对数据而言

5  //总花费
6  //从1——n几个城市
7  //七条路径(如下)
1 2 2 3 //从1到2路程为2,花费为3

求在固定花费下的最短路

深搜1

#include<stdio.h>
#include<string.h>
#include<algorithm>
#define INF 0x3f3f3f3f
using namespace std;
int tot,n;
int head[1010],v[1010];
int sum;
struct p
{
    int x;
    int y;
    int len;
    int cost;
    int next;
}e[10010];
void dfs(int pos,int length,int price)
{
    if(price>tot) return; //花费太大
    if(length>sum) return; //已大于找到的最短路
    if(pos==n&&price<=tot&&sum>length)
    {
        sum=length;
        return;
    }
    for(int i=head[pos];i!=-1;i=e[i].next)
    {
        int k=e[i].y;
        if(v[k]==0)
        {
            if(price+e[i].cost<=tot)
            {
                v[k]=1; //标记
                dfs(k,length+e[i].len,price+e[i].cost);
                v[k]=0; //回溯
            }
        }

    }
}
int main()
{
    int m;
    int a,b,c,d;
    scanf("%d%d%d",&tot,&n,&m);
    int num=0;
    memset(head,-1,sizeof(head));
    memset(v,0,sizeof(v));
    sum=INF;
    for(int i=0;i<m;i++)
    {
        scanf("%d%d%d%d",&a,&b,&c,&d);
        e[i].x=a;
        e[i].y=b;
        e[i].len=c;
        e[i].cost=d;
        e[i].next=head[a]; //建立邻接表
        head[a]=i;
    }
    dfs(1,0,0);
    if(sum!=INF)
        printf("%d\n",sum);
    else
        printf("-1\n");
    return 0;
}

深搜2

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<vector>
#define INF 0x3f3f3f3f
using namespace std;
int tot,sum,n,v[110];
int mint[110][10010];
struct p
{
    int yy;
    int len;
    int cost;
};
vector<p>e[110];
void dfs(int x,int y,int z)
{
    if(x==n)
    {
        sum=min(sum,z);
        return;
    }
    for(int i=0; i<e[x].size(); i++)
    {
        if(v[e[x][i].yy]==0)
        {
            if(z+e[x][i].len>=sum||y+e[x][i].cost>tot||z+e[x][i].len>=mint[e[x][i].yy][e[x][i].cost+y]) //剪枝1,现在的路程已经大于等于所求得的路程,剪枝2,超过所规定的的花费,剪枝3,在规定花费内现在到这个点的路程大于以前到这个点的路程
                continue;
            mint[e[x][i].yy][e[x][i].cost+y]=z+e[x][i].len;
            v[e[x][i].yy]=1;
            dfs(e[x][i].yy,y+e[x][i].cost,z+e[x][i].len);
            v[e[x][i].yy]=0;
        }

    }
}
int main()
{
    int m,x;
    while(~scanf("%d%d%d",&tot,&n,&m))
    {
        for(int i=0; i<m; i++)
        {
            p r;
            scanf("%d%d%d%d",&x,&r.yy,&r.len,&r.cost);
            e[x].push_back(r);
        }
        sum=INF;
        memset(v,0,sizeof(v));
        memset(mint,INF,sizeof(mint));
        mint[1][0]=0; //mint数组一维代表位置,二位代表花费,用它记录路程
        dfs(1,0,0);//位置 花费  路程
        if(sum!=INF)
            printf("%d\n",sum);
        else
            printf("-1\n");
    }

    return 0;
}

广搜

#include<stdio.h>
#include<algorithm>
#include<string.h>
#include<queue>
using namespace std;
int n,tot;
int v[1010],head[1010];
struct pp
{
    int x;
    int y;
    int len;
    int cost;
    int next;
}e[10010];
struct p
{
    int pos; //当前所到达的位置
    int num; //路程
    int sum; //花费 
    friend bool operator<(p x1,p y1) //优先队列,路程从小到大排序
    {
        if(x1.sum==y1.sum)
            return x1.num>y1.num;
        return x1.sum>y1.sum;
    }
};
int bfs()
{
    p now,tmp;
    priority_queue<p>Q;
    now.pos=1;
    now.sum=0;
    now.num=0;
    v[now.pos]=1;
    Q.push(now);
    while(!Q.empty())
    {
        now=Q.top();
        Q.pop();
        if(now.pos==n)
        {
            printf("%d\n",now.sum);
            return 1;
        }
        for(int i=head[now.pos];i!=-1;i=e[i].next)
        {
            tmp.pos=e[i].y;
            if(now.num+e[i].cost<=tot)
            {
                tmp.num=now.num+e[i].cost;
                tmp.sum=now.sum+e[i].len;
                v[tmp.pos]=1;
                Q.push(tmp);
            }
        }
    }
    return 0;
}
int main()
{
    int m,a,b,c,d;
    scanf("%d%d%d",&tot,&n,&m);
    memset(v,0,sizeof(v));
    memset(head,-1,sizeof(head));
    for(int i=0;i<m;i++)
    {
        scanf("%d%d%d%d",&a,&b,&c,&d);
        e[i].x=a;
        e[i].y=b;
        e[i].len=c;
        e[i].cost=d;
        e[i].next=head[a]; //建立邻接表
        head[a]=i;
    }
    if(!bfs())
        printf("-1\n");
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值