P - ROADS

P - ROADS

题目:
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<bits/stdc++.h>
#include<iostream>
#include<cstdio>
#include<vector>
#include<cstring>
using namespace std;
int K,N,R,S;
struct road
{
    int d,l,t;
};
int minl[110][10100];// //minL[i][j]表示从1到i点的,花销为j的最短路的长度
int vis[110];//用于城市的判重
int totallen = 0;
int totalcost = 0;
int minlen = (1 << 21);
vector<vector<road> > p(110);
void dfs(int s)
{
    if(s == N)
    {
        minlen = min(minlen,totallen);//每次遍历到终点n时选取最小长度
        return;
    }
    for(int i = 0;i < p[s].size();i++)
    {
        int d = p[s][i].d;//连接到城市d
        if(vis[d] == 1) continue;//遇到旧点直接跳过
        int cost = totalcost + p[s][i].t;
        if(cost > K) continue;//当前费用超过拥有的前,无法走下去
        if(minlen <= totallen + p[s][i].l
           || minl[d][cost] <= totallen + p[s][i].l) continue;
        vis[d] = 1;//将走过的这个城市标记为旧点
        totalcost += p[s][i].t;//走过这个城市将费用累加
        totallen += p[s][i].l;//将长度累加
        minl[d][cost] = totallen;
        dfs(d);
        vis[d] = 0;
        totalcost -= p[s][i].t;
        totallen -= p[s][i].l;
    }
}

int main()
{
    scanf("%d%d%d",&K,&N,&R);
    for(int i = 0;i < R;i++)
    {
        road road1;
        cin >> S >> road1.d >> road1.l >> road1.t;
        if(S == road1.d) continue;
        p[S].push_back(road1);
    }
    for(int i = 0;i < 110;i++)
    {
        for(int j = 0;j < 10100;j++) minl[i][j] = (1 << 21);
    }
    memset(vis,0,sizeof(vis));
    vis[1] = 1;//城市1已经经过
    dfs(1);//从城市1开始dfs
    if(minlen < (1 << 21)) cout << minlen << endl;
    else cout << "-1" << endl;
    return 0;
}

从城市 1开始深度优先遍历整个图,找到所有能到达 N 的走法,选一个最优的。
最优性剪枝:

  1. 如果当前已经找到的最优路径长度为L ,那么在继续搜索的过程中,总长度已经大于等于L的走法,就可以直接放弃,不用走到底了保存中间计算结果用于最优性剪枝:
  2. 用midL[k][m] 表示:走到城市k时总过路费为m的条件下,最优路径的长度。若在后续的搜索中,再次走到k时,如果总路费恰好为m,且此时的路径长度已经超过 midL[k][m],则不必再走下去了。

猜你喜欢

转载自blog.csdn.net/qq_41998938/article/details/84347342