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
Dijstra图算法的应用,磕磕绊绊的,不过算是自己写完的。
要素
- 定义INF,vis[] ,G[][],dis[]
- 进入算法,首先处理头结点,然后循环n次{先找到dis最小的节点u,然后以u为中继更新其他未访问节点}
- 最短路径的条数:进行累加计数;能找到的队伍数,也是累加计数
#include<cstdio> #include<iostream> #include<algorithm> using namespace std; const int maxn = 510; const int INF = 1E9; int G[maxn][maxn] = { 0 }; int TeamNum[maxn] = { 0 }; int TeamCnt[maxn] = { 0 }; int dis[maxn]; int pathNum[maxn]; bool vis[maxn] = { false }; int N, M, C1, C2; void Dijstra(int u) { dis[u] = 0; TeamCnt[u] = TeamNum[u]; for (int i = 0; i < N; i++) { int MIN = INF, u = -1; for (int j = 0; j < N; j++) { if (vis[j] == false && dis[j] < MIN) { //找到一个未访问的距离最小点作为起点 MIN = dis[j]; u = j; } } if (u == -1) return; //if u not update, indicate that not route for (int v = 0; v < N; v++) {// update path vis[u] = true; if (vis[v] == false && G[u][v] != 0) { if (dis[u] + G[u][v] < dis[v]) { dis[v] = dis[u] + G[u][v]; pathNum[v] = pathNum[u]; TeamCnt[v] = TeamCnt[u] + TeamNum[v]; } else if (dis[u] + G[u][v] == dis[v]) { pathNum[v] += pathNum[u]; if (TeamCnt[u] + TeamNum[v] > TeamCnt[v]) TeamCnt[v] = TeamCnt[u] + TeamNum[v]; } } } } } int main() { cin >> N >> M >> C1 >> C2; fill(dis, dis + maxn, INF); fill(pathNum, pathNum + maxn, 1); for (int i = 0; i < N; i++) cin >> TeamNum[i]; for (int i = 0; i < M; i++) { int c1, c2, L; cin >> c1 >> c2 >> L; G[c1][c2] = L; G[c2][c1] = L; } Dijstra(C1); cout << pathNum[C2] << " " << TeamCnt[C2] << endl; }
Bellman-Ford算法
与Dijstra算法有很大不同,N-1轮,遍历所有边(会重复访问节点)
统计最短路径条数时,由于重复访问节点,要设置前驱set<int> pre[maxn],当相同时,要重新统计前驱和。
num[]数组初始化与Dij相同
Adj邻接表存储,Adj[u][j],j不代表节点地址,只代表Adj中的地址,通过Adj[u][j].v来获取地址
#include<cstdio> #include<iostream> #include<algorithm> #include<vector> #include<set> using namespace std; struct node { int v, weight; //代表所连接的点和权值 node(int _v, int _weight) : v(_v), weight(_weight) {} }; const int maxn = 510; const int INF = 1e9; int dis[maxn]; int w[maxn] = { 0 }; int wCnt[maxn] = { 0 }; int num[maxn] = { 0 }; int N, M, C1, C2; vector<node> Adj[maxn]; set<int> pre[maxn]; bool BellmanFord(int source) { fill(dis, dis + maxn, INF); wCnt[source] = w[source]; //same dis[source] = 0; //same num[source] = 1; for (int i = 0; i < N - 1; i++) { for (int u = 0; u < M; u++) { for (int j = 0; j < Adj[u].size(); j++)// v is index of type node, not the real node { int v = Adj[u][j].v; int weight = Adj[u][j].weight; if (dis[u] + weight < dis[v]) { dis[v] = dis[u] + weight; wCnt[v] = wCnt[u] + w[v]; num[v] = num[u]; pre[v].clear(); pre[v].insert(u); } else if (dis[u] + weight == dis[v]) { //num[v] += num[u]; 由于重复计数,不能这样使用 if (wCnt[v] < wCnt[u] + w[v]) { wCnt[v] = wCnt[u] + w[v]; } num[v] = 0; pre[v].insert(u); set<int>::iterator it; for (it = pre[v].begin(); it != pre[v].end(); it++) { num[v] += num[*it]; } } } } } for (int i = 0; i < N - 1; i++) { for (int u = 0; u < M; u++) { for (int j = 0; j < Adj[u].size(); j++) { if (dis[u] + Adj[u][j].weight < dis[Adj[u][j].v]) return false; } } } return true; } int main() { cin >> N >> M >> C1 >> C2; for (int i = 0; i < N; i++) cin >> w[i]; for (int i = 0; i < M; i++) { int c1, c2, L; cin >> c1 >> c2 >> L; Adj[c1].push_back(node(c2, L)); Adj[c2].push_back(node(c1, L)); } if (BellmanFord(C1)) { cout << num[C2] << " " << wCnt[C2]; } // output the number of shortest path return 0; }