PAT 1003. Emergency (25)

题目地址: http://www.patest.cn/contests/pat-a-practise/1003

最短路

 1 #include<iostream>

 2 #include<cstring>

 3 #include<cstdio>

 4 

 5 using namespace std;

 6 

 7 const int INF = 1000000000;

 8 const int MAX_NODES = 500 + 10;

 9 int edge[MAX_NODES][MAX_NODES];

10 int d[MAX_NODES];

11 int cnt[MAX_NODES];

12 int c_max[MAX_NODES];

13 int c[MAX_NODES];

14 bool flg[MAX_NODES];

15 int n, m, s, t;

16 

17 void shortest_path() {

18         memset(flg, false, sizeof(flg));

19         for (int i = 0; i < n; ++i) d[i] = i == s ? 0 : INF;

20         cnt[s] = 1;

21         c_max[s] = c[s];

22         for (int i = 0; i < n; ++i) {

23                 int x, mx = INF;

24                 for (int j = 0; j < n; ++j) {

25                         if (!flg[j] && d[j] <= mx) {

26                                 mx = d[x = j];

27                         }

28                 }

29                 flg[x] = true;

30                 for (int j = 0; j < n; ++j) {

31                         if (edge[x][j]) {

32                                 if (d[j] > d[x] + edge[x][j]) {

33                                         d[j] = d[x] + edge[x][j];

34                                         cnt[j] = cnt[x];

35                                         c_max[j] = c[j] + c_max[x];

36                                 } else if (d[j] == d[x] + edge[x][j]) {

37                                         cnt[j] += cnt[x];

38                                         c_max[j] = max(c[j] + c_max[x], c_max[j]);

39                                 }

40                         }

41                 }

42         }

43 }

44 int main() {

45         while(cin >> n >> m >> s >> t) {

46                 memset(edge, 0, sizeof(edge));

47                 for (int i = 0; i < n; ++i) cin >> c[i];

48                 for (int i = 0; i < m; ++i) {

49                         int c1, c2, l;

50                         cin >> c1 >> c2 >> l;

51                         edge[c1][c2] = l;

52                         edge[c2][c1] = l;

53                 }

54                 shortest_path();

55                 cout << cnt[t] << " " << c_max[t] << endl;

56         }

57         return 0;

58 }

 

你可能感兴趣的:(merge)