PAT 甲级1003. Emergency (25) DIJKSTRA
来源:互联网 发布:淘宝网购物女装冬装 编辑:程序博客网 时间:2024/05/17 22:21
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<cstdio>#include<cstring>#include<algorithm>#include<queue>using namespace std;const int size=500;const int maxint=99999999;int dis[size]; //最短路 int cnt[size];//最短路个数int sum[size];//队伍最多数 int num[size];//每个城市人数 int map[size][size];//记录图 int n,m,c1,c2;void init(){for (int i=0;i<n;i++){dis[i]=maxint;cnt[i]=1;sum[i]=0;for (int j=0;j<n;j++)map[i][j]=maxint;}}void dijkstra(){int i,j,themin,pos;dis[c1]=0;int vis[size]={0};sum[c1]=num[c1];for (i=0;i<n;i++){int themin=maxint;for (j=0;j<n;j++)if (!vis[j]&&dis[j]<themin){pos=j;themin=dis[j];}vis[pos]=1;for (j=0;j<n;j++)if (!vis[j]&&map[pos][j]!=maxint){if (dis[pos]+map[pos][j]<dis[j]) {dis[j]=dis[pos]+map[pos][j];cnt[j]=cnt[pos];//最短路数等于上一节点最短路数 sum[j]=sum[pos]+num[j];}else if (dis[pos]+map[pos][j]==dis[j]){cnt[j]=cnt[j]+cnt[pos];//当前最短数加上新的路带来的路数 sum[j]=max(sum[j],sum[pos]+num[j]);}}}}int main(){int i,j,k,t,x,y,v;scanf("%d%d%d%d",&n,&m,&c1,&c2);init();for (i=0;i<n;i++)scanf("%d",&num[i]);for (i=0;i<m;i++){scanf("%d%d%d",&x,&y,&v);map[x][y]=map[y][x]=v;}dijkstra();printf("%d %d\n",cnt[c2],sum[c2]);return 0;}
- PAT 甲级1003. Emergency (25) DIJKSTRA
- 1003. Emergency (25)-PAT甲级真题(Dijkstra算法)
- 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 甲级试题1003 Emergency (25) dijkstra变形应用
- PAT 甲级 1003Emergency(Dijkstra最短路)
- 【PAT甲级】【C++】1003. Emergency (25)
- PAT TEST甲级1003. Emergency (25)
- PAT甲级练习1003. Emergency (25)
- PAT 1003. Emergency (25) Dijkstra算法 + DFS
- 合并表记录
- Hibernate 一对一主键双向关联
- 剑指offer面试题45 圆圈中最后剩下的数字(java实现)
- 关于maven包管理版本冲突的问题
- Ubuntu14.04安装pycharm的步骤
- PAT 甲级1003. Emergency (25) DIJKSTRA
- 新手上路之面向对象 笔记
- Mysql-创建数据库
- APPLYING DEEP LEARNING TO ANSWER SELECTION: A STUDY AND AN OPEN TASK 论文阅读
- nohup /dev/null 2>&1 含义详解
- 389. Find the Difference
- 挑战练习:添加后退按钮(二)基于《Android编程权威指南》
- android详细错误log日志文件获取
- Android8.0 PackageManagerService相关 -- APK安装和install 的变更和源码浅析