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.
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 1Sample Output
2 4
#include<cstdio> #include<algorithm> #include<string.h> #include<queue> #include<map> using namespace std; int city[505][505]; int teamnum[505]; int dijkstra[505]; bool isVisited[505]; int shortest,sumroad,sumteam; const int INF = 10000000; void init(){ for(int i = 0; i < 505;i ++) isVisited[i] = false; } void Dijkstra(int n,int begin,int end){ for(int i = 0; i < n;i ++) dijkstra[i] = city[begin][i]; isVisited[begin] = true; for(int i = 0; i < n - 1;i ++){ int min = INF; int flag = INF; for(int j = 0;j < n;j ++) if(isVisited[j] == false && dijkstra[j] < min){ min = dijkstra[j]; flag = j; } isVisited[flag] = true; if(flag == end) break; for(int k = 0; k < n; k ++) if(isVisited[k] == false && dijkstra[flag] + city[flag][k] < dijkstra[k]) dijkstra[k] = dijkstra[flag] + city[flag][k]; } } void DFS(int begin,int end,int n,int curDis,int curTeam){ isVisited[begin] = true; if(begin == end){ if(curDis == shortest){ sumroad ++; if(curTeam > sumteam) sumteam = curTeam; } return; } for(int i = 0;i < n; i ++){ if(isVisited[i] == false && city[begin][i] != INF) if(curDis + city[begin][i] > shortest) continue; else{ DFS(i,end,n,curDis + city[begin][i],curTeam + teamnum[i]); isVisited[i] = false; //关键 } } return; } int main(void){ int N,M,C1,C2; scanf("%d %d %d %d",&N,&M,&C1,&C2); for(int i = 0;i < N;i ++) scanf("%d",&teamnum[i]); for(int p = 0;p < N;p ++) for(int q = 0;q < N;q++) if(p == q) city[p][q] = 0; else city[p][q] = INF; for(int i = 0;i < N;i ++) dijkstra[i] = INF; for(int i = 0;i < N;i ++) isVisited[i] = false; while(M){ int a,b,len; scanf("%d %d %d",&a,&b,&len); city[a][b] = len; city[b][a] = len; M--; } Dijkstra(N,C1,C2); shortest = dijkstra[C2]; init(); DFS(C1,C2,N,0,teamnum[C1]); printf("%d %d",sumroad,sumteam); return 0; }
本文介绍了一种用于紧急救援场景下的最短路径寻找及资源最大化的算法实现。该算法通过Dijkstra算法找到两个城市间的最短路径,并利用深度优先搜索(DFS)算法确保途中收集尽可能多的救援队伍。
1万+

被折叠的 条评论
为什么被折叠?



