题目
大意:有N个城市,M 条道路,从C1城市出发,到C2城市结束。
接着给你这N个城市每个城市的救援队的数量。
求出C1-->C2的最短路径的数量 , 同时最短路径中能够集结的最大救援队数量。
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
分析
参考博客:https://www.liuchuo.net/archives/2359
比较简答的思考方式是,在更新最短路径的时候,同时更新最短路径的数量和救援人员的数量。
dis[i]表示从出发点到i结点最短路径的路径长度,
num[i]表示从出发点到i结点最短路径的条数,
w[i]表示从出发点到i点救援队的数目之和
weight[i]表示第i点的救援队数目
更新的核心代码,是在迪杰斯特拉算法的松弛阶段
for(int v = 0 ; v < n ; v++ )
if(vis[v] == false && e[u][v] != inf)
{
if(dis[u] + e[u][v] < dis[v]) //如果起点到v点可以松弛
{
dis[v] = dis[u] + e[u][v]; //立马更新从起点到第v点的最短路径长度
num[v] = num[u];//v点的最短路径的条数 == u点最短路径条数(u是当前找到的最优点)
w[v] = w[u] + weight[v];//v点救援队的数量要增加
}
else if(dis[u] + e[u][v] == dis[v])//如果相等
{
num[v] = num[v] + num[u];//最短路径的条数肯定需要增加
if(w[u] + weight[v] > w[v])//如果救援队数量能变多就更好了
w[v] = w[u] + weight[v];
}
}
还有一种是使用dfs先mark一下。
代码
#include<iostream>
#include <string.h>
#include <algorithm>
using namespace std;
int n, m, c1, c2;
int e[510][510], weight[510], dis[510], num[510], w[510];
bool vis[510];
const int inf = 0x3f3f3f3f;
int main()
{
scanf("%d%d%d%d" , &n , &m , &c1 , &c2);
for(int i = 0; i < n ; i++)
cin >> weight[i];
//初始化
for(int i = 0 ; i < 510 ; i++)
{
for(int j = 0 ; j < 510 ; j++)
{
if(i == j) e[i][j] = 0;
else e[i][j] = inf;
}
}
memset(dis , inf , sizeof(dis));
for(int i = 0 ; i < m ; i++)
{
int a , b ,c ;
cin >> a >> b >> c;
e[a][b] = e[b][a] = c;
}
dis[c1] = 0;
w[c1] = weight[c1];
num[c1] = 1;
//Dij
for(int i = 0 ; i < n ;i++)
{
int u = 0;
int minn = inf;
for(int j = 0 ; j < n ; j++)
{
if(vis[j] == false && dis[j] < minn)
{
u = j;
minn = dis[j];
}
}
// if(u == -1) break;
vis[u] = true;
for(int v = 0 ; v < n ; v++ )
{
if(vis[v] == false && e[u][v] != inf)
{
if(dis[u] + e[u][v] < dis[v])
{
dis[v] = dis[u] + e[u][v];
num[v] = num[u];
w[v] = w[u] + weight[v];
}
else if(dis[u] + e[u][v] == dis[v])
{
num[v] = num[v] + num[u];
if(w[u] + weight[v] > w[v])
w[v] = w[u] + weight[v];
}
}
}
}
cout << num[c2] << " " << w[c2] ;
return 0;
}