您的位置:首页 > 其它

pat甲级1003. Emergency (25)

2018-03-12 21:02 399 查看

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.InputEach 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.OutputFor 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

算法设计:

使用Dijkstra算法的经典题目,要输出最短路径的数量和能够召集的救护队的最多数量,只需在原有Dijkstra算法上做适当修改即可。

注意点:

此题为无向无环图,不要存储成有向图。

c++代码:

#include<bits/stdc++.h>
using namespace std;
struct Road{
int v;//道路尽头的城市编号
int length;//道路长度
Road(){
v=0;
length=0;
}
Road(int vv,int l):v(vv),length(l){}
};
vector<vector<Road>>graph(505);//图
int city[505];//每个城市救护队的数量
int dis[505];//到达每个城市的最短距离
int pathNum[505];//到达每个城市的最短路径的数量
int teamNum[505];//到达每个城市的救护队数量
bool visit[505];//每个城市是否被访问过
int N,M,C1,C2;
void Dijkstra(){
while(!visit[C2]){//如果终点城市还没有被访问,继续循环
//找出目前距离最短的还没有被访问的城市
int MIN=INT_MAX,v=-1;
for(int i=0;i<N;++i){
if(!visit[i]&&dis[i]<MIN){
MIN=dis[i];
v=i;
}
}
if(v==-1)return;//v==-1表示是非连通图,直接返回
visit[v]=true;//标记为已访问
for(int i=0;i<graph[v].size();++i){
int temp=graph[v][i].v;
if(!visit[temp]&&dis[temp]>dis[v]+graph[v][i].length){
dis[temp]=dis[v]+graph[v][i].length;//更新最短路径长度
pathNum[temp]=pathNum[v];//更新最短路径数量
teamNum[temp]=teamNum[v]+city[temp];//更新城市的救护队数量
}else if(dis[temp]==dis[v]+graph[v
4000
][i].length){
pathNum[temp]+=pathNum[v];//增加最短路径数量
//找出能够召集最多的城市救护队数量
teamNum[temp]=max(teamNum[temp],teamNum[v]+city[temp]);
}
}
}
}
int main(){
scanf("%d%d%d%d",&N,&M,&C1,&C2);
for(int i=0;i<N;++i)
scanf("%d",&city[i]);
while(M--){
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
graph[a].push_back(Road(b,c));
graph[b].push_back(Road(a,c));
}
fill(dis,dis+N,INT_MAX);//将最短路径均设置为最大值
dis[C1]=0;//C1城市是起点,最短路径为0
pathNum[C1]=1;//C1城市是起点,最短路径数量为1
teamNum[C1]=city[C1];//C1城市是起点,能够召集的救护队数量为本城市的数量
Dijkstra();
printf("%d %d",pathNum[C2],teamNum[C2]);//输出
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: