PAT 甲级 1003. Emergency
来源:互联网 发布:淘宝机械人 编辑:程序博客网 时间:2024/05/20 15:12
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
题目有坑点,要求的是最短路的个数而不是长度。用dijkstra写即可
#include<iostream>#include<cstdio>#include<cstring>using namespace std;const int inf=0x3f3f3f3f;int dis[510];int map[510][510];int book[510];int rescue[510];int kount[510];int pathKount[510];int n,m,c1,c2;void dijkstra(){kount[c1]=rescue[c1];pathKount[c1]=1;//for(int i=0;i<n;i++) dis[i]=map[c1][i];dis[c1]=0;book[c1]=1;for(int i=0;i<n;i++){int minn=inf,flag=c1;for(int j=0;j<n;j++){if(!book[j]&&dis[j]<minn){minn=dis[j];flag=j;}}book[flag]=1;for(int j=0;j<n;j++){if(!book[j]){if(dis[j]>dis[flag]+map[flag][j]){dis[j]=dis[flag]+map[flag][j];kount[j]=kount[flag]+rescue[j];pathKount[j]=pathKount[flag];}else if(dis[j]==dis[flag]+map[flag][j]){pathKount[j]+=pathKount[flag];if(kount[j]<kount[flag]+rescue[j]){kount[j]=kount[flag]+rescue[j];}}}}}return;}int main(){while(cin>>n>>m>>c1>>c2){memset(dis,inf,sizeof(dis));memset(book,0,sizeof(book));memset(map,inf,sizeof(map));for(int i=0;i<n;i++) scanf("%d",&rescue[i]);while(m--){int a,b,len;scanf("%d%d%d",&a,&b,&len);if(map[a][b]>len) map[a][b]=map[b][a]=len;}dijkstra();cout<<pathKount[c2]<<" "<<kount[c2]<<endl;}}
- PAT 甲级 1003.Emergency
- [PAT-甲级]1003.Emergency
- PAT 甲级 1003. Emergency
- Pat甲级1003. Emergency (25)
- 【PAT甲级】1003. Emergency (25)
- PAT甲级1003. Emergency (25)
- 1003. Emergency (25) PAT 甲级
- PAT甲级 1003. Emergency (25)
- PAT甲级1003. Emergency (25)
- PAT 甲级 1003. Emergency (25)
- 【PAT甲级】【C++】1003. Emergency (25)
- PAT TEST甲级1003. Emergency (25)
- PAT甲级练习1003. Emergency (25)
- PAT 甲级1003. Emergency (25) DIJKSTRA
- pat 甲级 1003 Emergency
- PAT甲级练习题A1003. Emergency
- 1003. Emergency (25)-PAT甲级真题(Dijkstra算法)
- PAT甲级 1003.Emergency(25) 题目翻译与答案
- 268. Missing Number
- Galera Cluster for MySQL启动不了,一直不停的自动重启!
- LinkedHashSet实现Hashcode和equals方法
- 26.Remove Duplicates from Sorted Array
- CentOS下Hive2.3.0单机模式安装详解
- PAT 甲级 1003. Emergency
- CDH安装
- 啊哈,灵机一动-----记网易内推编程题堆棋子
- Django编写RESTful API(二):请求和响应
- 27.Remove Element
- Win10 配置tensorflow1.3 GPU版本以及tensorboard的使用
- 修改eclipse工程jdk版本
- Maven之scope含义的说明-yellowcong
- 堆排序