PAT甲级 1003. Emergency (25)
来源:互联网 发布:公安部软件违章查询 编辑:程序博客网 时间:2024/05/20 14:39
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
最短路变形:
#include <iostream>#include<queue>#include<cstdio>#include<algorithm>#include<cmath>#include<set>#include<cstring>using namespace std;#define LL long longint n,m,a,b,c,e,f,g,x,y;const int maxn=1e3+10;const int inf=1e9;int rec[maxn],r[maxn],path[maxn],pathnum[maxn],d[maxn];int Map[maxn][maxn];int st,en;typedef pair<int,int> P;int dijkstra(int st){for(int i=0;i<n;i++){d[i]=inf;rec[i]=r[i];pathnum[i]=1;}pathnum[st]=1;d[st]=0;priority_queue<P,vector<P>,greater<P> > q;q.push(P(0,st));while(!q.empty()){P p=q.top();q.pop();int v=p.second;if(d[v]<p.first) continue;for(int i=0;i<n;i++){if(d[i]>d[v]+Map[v][i]){d[i]=d[v]+Map[v][i];path[i]=v;pathnum[i]=pathnum[v];rec[i]=rec[v]+r[i];q.push(P(d[i],i));}else if(d[i]==d[v]+Map[v][i]){pathnum[i]+=pathnum[v];if(rec[i]<rec[v]+r[i]){rec[i]=rec[v]+r[i];}}}}}int main(){scanf("%d%d%d%d",&n,&m,&st,&en);for(int i=0;i<n;i++) for(int j=0;j<n;j++) Map[i][j]=inf;for(int i=0;i<n;i++) scanf("%d",&r[i]);for(int i=0;i<m;i++){scanf("%d%d%d",&a,&b,&c);Map[a][b]=Map[b][a]=c;}dijkstra(st);printf("%d %d\n",d[en],rec[en]); return 0;}
- 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 甲级 1003.Emergency
- [PAT-甲级]1003.Emergency
- PAT 甲级 1003. Emergency
- 【PAT甲级】【C++】1003. Emergency (25)
- PAT TEST甲级1003. Emergency (25)
- PAT甲级练习1003. Emergency (25)
- PAT 甲级1003. Emergency (25) DIJKSTRA
- 1003. Emergency (25)-PAT甲级真题(Dijkstra算法)
- PAT甲级 1003.Emergency(25) 题目翻译与答案
- pat 甲级 1003 Emergency
- pat甲级_路径问题(例题:pat 1003 Emergency (25))
- 基于spring的文件上传
- 类型转换
- 1.7 Getting Started
- jquery侧边栏(仿购物网站)
- 傅里叶变换(由公式推导出来的c++源代码)
- PAT甲级 1003. Emergency (25)
- Linux下Pycharm配置pyqt5开发环境配置
- design项目:2、mybatis + easyui datagrid 分页查询功能介绍
- 1.8 Getting Started
- shell脚本之$(),单引号和eval的区别
- groovy Range
- 红米4手机行间距随字体大小变化
- Python 字典常用操作
- 从零开始的LED灯(基于嵌入式开发S3C2440)