POJ 1724 ROADS

Time Limit: 1000MS Memory Limit: 65536KB 64bit IO Format: %I64d & %I64u

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
1 2 2 3
7
2 4 3 3
3 4 2 4
4 6 2 1
1 3 4 1
3 5 2 0
5 4 3 2

Sample Output

11

题意:有N个城市,编号从1到N,城市之间有R条单向路,每条路连接两个城市,有长度和过路费。Bob只有K元钱,他想从1走到N,问最短需要走多长的路。如果到不了N,输出-1
分析:从城市1开始进行深搜,遍历整个图,找到能走的N的最短路径。不过如果不进行优化的话,肯定会超时!在优化时,需要考虑:(1)如果当前已经找到的最优路径为L,那么再继续搜索的过程中,总长度已经大于L的走法可以直接放弃,不用再走了。(2)用minL[k][m]表示:走到城市k,总过路费为m的情况下,最优路径的长度。若在后来的搜索中,再次走到k时,如过总路费恰好为m,但此时的路径长度已经超过了 minL[k][m],就不用再往下走了。

<pre name="code" class="cpp">#include<stdio.h>
#include<string.h>
#include<vector>
#include<iostream>
#include<algorithm>

using namespace std;

const int INF = 0x3f3f3f3f;

int K,N,R;
struct road
{
    int d,l,t;
};
vector< vector<road> >mapp(110);//邻接表,map[i]是指从点i有路连着的城市集合
int minlen = INF;//当前找到的最优路径的长度
int totallen;//正在走的路径的长度
int totalcost;//正在走的路径的花费
int vis[110];//标记城市是否已经走过
int minL[110][10100];//minL[i][j]表示从1到i点的,花费为j的最短路径的长度

void dfs(int s)//从s开始向n搜索
{
    if(s == N)  //走到N
    {
        minlen = min(minlen,totallen); //更新minlen
        return;
    }
    for(int i = 0;i < mapp[s].size();i++)
    {
        int d = mapp[s][i].d; //s有路连到d
        if(!vis[d])//若d没被访问过
        {
            int cost = totalcost + mapp[s][i].t; //从1走到d的费用
            if(cost > K) //如果所用费用已经超过K,则不走d
                continue;
            //如果从1走到d的总长已经超过之前求得的最优解或者大于之前到达d花费为cost的所走路径的长度,则不走d
            if((totallen + mapp[s][i].l >= minlen) || (totallen + mapp[s][i].l >= minL[d][cost]))
                continue;
            totallen += mapp[s][i].l;
            totalcost += mapp[s][i].t;
            minL[d][cost] = totallen;
            vis[d] = 1;
            dfs(d);
            vis[d] = 0;
            totallen -= mapp[s][i].l;
            totalcost -= mapp[s][i].t;
        }
    }
}

int main()
{
    scanf("%d %d %d",&K,&N,&R);
    for(int i = 0;i < R;i++)
    {
        int s;
        road r;
        scanf("%d %d %d %d",&s,&r.d,&r.l,&r.t);
        if(s != r.d)
            mapp[s].push_back(r);
    }
    for(int i = 0;i < 110;i++)
    {
        for(int j = 0;j < 10100;j++)
        {
            minL[i][j] = INF;
        }
    }
    memset(vis,0,sizeof(vis));
    totallen = 0;
    totalcost = 0;
    vis[1] = 1;
    minlen = INF;
    dfs(1);
    if(minlen < INF)
        printf("%d\n",minlen);
    else
        printf("-1\n");
    return 0;
}


 
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值