1003 Emergency (25 point(s))
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 Specification:
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 Specification:
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
大意:
给定一个无向图,给定起始点和终点,求出从起点到重点的所有最短距离的条数,并且输出最大的获取物资
这里要注意的一点是无向图,对应的需要把邻接矩阵对角对称
#include<iostream>
using namespace std;
int main(){
const int MAX = 510;
const int INF = 0x3f3f3f3f;
int N,M,C1,C2;
int map[MAX][MAX];
int d[MAX];
int w[MAX];
int weight[MAX];
int vis[MAX]{false};
int num[MAX]{0};
fill(d,d+MAX,INF);
fill(w,w+MAX,0);
fill(weight,weight+MAX,0);
fill(map[0],map[0]+MAX*MAX,INF);
cin>>N>>M>>C1>>C2;
// get weight
for(int i=0;i<N;i++){
int temp;
cin>>temp;
weight[i]=temp;
}
// for(int i=0;i<N;i++){
// for(int j=0;j<N;j++){
// cout<<map[i][j]<<" ";
// }
// cout<<endl;
// }
for(int i=0;i<M;i++){
int c1;
int c2;
int lenth;
cin>>c1>>c2>>lenth;
map[c1][c2] = lenth;
map[c2][c1] = map[c1][c2];
}
// for(int i=0;i<N;i++){
// for(int j=0;j<N;j++){
// if(map[i][j]){
// cout<<i<<" "<<j<<" "<<map[i][j]<<endl;
// }
// }
// }
d[C1] =0;
w[C1] = weight[C1];
num[C1]=1;
for(int i=0;i<N;i++){
int min =INF;
int u_index= -1;
for(int j=0;j<N;j++){
if(!vis[j]&&min>d[j]){
min = d[j];
u_index = j;
}
}
if(u_index==-1){
break;
}
vis[u_index] = true;
for(int j=0;j<N;j++){
if(map[u_index][j]!=INF&&!vis[j]){
if(d[u_index]+map[u_index][j]<d[j]){
d[j] = d[u_index]+map[u_index][j];
w[j] = w[u_index]+weight[j];
num[j]=num[u_index];
}else if(d[u_index]+map[u_index][j]==d[j]){
if(w[u_index]+weight[j]>w[j]){
w[j] = w[u_index]+weight[j];
}
num[j]+=num[u_index];
}
}
}
}
cout<<num[C2]<<" "<<w[C2]<<endl;
return 0;
}
最終錯誤原因:
1.仔細審題,題目的要求是最后输出最短路径的条数,而不是最短路径的距离
2.这是一个无向图,所以必须要在邻接矩阵中对应的对称化map[c2][c1] = map[c1][c2];
spfa算法队列实现:
#include<iostream>
#include<queue>
#include<vector>
#include<cstring>
#include<set>
using namespace std;
const int MAXN = 10000;
int N,M;
const int INF = 0xf3f3f3f;
int weight[MAXN];
struct node{
int to,len,rec;
node(){}
node(int to,int len,int rec):to(to),len(len),rec(rec){};
};
int innum[MAXN];
bool inque[MAXN];
int dist[MAXN];
set<int> pre[MAXN];
vector<node> map_[MAXN];
void add_edge(int a, int b, int cost){
map_[a].push_back(node(b,cost,map_[b].size()));
map_[b].push_back(node(a,cost,map_[a].size()-1));
}
vector<int> temp_path;
int cal(vector<int> path){
int ans = 0;
for(int i = path.size()-1; i>=0;i--){
ans += weight[path[i]];
}
return ans;
}
int max_ = -1;
int pathNum = 0;
void dfs_out(int start,int end){
if(start == end){
temp_path.push_back(start);
int ans = cal(temp_path);
pathNum++;
if(ans > max_){
max_ = ans;
}
temp_path.pop_back();
return;
}
temp_path.push_back(end);
set<int>::iterator it = pre[end].begin();
for(;it!=pre[end].end();it++){
// printf("%d\n",*it);
dfs_out(start,*it);
}
temp_path.pop_back();
}
bool spfa(int start){
fill(dist,dist+N,INF);
memset(inque,0,sizeof(inque));
memset(innum,0,sizeof(innum));
dist[start] = 0;
inque[start] = 1;
innum[start] = 1;
queue<node> que;
que.push(node(start,0,0));
while(!que.empty()){
node temp = que.front(); que.pop();
inque[temp.to] = false;
for(int i=0;i<map_[temp.to].size();i++){
node nnode = map_[temp.to][i];
if(dist[nnode.to]> dist[temp.to] + nnode.len){
dist[nnode.to] = dist[temp.to] + nnode.len;
pre[nnode.to].clear();
pre[nnode.to].insert(temp.to);
if(inque[nnode.to]) continue;
inque[nnode.to] = 1;
que.push(nnode);
if(innum[nnode.to]>= N-1) return false;
innum[nnode.to]++;
}else if(dist[nnode.to]== dist[temp.to] + nnode.len){
pre[nnode.to].insert(temp.to);
}
}
}
return true;
}
int main(){
int start,end;
scanf("%d%d%d%d",&N,&M,&start,&end);
for(int i=0;i<N;i++)
scanf("%d",&weight[i]);
for(int i=0;i<M;i++){
int a,b,cost;
scanf("%d%d%d",&a,&b,&cost);
add_edge(a,b,cost);
}
spfa(start);
dfs_out(start,end);
printf("%d %d\n",pathNum,max_);
return 0;
}
这里对spfa 和 djikstra 算法进行比较:
djikstra 算法 的优先队列实现方式:
https://blog.csdn.net/Willen_/article/details/100358199
可以看出两者都是有很多的相同点:
1.dijkstra 使用优先队列 来对最小的未访问的边进行优化,spfa可以使用队列,也可以使用有限队列
2.两者的入栈都是在松弛操作发生之后才开始入队,十分相似
3.两者都有一个公共的记录单源数组,进行全局优化
不同点:
1.spfa 有一个入队数组,来确保某个点是否进入队列,而 dijkstra的优先队列实现没有
2.spfa 有一个入队次数数组,来确保某个点是否进入队列次数超过某个阈值一般是 顶点个数N -1 来判断是否有可达的负环,而 dijkstra的优先队列实现没有
3.spfa 和 (Bellman-Ford) 在统计最短路径条数的做法上会有差异,因为这个算法可能会多次访问曾经访问过的顶点,所以在记录前驱数组的做法中要用set<int> pre[MAXN] 数组来保存。而dijkstra的优先队列实现 用 vector<int> 就行,因为应用这个算法的时候,我们已经确保了在没有负环的情况下使用。