URAL 1031. Railway Tickets(spfa)

题目链接

不知为何会在dp里呢。。。INF取小了,2Y。

 1 #include <cstring>

 2 #include <cstdio>

 3 #include <string>

 4 #include <iostream>

 5 #include <algorithm>

 6 #include <vector>

 7 #include <queue>

 8 #include <cmath>

 9 using namespace std;

10 #define INF 1e9

11 int l1,l2,l3,c1,c2,c3;

12 int p[20001];

13 int dis[20001];

14 int in[20001];

15 int judge(int x,int y)

16 {

17     int d = p[x] - p[y];

18     if(d < 0)

19     d = -d;

20     if(d <= l1)

21     return c1;

22     if(d <= l2)

23     return c2;

24     if(d <= l3)

25     return c3;

26     return INF;

27 }

28 int main()

29 {

30     int i,n,sv,ev,w,u;

31     queue<int>que;

32     scanf("%d%d%d%d%d%d",&l1,&l2,&l3,&c1,&c2,&c3);

33     scanf("%d",&n);

34     scanf("%d%d",&sv,&ev);

35     for(i = 2;i <= n;i ++)

36     scanf("%d",&p[i]);

37     for(i = 1;i <= n;i ++)

38     {

39         dis[i] = INF;

40         in[i] = 0;

41     }

42     dis[sv] = 0;

43     in[sv] = 1;

44     que.push(sv);

45     while(!que.empty())

46     {

47         u = que.front();

48         que.pop();

49         in[u] = 0;

50         for(i = 1;i <= n;i ++)

51         {

52             w = judge(i,u);

53             if(dis[i] > dis[u] + w)

54             {

55                 dis[i] = dis[u] + w;

56                 if(!in[i])

57                 {

58                     in[i] = 1;

59                     que.push(i);

60                 }

61             }

62         }

63     }

64     printf("%d\n",dis[ev]);

65     return 0;

66 }

 

你可能感兴趣的:(SPFA)