admin管理员组文章数量:1577824
题目
1003. Emergency (25)
时间限制
400 ms
内存限制
65536 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
解题思路
- 1.题目大意是找出最短路径的条数和找出这些最短路径中
team
数最多的一组。- 2.可以先用
Dijkstra
算法求最短路径,其中用pre
保存前一个节点,由于最短路径可能有多条,所以前节点也可能有多个,可用数组保存,最后在用DFS
算法从c2
找到c1
,并计算路径个数和找到最大值。- 3.也可以直接用
dfs
算法求俩点间的最短路径
AC代码
#include<iostream>
#include<cstring>
#include<vector>
#include<map>
#include<algorithm>
using namespace std;
int n,m,c1,c2;
int road[501][501],dist[501],numOfTeam[501];
bool visited[501];
#define INF 0x3f3f3f3f
map<int ,vector<int> > pre;
//最短路径条数和每条路径能带的team的个数,以及最大的team
int cnt=0,team_city[501],MAX_TEAM=0;
void Dijkstra(int c){
//标记为访问
visited[c] = true;
//更新相邻边最短路径
for (int i = 0; i < n; ++i) {
if (dist[i] > dist[c] + road[c][i]&&road[c][i]!=INF) {
dist[i] = dist[c] + road[c][i];
//找到比之前小的路径了,pre要重置
pre[i].clear();
pre[i].push_back(c);
//如果是相通的,并且等于之前找到的最短路径,则pre增加一个数
}else if (dist[i] == dist[c] + road[c][i]&&road[c][i]!=INF) {
pre[i].push_back(c);
}
}
//找到为访问的最小的dist[i],并记录下一个要访问的点
int tem_min = INF,nx=-1;
for (int i = 0; i < n; ++i) {
//找到未访问的最小值
if (visited[i]) {
continue;
}else if (dist[i] < tem_min) {
tem_min = dist[i];
nx = i;
}
}
//访问下一点
if (nx != -1) {
Dijkstra(nx);
}
}
void DFS(int c){
//如果已经找到了c1,则cnt+1,并return
if (c==c1) {
MAX_TEAM = max(MAX_TEAM,team_city[c1]);
cnt++;
return;
}
//找到一个儿子并访问
vector<int> tem = pre[c];
for (int i = 0; i < tem.size(); ++i) {
//更新下一点的team,并访问下一点
team_city[tem[i]] = team_city[c] + numOfTeam[tem[i]];
DFS(tem[i]);
}
}
int main(int argc, char *argv[])
{
memset(road,INF,sizeof(road));
memset(dist,INF,sizeof(dist));
memset(visited,false,sizeof(visited));
cin >> n >> m >> c1 >> c2;
for (int i = 0; i < n; ++i) {
cin >> numOfTeam[i];
}
int where,to,cost;
for (int i = 0; i < m; ++i) {
cin >> where >> to >> cost;
road[where][to] = road[to][where] = cost;
}
//用Dijkstra算法找到最短路径并保存pre
//初始化
dist[c1] = 0;
Dijkstra(c1);
//用DFS算法遍历(从c2到c1),记录路径条数和最大team数
//更新visited
memset(visited,false,sizeof(visited));
//初始化
team_city[c2] = numOfTeam[c2];
DFS(c2);
cout << cnt << " " << MAX_TEAM << endl;
return 0;
}
测试用例(作者)
6 9 0 5
1 2 1 5 3 4
0 1 1
0 2 2
0 3 1
1 2 1
2 3 1
2 4 1
2 5 1
3 4 1
4 5 1
6 9 0 2
1 2 1 5 3 4
0 1 1
0 2 2
0 3 1
1 2 1
2 3 1
2 4 1
2 5 1
3 4 1
4 5 1
6 9 0 4
1 2 1 5 3 4
0 1 1
0 2 2
0 3 1
1 2 1
2 3 1
2 4 1
2 5 1
3 4 1
4 5 1
答案
4 13
3 7
1 9
版权声明:本文标题:PAT 1003. Emergency (25)(俩点间最短路径) 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://m.elefans.com/dianzi/1727823965a1132049.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论