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

**题意是这样的,给你有向图,求从1到n的最短路,不过由于经费有限,我们的最短路要满足权的和不超过K.
下面是我的一份wrong answer代码**
错误就是错在Pair的排序上,将备注部分修改就可以了。
使用的算法是变形过的dijkstra算法,类似bfs了,每次将能遍历到的状态加入优先队列中,当取出的状态的节点是n时,说明n已经得到了最短路,为什么呢?这是由优先队列的性质决定的,
加入优先队列的状态呢都是满足了限制条件的状态,在众多状态中最小的状态一定是距离最短的,所以这个状态对应的顶点的最短路就已经确定了。

#include 
#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;

const int maxn = 105;
const int maxm = maxn * maxn;
const int INF= 0x3f3f3f3f;
struct Edge {
    int to,len,cost;
};
vector G[maxn];
int n,m,K;
struct Pair
{
    int n,d,c;
    bool operator <(const Pair &t)const{
        if(dreturn true; //应该return false
        if(d==t.d && creturn true; //应该 return false
        return false; //应该return true;
    }
};
priority_queue pq;

int main()
{
    //freopen("input.txt","r",stdin);
    while(scanf("%d" ,&K)!=EOF)
    {
        for(int i=0;iscanf("%d" ,&n);
        scanf("%d" ,&m);
        int from,to,len,cost;
        for(int i=1;i<=m;i++)
        {
            scanf("%d%d%d%d" ,&from,&to,&len,&cost);
            G[from].push_back((Edge){to,len,cost});
        }
        while(!pq.empty()) pq.pop();
        pq.push((Pair){1,0,0});
        int res=-1;
        while(!pq.empty())
        {
            Pair t= pq.top(); pq.pop();
            int v=t.n;
            if(v==n)
            {
                res=t.d; break;
            }
            for(int i=0;iif(t.c+e.cost <= K)
                {
                    pq.push((Pair){e.to,t.d+e.len,t.c+e.cost});
                }
            }
        }
        printf("%d\n",res);
    }
    return 0;
}

你可能感兴趣的:(曾经水过的题)