1003. Emergency (25)
来源:互联网 发布:子域名 需要备案吗 编辑:程序博客网 时间:2024/05/17 07:59
1003. Emergency (25)
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 21 2 1 5 30 1 10 2 20 3 11 2 12 4 13 4 1Sample Output
2 4
提交代码
解题思路,该题和天梯赛中紧急救援那道题几乎一模一样,那道天梯赛的题目反而更难一点。除了输出路径其他的一模一样。
具体的可以跳转看紧急救援那题http://blog.csdn.net/fscsmile/article/details/64954428
#include<iostream> #include<cstdio> #include<cstring> #include<string> #include<algorithm> typedef long long LL; using namespace std; const int maxn = 500 + 10, INF = 0x3f3f3f3f; int N, M, S, D; int val[maxn], dis[maxn], map[maxn][maxn], cntp[maxn], num[maxn]; //分别储存每个城市碎舞数量,和出发点的距离,地图,最短路径数量,总的队伍数量 bool vis[maxn]; void dijkstra(int s) { for (int i = 0; i < N; i++){ dis[i] = INF; } memset(cntp, 0, sizeof(cntp)); memset(num, 0, sizeof(num)); memset(vis, false, sizeof(vis)); dis[s] = 0; cntp[s] = 1;//最短路径条数为1 num[s] = val[s]; //出发点的队伍总数就是该地队伍总数 for (int i = 0; i < N; i++){ if (i != s) { dis[i] = map[s][i]; if (map[s][i] != INF){ cntp[i] = 1;//如果和出发点连通,路径条数为1 num[i] = val[i] + num[s];//该地总队伍数等于该地队伍数加上出发点的队伍数 } } } vis[s] = true; for (int t = 0; t < N - 1; t++){ int minn = INF, index = s; for (int i = 0; i < N; i++) { //找出离出发点距离最短的一个点 if (!vis[i] && minn > dis[i]){ minn = dis[i]; index = i; } } vis[index] = true;//吧该点加入到路径中 for (int i = 0; i < N; i++){ if (!vis[i] && map[index][i] < INF) { if (dis[index] + map[index][i] == dis[i]) { cntp[i] += cntp[index]; //更新最短路径数 if (num[index] + val[i] > num[i]){ num[i] = val[i] + num[index];//更新队伍数 } } else if (dis[index] + map[index][i] < dis[i]){ cntp[i] = cntp[index]; dis[i] = dis[index] + map[index][i]; //更新最短距离 num[i] = val[i] + num[index]; } } } } } int main() { scanf("%d%d%d%d", &N, &M, &S, &D); for (int i = 0; i < N; i++){ scanf("%d", &val[i]); } for (int i = 0; i < N; i++){ for (int j = 0; j < N; j++){ map[i][j] = (i == j ? 0 : INF); } } for (int i = 0; i < M; i++){ int u, v, w; scanf("%d%d%d", &u, &v, &w); map[u][v] = map[v][u] = w; } dijkstra(S); printf("%d %d\n", cntp[D], num[D]); return 0; }
- 1003. Emergency (25)
- 1003. Emergency (25)-PAT
- (PAT)1003. Emergency (25)
- 1003. Emergency (25)
- 【C++】1003. Emergency (25)*
- 1003. Emergency (25)
- PAT 1003. Emergency (25)
- 1003. Emergency (25)
- 1003. Emergency (25)
- PAT 1003. Emergency (25)
- 1003. Emergency (25)
- 1003. Emergency (25)
- [PAT]1003. Emergency (25)
- 1003. Emergency (25)
- 1003. Emergency (25)
- 1003. Emergency (25)
- 1003. Emergency (25)
- 1003. Emergency (25)
- Linux脚本——复制特定文件到特定文件夹
- java enum(枚举)的使用和总结
- Vijos P1332 最大的括号
- hdu 3652 B-number(数位dp)
- SAP HANA Cloud Platform
- 1003. Emergency (25)
- Nginx安装的前期准备工作
- C语言操作符总结
- Java
- 校招季总结
- 开机自动启动控制台并运行自己编译好的c++程序
- 【P2822】组合数问题 2016NOIP提高组 D2T1
- 猴子选大王
- thinkPHP三元运算符的用法及多种情况下switch的用法