poj 1724 ROADS

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



大致题意:

给定一个图,图中每条路都有 路长Length 和 过路费Toll 两个参数,一条路连接两个城市,任意两个城市之间有且仅有一条路。

现在只有 K 块钱,要求从起点City1出发,到达终点CityN的最短路,也就是说在 K 花费内的最短路。

 

 1 #include <iostream>
 2 #include <stdio.h>
 3 #include <string.h>
 4 #define MM 9999999
 5 using namespace std;
 6 int K,N,R;
 7 struct node
 8 {
 9     int s,d,l,t,next;
10 }dd[10005];
11 bool flag[111];
12 int head[111];
13 int tot;
14 int Minlen;
15 void dfs(int pos,int len,int cost)
16 {
17     if(pos==N)
18     {
19         if(cost<=K && len<Minlen)
20         {
21             Minlen=len;
22         }
23         return;
24     }
25     if(len>=Minlen)
26     {
27         return;
28     }
29     if(cost>K)
30     {
31         return;
32     }
33     int i;
34     for(i=head[pos];i!=-1;i=dd[i].next)
35     {
36         if(!flag[dd[i].d])
37         {
38             int sd=dd[i].d;
39             int sl=dd[i].l;
40             int st=dd[i].t;
41             flag[sd]=true;
42             dfs(sd,len+sl,cost+st);
43             flag[sd]=false;
44         }
45     }
46 }
47 int main()
48 {
49     while(~scanf("%d%d%d",&K,&N,&R))
50     {
51         int i;
52         tot=0;
53         memset(head,-1,sizeof(head));
54         for(i=0;i<R;i++)
55         {
56             scanf("%d%d%d%d",&dd[i].s,&dd[i].d,&dd[i].l,&dd[i].t);
57             dd[i].next=head[dd[i].s];
58             head[dd[i].s]=tot++;
59         }
60         memset(flag,false,sizeof(flag));
61         flag[1]=true;
62         Minlen=MM;
63         dfs(1,0,0);
64         if(Minlen==MM)
65         {
66             printf("-1\n");
67         }
68         else
69         {
70             printf("%d\n",Minlen);
71         }
72     }
73     return 0;
74 }
View Code

方法是使用了DFS+剪枝;

这题和POJ3411差不多,学习使用邻接矩阵方法来处理存在相同起点和终点的情况。

确实学习了,这种方法省了很多时间。

posted @ 2013-06-11 21:18  欧阳生朵  阅读(230)  评论(0编辑  收藏  举报