poj 1062 昂贵的聘礼

Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 15126 Accepted: 3992

      最短路问题,不过因为存在着等级的差异所以需要枚举一下。本题的思路就是对冒险者的等级进行枚举,也就是说冒险者只能和在他等级以上的人进行交易。这样枚举的好处是能够把所有的情况都考虑进去。有一点需要注意:酋长的等级不一定是最高的

代码:

 

  
    
1 #include < stdio.h >
2 #include < string .h >
3   struct no
4 {
5 int p,l;
6 }B[ 1000 ];
7   int map[ 105 ][ 105 ];
8 int m,n,dist[ 105 ],u;
9 void dijkstra( int v0, int l)
10 {
11 int i,j,visit[ 105 ],min;
12 for (i = 1 ;i <= n;i ++ )
13 {
14 if (map[v0][i] !=- 1 && B[i].l - l >= 0 && B[i].l - l <= m)
15 dist[i] = map[v0][i];
16 else
17 dist[i] = 0xfffffff ;
18 visit[i] = 0 ;
19 }
20 visit[v0] = 1 ;
21 for (i = 1 ;i < n;i ++ )
22 {
23 min = 0xfffffff ;u = i;
24 for (j = 1 ;j <= n;j ++ )
25 {
26 if ( ! visit[j] && dist[j] < min)
27 {
28 u = j;
29 min = dist[j];
30 }
31 }
32 visit[u] = 1 ;
33 for (j = 1 ;j <= n;j ++ )
34 {
35 if ( ! visit[j] && (B[j].l - l) >= 0 && (B[j].l - l) <= m && map[u][j] !=- 1 && dist[j] > dist[u] + map[u][j])
36 dist[j] = dist[u] + map[u][j];
37 }
38 }
39 }
40 int main()
41 {
42 int p,l,x,j,i,b,t,min;
43 scanf( " %d%d " , & m, & n);
44 memset(map, - 1 , sizeof (map));
45 for (i = 1 ;i <= n;i ++ )
46 {
47 scanf( " %d%d%d " , & p, & l, & x);
48 for (j = 1 ;j <= x;j ++ )
49 {
50 scanf( " %d%d " , & b, & t);
51 map[i][b] = t;
52 }
53 B[i].p = p;
54 B[i].l = l;
55 }
56 min = B[ 1 ].p;
57 for (i = B[ 1 ].l - m;i <= B[ 1 ].l;i ++ )
58 {
59 dijkstra( 1 ,i);
60 for (j = 1 ;j <= n;j ++ )
61 {
62 if (dist[j] + B[j].p < min)
63 min = dist[j] + B[j].p;
64 }
65 }
66 printf( " %d\n " ,min);
67 return 0 ;
68 }
69
70 /*
71 1 5
72 10000 3 2
73 2 5000
74 3 5000
75 3000 4 1
76 4 400
77 3000 2 1
78 4 500
79 1000 3 1
80 5 100
81 100 2 0
82
83 1 5
84 10000 3 2
85 2 5000
86 3 5000
87 3000 2 1
88 4 500
89 3000 4 1
90 4 400
91 1000 3 1
92 5 100
93 100 2 0
94
95 */
96

 

你可能感兴趣的:(poj)