Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10202 Accepted: 3786

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`

dfs就是从1号开始进行逐层递归遍历所有情况，一旦金币数量不能满足花费，就退出递归，直到找到最短的路径。

```//332K	94MS
#include<stdio.h>
#include<string.h>
#define M 107
#define inf 0x3f3f3f
int n,m,k;
struct E
{
int u,v,l,c,next;
} edge[M*M];
void addedge(int u,int v,int l,int c)
{
edge[nodes].u=u;
edge[nodes].v=v;
edge[nodes].l=l;
edge[nodes].c=c;
}
void dfs(int city,int nowlen,int nowmoney)
{
if(nowlen>minlen)return;
if(city==n&&nowlen<minlen&&nowmoney>=0)
{
minlen=nowlen;
return;
}
{
int v=edge[i].v;
int l=edge[i].l;
int c=edge[i].c;
if(!vis[v]&&nowmoney>=c)
{
vis[v]=1;
dfs(v,nowlen+l,nowmoney-c);
vis[v]=0;
}
}
return;
}
int main()
{
memset(vis,0,sizeof(vis));
nodes=0,minlen=inf;
scanf("%d%d%d",&k,&n,&m);
int u,v,l,c;
while(m--)
{
scanf("%d%d%d%d",&u,&v,&l,&c);
}
dfs(1,0,k);
if(minlen==inf)printf("-1\n");
else
printf("%d\n",minlen);
}
```
```//1028K	32MS
#include<stdio.h>
#include<string.h>
#include<queue>
#define M 1007
using namespace std;
int k,n,m;
struct Q
{
int pos,len,money;
bool operator<(const Q t)const
{
return t.len<len;
}
} ;
struct node
{
int u,v,next,l,c;
}edge[M*M];
void addedge(int u,int v,int l,int c)
{
edge[nodes].u=u;
edge[nodes].v=v;
edge[nodes].l=l;
edge[nodes].c=c;
}
int bfs(int u)
{
priority_queue<Q>q;
Q now,next;
now.pos=1;
now.len=0;
now.money=0;
q.push(now);
while(!q.empty())
{
now=q.top();
q.pop();
int pos=now.pos,len=now.len,money=now.money;
if(pos==n&&money<=k)return len;
{
int v=edge[i].v;
int l=edge[i].l;
int c=edge[i].c;
if(money+c<=k)
{
next.pos=v;
next.len=len+l;
next.money=money+c;
q.push(next);
}
}
}
return -1;
}
int main()
{
scanf("%d%d%d",&k,&n,&m);
nodes=0;
int u,v,l,c;
for(int i=0; i<m; i++)
{
scanf("%d%d%d%d",&u,&v,&l,&c);
}
int ans=bfs(1);
printf("%d\n",ans);
}
```

POJ 1724 ROADS Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 12766   Accepted: 4722 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

## 题单二：图论500

http://wenku.baidu.com/link?url=gETLFsWcgddEDRZ334EJOS7qCTab94qw5cor8Es0LINVaGMSgc9nIV-utRIDh--2UwRLvsvJ5tXFjbdpzbjygEdpGehim1i5BfzYgYWxJmu ==========  以下是最小生成树+并查集=========================[HDU]1213         How Many Tables        基础并查集★1272         小