http://poj.org/problem?id=1724

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

 

 

在求最短路径的基础上,加了限制,初始有一定数目硬币,走每条边要花费硬币,那么我们将起点到某点的最短路设计为d(v,money),表示从起点到达点v并且此时剩余硬币数为money对应的最短路径长度。

改一改刘汝佳模板就好了。

#include<iostream>
#include<algorithm>
#include<queue>
#include<vector>
#include<cstring>
#include<cstdio>
using namespace std;

struct Edge{
	int from,to,dist,cost;
};
struct HeapNode{
	int u,d,mon;
	bool operator < (const HeapNode& x)const{
		return d>x.d;
	}
};
int n,m,s,money;
vector<Edge> edges;
vector<int> G[300+10];
bool vis[200+10][10000+10];
int d[200+10][10000+10];

void init()
{
	edges.clear();
	for(int i=1;i<=n;i++)G[i].clear();
}


void AddEdge(int f,int t,int d,int c)
{ 
	edges.push_back((Edge){f,t,d,c});
	G[f].push_back(edges.size()-1);
} 

void dijkstra(int s)
{
	priority_queue<HeapNode> Q;
	for(int i=1;i<=n;i++)for(int j=0;j<=money;j++)d[i][j]=(1<<30);
	d[s][money]=0;
	memset(vis,0,sizeof(vis));
	Q.push((HeapNode){s,0,money});
	while(!Q.empty())
	{
		HeapNode x=Q.top();
		Q.pop();
		int u=x.u;
		if(vis[u][x.mon])continue;
		vis[u][x.mon]=1;
		for(int i=0;i<G[u].size();i++)
		{
			Edge& e=edges[G[u][i]];
			int v=e.to;
			if(x.mon>=e.cost&&d[v][x.mon-e.cost]>d[u][x.mon]+e.dist)
			{
				d[v][x.mon-e.cost]=d[u][x.mon]+e.dist;
				Q.push((HeapNode){v,d[v][x.mon-e.cost],x.mon-e.cost});
			}
		}
	}
}

int main()
{
//	freopen("input.in","r",stdin);
	while(cin>>money)
	{
		init();
		cin>>n>>m;
		int a,b,c,d1;
		while(m--)
		{
			cin>>a>>b>>c>>d1;
			AddEdge(a,b,c,d1);
		//	AddEdge(b,a,c,d1);
		}
		dijkstra(1);
		int minn=(1<<30);
		for(int i=0;i<=money;i++)if(d[n][i]<minn)minn=d[n][i];
		if(minn==(1<<30))puts("-1");
		else printf("%d\n",minn);
	}
	return 0;
}