问题描述

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 (≤) - the number of cities (and the cities are numbered from 0 to N−1), M - the number of roads, C​1​​ and C​2​​ - 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​2​​ 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​​ to C​2​​.

Output Specification:

For each test case, print in one line two numbers: the number of different shortest paths between C​1​​ and C​2​​, 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

思路:Floyd+DFS

遇到的问题:
1.一开始题看错了,问路径数量想当然地以为是问最短路径长度,又撞上了如此巧的Sample,惨
2.Floyd翻了个车,里面的if语句条件写的有点问题,碰巧又错得很艺术,简单的样例都能过,大囧 总结:太久不摸键盘了,手生了,以后还是得多多练习

代码:
#include <iostream>
using namespace std; int n,m,c1,c2,dist[][],res[],map[][],len=,sup,ressum,resmax=,count=;
// n城市数,m路径数,c1、c2起止城市,dist存距离,res存救援队数量,map存路径长度
// len存DFS路径长度,sup存c1到c2距离,ressum存DFS过程中救援队数量,resmax存最大救援队数量,count存路径数
bool proc[]; //存储DFS过程中城市经过情况,防止重复经过某一城市 void dfs(int x){
if (len>sup) return; // 走过距离超过c1、c2距离直接返回
if (x==c2){ //到达c2
if (len==sup){ //恰好是最短的路径
count++; //路径数+1
if (ressum>resmax) resmax=ressum; //判断此时救援队数量
}
return;
}
for (int i=;i<n;i++){
if (map[x][i]>= && proc[i]==false){ // i是x可以通往的城市
len+=map[x][i];
ressum+=res[i];
if (len<=sup){
proc[i]=true; //上述处理DFS准备
dfs(i);
proc[i]=false; //下述处理DFS结束后还原
}
len-=map[x][i];
ressum-=res[i];
}
}
return;
} int main(){
int x,y,dis;
//初始化
for (int i=;i<=;i++){
proc[i]=false;
for (int j=;j<=;j++){
dist[i][j]=map[i][j]=-;
}
}
//读入
cin >> n >> m >> c1 >> c2;
for (int i=;i<n;i++)
cin >> res[i];
for (int i=;i<m;i++){
cin >> x >> y >> dis;
dist[y][x]=dist[x][y]=map[y][x]=map[x][y]=dis;
}
//特殊情况判断
if (c1==c2){
cout << "1 " << res[c1];
return ;
}
//Floyd处理
for (int k=;k<n;k++)
for (int i=;i<n;i++)
for (int j=;j<n;j++)
if ((dist[i][j]< && dist[i][k]>= && dist[k][j]>=)||
(dist[i][j]>dist[i][k]+dist[k][j]&&dist[i][k]>=&&dist[k][j]>=))
dist[i][j]=dist[i][k]+dist[k][j];
//DFS
sup=dist[c1][c2];
proc[c1]=true;
ressum=res[c1];
dfs(c1);
//输出
cout << count << " " << resmax;
return ;
}

最新文章

  1. BZOJ2595[WC2008]游览计划
  2. 【软件工具】Driver Booster3永久激活法
  3. STL学习
  4. html5-websocket初探
  5. WebDriver 页面等待
  6. iptables的recent模块
  7. 转: javaWeb学习总结(见过最好的知识合集,相当给力,强烈推荐)
  8. LightOj_1265 Island of Survival
  9. poj1797 - Heavy Transportation(最大边,最短路变形spfa)
  10. Senparc.Weixin.MP SDK 微信公众平台开发教程 索引
  11. Python新手学习基础之运算符——比较运算符
  12. HDU多校练习第一场4608——I_Number
  13. Mycat 依赖包解读
  14. 使用mybatis注解@Options实现添加记录时返回主键值
  15. mysql监控执行的sql语句
  16. new.target
  17. Delphi中break,exit,abort跳出循环的比较
  18. RTS与CTS的含义【转】
  19. Unicode编码:保存中文cookie
  20. PCL(point cloud library) 学习——简介

热门文章

  1. github三步走(init;add . ;commit -m &quot;提交说明&quot;)
  2. Request库的安装与使用
  3. mysql 支持emoji表情
  4. Spark作业执行流程源码解析
  5. Tensorflow和pytorch安装(windows安装)
  6. make: *** No targets specified and no makefile found. Stop.错误
  7. 珠峰-6-http和http-server原理
  8. OpenCV4系列之图像梯度和边缘检测
  9. CSS权威指南(第三版)
  10. [PHP] 使用PHP在mongodb中进行count查询