Shortest path problem

POJ-基本最短路问题

POJ 1062 昂贵的聘礼

中文题,dijkstra基本运用

POJ 1062
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
//By Brickgao
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <vector>
using namespace std;
int M, N, lev[110];
int dis[110], map[110][110];
bool vis[110];
void dijstra(int m, int n)
{
int i, k;
int min;
memset(vis, false, sizeof(vis));
memset(dis, 999999999, sizeof(dis));
vis[0] = true;
for(i = 0; i <= N; i++)
if(lev[i] >= m && lev[i] <= n)
dis[i] = map[0][i];
k = 0;
while(1)
{
min = 999999999;
for(i = 0; i <= N; i++)
if(lev[i] >= m && lev[i] <= n && !vis[i] && min > dis[i])
{
min = dis[i];
k = i;
}
vis[k] = true;
if(k == 1)
return;
for(i = 0; i <= N; i++)
if(lev[i] >= m && lev[i] <= n && !vis[i] && dis[i] > dis[k] + map[k][i])
{
dis[i] = dis[k] + map[k][i];
}
}
}
int main()
{
int i, j, k, t, p;
memset(map, 127, sizeof(map));
cin >> M >> N;
int ans = 999999999;
for(i = 1; i <= N; i++)
{
scanf("%d%d%d", &map[0][i], &lev[i], &k);
for(j = 1; j <= k; j++)
{
scanf("%d%d", &t, &p);
map[t][i] = p;
}
}
for(i = lev[1] - M; i <= lev[1]; i++)
{
dijstra(i, i + M);
if(ans > dis[1])
ans = dis[1];
}
cout << ans << endl;
return 0;
}