【PAT】1003. Emergency (25) DFS 最短路径及最短路径数
2016-01-01 11:14
489 查看
1003. Emergency (25)
时间限制400 ms
内存限制
32000 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue
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
题意:有n个城市,每个城市都有自己的救援队,给定m条路径,问从城市c1到c2去救援的最短路径数是多少及能最大的救援队伍。
参考/article/1439565.html
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define maxN 505
#define Inf 0x3f3f3f3f
using namespace std;
int resTm[maxN];
bool visited[maxN];
int map[maxN][maxN];
int N, M, C1, C2;
int x, y, L;
int minDist = Inf;//最短路径距离和
int maxRes; //最大救援数和
int ccount; //路径数目
//start->起点 end->终点 dist->路径长度 resNum->救援队数目
void dfs(int start, int end,int dist, int resNum)
{
if (start == end)//停止条件
{
if (dist < minDist)
{
minDist = dist;
ccount = 1;//找到更短的路径
maxRes = resNum;//救援队数目
}
else
{
if (dist == minDist)
{
ccount++;//最短路径数+1
if (resNum > maxRes)//更新最大救援数
{
maxRes = resNum;
}
}
}
return;
}
if (dist > minDist)return;//当前路径长度大于最短路径长度->进行剪枝
for (int k = 0; k < N; k++)
{
if (!visited[k] && map[start][k] != Inf)//判断是否访问过且是否可达
{
visited[k] = true;//回溯时避免出现环,即从end又找回到start
dfs(k, end,map[start][k]+dist, resTm[k] + resNum);//对k的邻点进行dfs深搜
visited[k] = false;//访问完所有邻点(邻点的邻点)后撤销标记,方便后续查找
}
}
}
int main1003()
{
//freopen("in.txt", "r", stdin);
//freopen("out.txt", "w", stdout);
memset(map, Inf, sizeof(map));
cin >> N >> M >> C1 >> C2;
for (int i = 0; i < N; i++)
{
cin >> resTm[i];
visited[i] = false;
}
for (int i = 0; i < M; i++)
{
cin >> x>>y >> L;
map[x][y] = map[y][x] = min(L,map[x][y]);
}
dfs(C1, C2, 0, resTm[C1]);
cout << ccount << " " << maxRes << endl;
return 0;
}
/*
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 1Sample Output
2 4
*/
相关文章推荐
- AES加密算法C代码分析
- Spring不能注入Static变量的原因及Spring注入静态变量
- Hadoop 权威指南
- codevs 1021 玛丽卡
- 客户端在连接服务器报10061错误的原因
- codevs 1519 过路费
- oozie ErrorCode 含义
- watchman--no-pretty get-sockname returned with exit code 1 ERROR
- 20160101罗振宇的跨年演讲
- B. 沙漠之旅(分组背包)
- HDFS 和 YARN 的 HA 故障切换
- CCA Spark and Hadoop 开发者认证技能点【2016只为hadoop达到巅峰】
- Pashmak and Garden
- mysql基础学习笔记
- HDFS 用户手册
- poj Kaka's Matrix Travels
- halcon基础之Blob Analysis
- 详解 WordCount 运行后历史日志记录
- leetcode第8题——*String to Integer (atoi)
- Eclipse搭建android环境及Genymotion模拟器安装问题解决方法