题目描述
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.
输入
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, C 1 C_1 C1 and C 2 C_2 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 c 1 c_1 c1 , c 2 c_2 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 C 1 C_1 C1 to C 2 C_2 C2.
输出
For each test case, print in one line two numbers: the number of different shortest paths between C 1 C_1 C1 and C 2 C_2 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.
思路
看了看网上的有用djskl的最短路径算法,也还有结合加dfs的,但由于给定了起始点,因此只用深搜也是可以的(当然这里N比较少)。
代码
#include<iostream>
#include<cstdio>
#include<cstdio>
#include<vector>
using namespace std;
int number[1000];
int graph[1000][1000] = { 0 };
int visit[1000] = { 0 };
int m_d = 1000000000;
int m_r = 0;
int N, M, S, E;
int all = 1;
void dfs(int cur, int d, int r)
{
visit[cur] = 1;
if (cur == E)
{
if (d < m_d)
{
m_d = d;
all = 1;
m_r = r;
}
else if (d == m_d)
{
if (r > m_r)
{
m_r = r;
}
all++;
}
return;
}
if (m_d < d)
{
return;
}
for (int i = 0; i < N; i++)
{
if (visit[i] == 0 && graph[cur][i] != -1)
{
visit[i] = 1;
dfs(i, d + graph[cur][i], r + number[i]);
visit[i] = 0;
}
}
}
int main()
{
cin >> N >> M >> S >> E;
for (int i = 0; i < N; i++)
{
cin >> number[i];
}
for (int i = 0; i < N; i++)
{
for (int j = 0; j < N; j++)
{
graph[i][j] = -1;
}
}
for (int i = 0; i < M; i++)
{
int a, b, d;
cin >> a >> b >> d;
graph[a][b] = d;
graph[b][a] = d;
}
dfs(S, 0, number[S]);
visit[S] = 1;
printf("%d %d\n",all, m_r);
}
.