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.
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
AC参考代码:
#include <stdio.h>
#include <stdlib.h>
const int MAX = 501; //The max number of cities
const int MAX_INT = 999999999;
int path[MAX][MAX],isVisited[MAX],weight[MAX]; //The variable about Cities
int minDist,maxWeight,cnt,cities; //The variable of recording the boundary value
void initGraph(int cities)
{
for(int i=0;i<cities;i++){
isVisited[i] = 0; //The value of 0 means not visited
for(int j=0;j<cities;j++){
path[i][j] = MAX_INT; //The initial length of path is infinite
}
}
}
void DFS(int start,const int dest,int dist,int weit)
{
if(start == dest){ //The boundary value
if(dist < minDist){ //There is new minist path
minDist = dist;
cnt = 1; //Init the new recording value
maxWeight = weit;
}
else if(dist == minDist){ //More minist path
cnt++;
if(maxWeight < weit){
maxWeight = weit;
}
}
return;
}
if(dist > minDist) return; //pruning,optimise the algorithm
for(int i=0;i<cities;i++){
if(isVisited[i]==0 && path[start][i]!=MAX_INT){ //The city is not visited and there is a path bewteen two cites
isVisited[i] = 1;
DFS(i,dest,dist+path[start][i],weit+weight[i]);
isVisited[i] = 0; //When you back,this node could be one of another path,so remark it.
}
}
}
int main()
{
int N,M,c1,c2,x,y,temp;
scanf("%d %d %d %d",&N,&M,&c1,&c2);
cities = N; minDist = MAX_INT,cnt = 0;
initGraph(cities);
for(int i=0;i<N;i++){
scanf("%d",&weight[i]);
}
for(int i=0;i<M;i++){
scanf("%d %d %d",&x,&y,&temp);
path[x][y] =path[y][x] = temp;
} //Finish data input
DFS(c1,c2,0,weight[c1]);
printf("%d %d",cnt,maxWeight);
return 0;
}
此题主要考的是DFS深度优先遍历+递归的考点,涉及的变量相对多点比较综合
注意点:
1:在做DFS的时候临界点要注意:
1、找到最短路径,更新所有信息
2、找到同样长度的最短路径,更新cnt的计数值,同时对weight作判断
3、其他直接return 就ok
2:if(dist > minDist) return; 实现搜索的剪枝,优化代码
3:还有初始化图的时候 path[x][y] =path[y][x] 要完全初始化
4:DFS(i,dest,dist+path[start][i],weit+weight[i]); isVisited[i] = 0;
DFS完子路径的时候回退的时候需要将i结点重置为未访问,因为在图中深度优先遍历的时候下次可能再次用到。
其他没什么注意的,水水过了
分享到:
相关推荐
安装说明:解压后打开Setup.exe安装程序,安装后一定不要立即运行Spy Emergency。将补丁目录内的Patch.x86.exe文件复制到软件安装目录下并运行,点击patch对源程序打补丁。启动软件后,界面为英文,你需要点击左侧...
This header file contains global constant enum definitions, global variable declaration.
IBM Intelligent Operations Center for Emergency Management.PDF
Written by authors who are practicing emergency physicians and emergency medicine educators, Clinical Emergency Medicine distills the entire content of the emergency medicine curriculum into less than...
Spy Emergency是一款由斯洛伐克开发的专业好用的电脑杀毒软件。软件专注于查杀间谍软件、木马、键盘记录机等病毒。软件独创多轮检测技术,并提供常规的实时防护等。Spy Emergency界面美观简洁、使用方便,杀毒速度快...
3.emergency_rescue.mp4紧急救援的视频; 4.2020_09_03__opencv.exe可以直接运行的可执行文件; 5.2020_09_03__opencv.cpp源码; 6.请结合博客:“【opencv-c++】opencv之dnn加载darknet的yolov3模型(1)”
...
...
新版完整标准 BS ISO 22329-2021 Security and resilience — Emergency management —Guidelines for the use of social media in emergencies.pdf
Csgo automatic emergency stop
地铁虚拟现实仿真,virtual reality; emergency evacuation; fire simulation; visualization
Spy Emergency 是一款来自斯洛伐克的反间谍产品,专注于查杀间谍软件、木马、键盘记录机,以及广告程序等。顾名思义是『反间谍应急中心』,界面极酷,以火红的默认主题彰显其强力的查杀能力。数据库有十余兆之大,...
simulation and design of automatic diesel generator with asynchronous machine
Emergency
25 4.1 eCall session dynamic view: end-to-end In-band signaling .................................................................. 25 4.1.1 MSD transfer in push mode ....................................
CANopen high-level protocol for CAN-bus H. Boterenbrood NIKHEF, Amsterdam March 20, 2000 Contents ...1 INTRODUCTION ............5.5 EMERGENCY OBJECTS ........ 22 REFERENCES .......... 23
Emergency Plan for Fast Fault Recovery of the VoLTE Solution
Identification and development of microgrids emergency control procedures. PhD Dissertation
内含两千多张,224x224彩色图像,用于训练一个紧急车辆的识别网络,我会利用该数据集,做一个Pytorch的预训练模型多重CNN实现模型的对比实验。