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 Specification:

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, C​1​​ and C​2​​ - 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 c​1​​, c​2​​ 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 C​1​​ to C​2​​.

Output Specification:

For each test case, print in one line two numbers: the number of different shortest paths between C​1​​ and C​2​​, 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

题意:n个城市m条路,每个城市有救援小组,联通城市之间的距离已知。
给定起点和终点,求从起点到终点的最短路径条数以及最短路径上的救援小组数目之和最大的那个 题解:和求Disjktra多重权值一样,
在更新中间节点路径更短的时候,顺带更新救援小组的数量
当最短路径相同的时候,更新最短路径条数
#include<iostream>
#include<cstdio>
#include<cstring>
#define MAX 1000000
using namespace std;
int a[][];
int num[];//每个村庄有多少个救援队
int dis[];//起点S到村庄j的最短距离
int val[];//起点S到村庄j可以聚集救援队的数量
int vis[];
int cnt[];//起点S到村庄j的最短路径有多少条
void dijkstra(int start, int n)
{
int i, j, k, min;
for (i = ; i < n; i++)//(初始化)存放起点到其余顶点的距离
dis[i] = a[start][i]; dis[start] = ;
val[start] = num[start];
cnt[start] = ; for (i = ; i < n; i++)
{
min = MAX;
k = -;
for (j = ; j < n; j++) //求出初始起点s直接到j点距离最短的点的下标值
{
if (vis[j]== && min > dis[j])
{
min = dis[j];
k = j;
}
}
vis[k] = ;
if (k == -)
return;
for (j = ; j <= n; j++)
{
if (dis[j] > dis[k] + a[k][j])//若找到其他途径比从1号顶点直接到目的顶点的距离短,则替换掉
{
cnt[j]=cnt[k];
dis[j] = dis[k] + a[k][j];
val[j]=num[j]+val[k];
} else if (dis[j] == dis[k] + a[k][j])//如果距离相同
{
cnt[j]=cnt[j]+cnt[k];
val[j] = max(val[j],val[k] + num[j]);//更新救援队数量
}
}
}
} int main()
{
int n, m;
int i;
int s, e;
cin>>n>>m>>s>>e;
for(int i=;i<n;i++)
cin>>num[i];
int t1,t2,t3;
memset(vis, , sizeof(vis));
memset(cnt, , sizeof(cnt));
memset(cnt, , sizeof(val));
memset(a, MAX, sizeof(a));//初始化所有点的距离/花费为无穷大
for (i = ; i < m; i++)
{
scanf("%d%d%d", &t1, &t2, &t3);
if (a[t1][t2] > t3)//去重
a[t1][t2] = a[t2][t1] = t3;
}
dijkstra(s, n);
printf("%d %d\n", cnt[e], val[e]);
return ;
}
 

1003 Emergency (25分) 求最短路径的数量的更多相关文章

  1. PAT 1003 Emergency (25分)

    As an emergency rescue team leader of a city, you are given a special map of your country. The map s ...

  2. 【PAT甲级】1003 Emergency (25 分)(SPFA,DFS)

    题意:n个点,m条双向边,每条边给出通过用时,每个点给出点上的人数,给出起点终点,求不同的最短路的数量以及最短路上最多能通过多少人.(N<=500) AAAAAccepted code: #in ...

  3. 1003 Emergency (25分)

    As an emergency rescue team leader of a city, you are given a special map of your country. The map s ...

  4. PAT 1003. Emergency (25) dij+增加点权数组和最短路径个数数组

    1003. Emergency (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue As an emerg ...

  5. PAT 解题报告 1003. Emergency (25)

    1003. Emergency (25) As an emergency rescue team leader of a city, you are given a special map of yo ...

  6. PAT 1003. Emergency (25)

    1003. Emergency (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue As an emerg ...

  7. 1003 Emergency (25)(25 point(s))

    problem 1003 Emergency (25)(25 point(s)) As an emergency rescue team leader of a city, you are given ...

  8. PAT 甲级 1003. Emergency (25)

    1003. Emergency (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue As an emerg ...

  9. PAT 甲级1003 Emergency (25)(25 分)(Dikjstra,也可以自己到自己!)

    As an emergency rescue team leader of a city, you are given a special map of your country. The map s ...

随机推荐

  1. 【Python】一些函数

    Python 数字类型转换 有时候,我们需要对数据内置的类型进行转换,数据类型的转换,你只需要将数据类型作为函数名即可. int(x) 将x转换为一个整数. float(x) 将x转换到一个浮点数. ...

  2. 【网搜】禁止 number 输入非数字(Android仍有问题)

    目的:使用 number 表单,让其只可输入数字. 问题:ios 可正常限制,Android 仍可输入  [ e | . |  - |  + ]   这4个字符.猜测这4个字符在数值中为科学记数.小数 ...

  3. C语言程序设计100例之(26):二进制数中1的个数

    例26   二进制数中1的个数 问题描述 如果一个正整数m表示成二进制,它的位数为n(不包含前导0),称它为一个n位二进制数.所有的n位二进制数中,1的总个数是多少呢? 例如,3位二进制数总共有4个, ...

  4. Python隐藏特性:字符串驻留、常量折叠

    下面是Python字符串的一些微妙的特性,绝对会让你大吃一惊. 案例一: >>> a = "some_string" >>> id(a) 140 ...

  5. JavaScript 对象属性与方法

    对象的创建: 1 字面量创建 var obj = {a:1,b:2}; 2 构造函数创建 var obj = new Object(); obj.a = 1; obj.b = 2; 3 Object. ...

  6. 理解Spring 容器、BeanFactory 以及 ApplicationContext

    一.spring 容器理解 spring 容器可以理解为生产对象(Object)的地方,在这里容器不只是帮助我们创建对象那么简单,它负责了对象的整个生命周期-创建.装配.销毁.而这里对象的创建管理的控 ...

  7. 《JavaScript高级程序设计》读书笔记(五)引用类型

    内容---使用对象---创建并操作数组---理解基本的JavaScript类型---使用基本类型和基本包装类型 引用类型--引用类型的值(对象)是引用类型的一个实例--在ECMAScript中,引用类 ...

  8. 6.Python字符串

    #header { display: none !important; } } #header-spacer { width: 100%; visibility: hidden; } @media p ...

  9. 九、Appium-python-UI自动化之通过text定位

    1.通过xpath定位text xpath路径为://android.widget.EditText[@text='请输入包含街道的完整地址'] 2.通过AndroidUIAutomator # 这个 ...

  10. Update(Stage4):Structured Streaming_介绍_案例

    1. 回顾和展望 1.1. Spark 编程模型的进化过程 1.2. Spark 的 序列化 的进化过程 1.3. Spark Streaming 和 Structured Streaming 2. ...