Bellman Ford算法在实际应用时切记不要将INF设置为0x7fffffff,该值加上一个任何一个正整数就变成负数, 从而会产生段错误。

以下题为例:

1003. Emergency (25)

As an emergency rescue team leader of a city, you are given a special map of your country. The map shows several scattered cities connected by some roads. Amount of rescue teams in each city and the length of each road between any pair of cities are marked on the map. When there is an emergency call to you from some other city, your job is to lead your men to the place as quickly as possible, and at the mean time, call up as many hands on the way as possible.

 

Input

Each input file contains one test case. For each test case, the first line contains 4 positive integers: N (<= 500) - the number of cities (and the cities are numbered from 0 to N-1), M - the number of roads, C1 and C2 - the cities that you are currently in and that you must save, respectively. The next line contains N integers, where the i-th integer is the number of rescue teams in the i-th city. Then M lines follow, each describes a road with three integers c1, c2 and L, which are the pair of cities connected by a road and the length of that road, respectively. It is guaranteed that there exists at least one path from C1 to C2.

Output

For each test case, print in one line two numbers: the number of different shortest paths between C1 and C2, and the maximum amount of rescue teams you can possibly gather.
All the numbers in a line must be separated by exactly one space, and there is no extra space allowed at the end of a line.

Sample Input

5 6 0 2
1 2 1 5 3
0 1 1
0 2 2
0 3 1
1 2 1
2 4 1
3 4 1

Sample Output

2 4
 1 #include <iostream>
 2 #include <vector>
 3 #include <set>
 4 #include <algorithm>
 5 using namespace std;
 6 const int maxn = 510, inf = 0x3fffffff;//一定要注意这里,不能设成最大的int值
 7 struct Arc{
 8     int v, len;
 9 };
10 vector<Arc> arc[maxn];
11 int N, M, C1, C2, W[maxn];
12 int dis[maxn], pathNum = 0, maxValue = 0;
13 vector<int> tempPath, optPath;
14 set<int> pre[maxn];
15 
16 bool Bellman(int s){
17     fill(dis, dis+maxn, inf);
18     dis[s] = 0;
19     for(int i = 0; i < N-1; ++ i){
20         //对每条边进行松弛 
21         for(int u = 0; u < N; ++ u){
22             for(int j = 0; j < arc[u].size(); ++ j){
23                 int v = arc[u][j].v;
24                 if(dis[u] + arc[u][j].len < dis[v]){
25                     dis[v] = dis[u] + arc[u][j].len;
26                     pre[v].clear();
27                     pre[v].insert(u);
28                 }else if(dis[u] + arc[u][j].len == dis[v]){
29                     pre[v].insert(u);
30                 }
31             }
32         }
33     }
34     //对于该题已知没有负环,故可以省略此步 
35 //    for(int u = 0; u < N; ++ u){
36 //            for(int j = 0; j < arc[u].size(); ++ j){
37 //                int v = arc[u][j].v;
38 //                if(dis[u] + arc[u][j].len < dis[v]){
39 //                    //还能松弛,则有从源点可达的负环 
40 //                    return false;
41 //                }
42 //            }
43 //        }
44 //    } 
45     return true;
46 }
47 
48 int calValue(vector<int> &v){
49     int ret = 0;
50     for(int i = 0; i < v.size(); ++ i){
51         ret += W[v[i]];
52     }
53     return ret;
54 }
55 
56 void DFS(int v){
57     tempPath.push_back(v);
58     if(v == C1){
59         pathNum ++;
60         int thisValue = calValue(tempPath);
61         if(thisValue > maxValue){
62             maxValue = thisValue;
63             optPath = tempPath;
64         } 
65         tempPath.pop_back(); 
66         return;
67     }
68     for(set<int>::iterator it = pre[v].begin(); it != pre[v].end(); it ++){
69         DFS(*it);
70     }
71     tempPath.pop_back();
72 }
73 
74 //Bellman + DFS 
75 int main()
76 {
77     scanf("%d%d%d%d", &N, &M, &C1, &C2);
78     for(int i = 0; i < N; ++ i)    scanf("%d", &W[i]);
79     for(int i = 0; i < M; ++ i){
80         int v1, v2;
81         Arc t;
82         scanf("%d%d%d", &v1, &v2, &t.len);
83         t.v = v2; arc[v1].push_back(t);
84         t.v = v1; arc[v2].push_back(t);
85     }
86     Bellman(C1);
87     DFS(C2);
88     printf("%d %d\n", pathNum, maxValue);
89     return 0;
90 }

 

Posted on 2017-03-01 17:45  小小旅行商  阅读(222)  评论(0编辑  收藏  举报