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
题目分析: dfs操作实现即可。
代码:
#include<stdafx.h>
#include <iostream>
using namespace std;
int N, M, C1, C2;
int Rescue[505] = {0};
int L[505][505] ={0};
bool visited[505] = {false};
int currDis = 0;
int MinDis = 1<<30;
int NumRoad = 0;
int MaxRescue = 0;
int CurrRescue = 0;
void dfs(int curr)
{
int i;
if (curr == C2)
{
if (currDis < MinDis)
{
MinDis = currDis;
MaxRescue = CurrRescue;
NumRoad = 1;
}
else if(currDis == MinDis)
{
if (CurrRescue > MaxRescue)
{
MaxRescue = CurrRescue;
}
NumRoad++;
}
return;
}
for (i = 0;i<N;i++)
{
if ( i != curr && !visited[i] && L[i][curr])
{
currDis += L[i][curr];
CurrRescue += Rescue[i];
visited[i] = true;
dfs(i);
currDis -= L[i][curr];
CurrRescue -= Rescue[i];
visited[i] = false;
}
}
}
int main()
{
scanf("%d %d %d %d", &N, &M, &C1, &C2);
int i;
for (i = 0;i<N;i++)
{
scanf("%d", &Rescue[i]);
}
//int j;
int tmp1, tmp2, tmp3;
for (i = 0;i<M;i++)
{
scanf("%d %d %d", &tmp1, &tmp2, &tmp3);
L[tmp1][tmp2] = tmp3;
L[tmp2][tmp1] = tmp3;
}
visited[C1] = true;
//currDis += Rescue[C1];
CurrRescue += Rescue[C1];
dfs(C1);
printf("%d %d", NumRoad, MaxRescue);
return 0;
}
本文深入探讨了信息技术领域的多个细分技术领域,包括前端开发、后端开发、移动开发、游戏开发等,提供了关于大数据开发、开发工具、嵌入式硬件、嵌入式电路知识、嵌入式开发环境、音视频基础、音视频直播流媒体、图像处理AR特效、AI音视频处理、测试、基础运维、DevOps、操作系统、云计算厂商、自然语言处理、区块链、隐私计算、文档协作与知识管理、版本控制、项目管理与协作工具、有监督学习、无监督学习、半监督学习、强化学习、数据安全、数据挖掘、数据结构、算法、非IT技术、自动推理、人工神经网络与计算、自动驾驶、数据分析、数据工程、程序设计方法、数据库理论、代码管理工具等领域的最新技术和实践。
1305

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



