1003 Emergency (25)(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.

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
//转自:https://www.cnblogs.com/549294286/p/3571448.html
#include <iostream>
#include <cstring>
#include <cstdio> using namespace std; #define INF 0x3f3f3f3f
#define MX 501 int mp[MX][MX];
int v[MX];
int dist[MX];
int amount[MX];
int teams[MX];
int pathcount[MX];
int N,M,start,en; void dijkstra(int s){
amount[s] = teams[s];//s为开始时
dist[s] = ;
pathcount[s] = ;//目前有一条路 while (){
int u, dmin=INF;
for (int i=; i<N; i++){
if (v[i]== && dist[i]<dmin){//v一开始全为0
dmin = dist[i];//第一次循环,选出来的点是s
u = i;//
}
}
if (dmin==INF || u==en) break;
v[u] = ;//设置为被访问过,每次都选最小的那个点出来。
printf("\n");
for (int i=; i<N; i++){
if(v[i]==){
if (dist[i] > dist[u] + mp[u][i]){
dist[i] = dist[u] + mp[u][i];
amount[i] = amount[u] + teams[i];
pathcount[i] = pathcount[u];
}else if (mp[u][i]!=INF&&dist[i] == dist[u] + mp[u][i]){
pathcount[i] += pathcount[u];
if (amount[i] < amount[u] + teams[i])
amount[i] = amount[u] + teams[i];
}
printf("%d %d %d %d %d\n",u,i,dist[i],pathcount[i],amount[i]);
} }
}
} int main()
{
scanf("%d%d%d%d", &N,&M,&start,&en);
for (int i=; i<N; i++)
{
scanf("%d", &teams[i]);//每个点所有的救援队数
}
for (int i=; i<N; i++)
{
dist[i] = INF;//每一个距离初始化为正无穷。
for (int j=; j<N; j++)
mp[i][j] = INF;
}
for (int i=; i<M; i++)
{
int c1, c2, L;
scanf("%d%d%d", &c1,&c2,&L);
mp[c1][c2] = mp[c2][c1] = L;//将没有边的设置为正无穷。
} dijkstra(start);
printf("%d %d", pathcount[en], amount[en]); return ;
}

//意思就是给出一个图,每个点有权值,边有权重,找出最短的权重的边,并且记录点的最大的权值和。就是要找出所有的最短边,并且进行比较。

//这里是给出了一个循环,每次找出最短的,并且遍历所有的点。看是否可以更新距离。如果到当前点的距离相等的话,那么就路径数相加,并且amount[i]取两者较大值。这是一种题型。应该会的。

代码来自:https://www.liuchuo.net/archives/2359

#include <iostream>
#include <algorithm>
using namespace std;
int n, m, c1, c2;
int e[][], weight[], dis[], num[], w[];
bool visit[];
const int inf = ;
int main() {
scanf("%d%d%d%d", &n, &m, &c1, &c2);
for(int i = ; i < n; i++)
scanf("%d", &weight[i]);
fill(e[], e[] + * , inf);//原来二维数组是这样初始化的。
fill(dis, dis + , inf);
int a, b, c;
for(int i = ; i < m; i++) {
scanf("%d%d%d", &a, &b, &c);
e[a][b] = e[b][a] = c;
}
dis[c1] = ;
w[c1] = weight[c1];
num[c1] = ;//到当前点的共计路径条数。
for(int i = ; i < n; i++) {
int u = -, minn = inf;
for(int j = ; j < n; j++) {
if(visit[j] == false && dis[j] < minn) {
u = j;
minn = dis[j];
}
}
if(u == -) break;//迪杰斯特拉都有这么一个判断,就是找不到其他的可以访问的点了。
visit[u] = true;
for(int v = ; v < n; v++) {
if(visit[v] == false && e[u][v] != inf) {
if(dis[u] + e[u][v] < dis[v]) {
dis[v] = dis[u] + e[u][v];
num[v] = num[u];
w[v] = w[u] + weight[v];//点权相加
} else if(dis[u] + e[u][v] == dis[v]) {
num[v] = num[v] + num[u];//如果相等,就把两者相加。
if(w[u] + weight[v] > w[v])
w[v] = w[u] + weight[v];
}
}
}
}
printf("%d %d", num[c2], w[c2]);
return ;
}

//感觉这个代码更好理解一点,就是使用迪杰斯特拉的变形,也就是在更新边的同时加了几个判断条件,通常来说迪杰斯特拉是遍历一遍就会结束了,但是这个加了一个for循环,那么就每次都会选出一个最短的边(到c1,即原点),选出后才会被标记为已经访问过。

PAT 1003 Emergency[图论]的更多相关文章

  1. PAT 1003. Emergency (25)

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

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

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

  3. PAT 1003 Emergency

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

  4. PAT 1003 Emergency 最短路

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

  5. 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 ...

  6. PAT 1003. Emergency 单源最短路

    思路:定义表示到达i的最短路径数量,表示到达i的最短径,表示最短路径到达i的最多人数,表示从i到j的距离, 表示i点的人数.每次从u去更新某个节点v的时候,考虑两种情况: 1.,说明到达v新的最短路径 ...

  7. 图论 - PAT甲级 1003 Emergency C++

    PAT甲级 1003 Emergency C++ As an emergency rescue team leader of a city, you are given a special map o ...

  8. PAT甲级1003. Emergency

    PAT甲级1003. Emergency 题意: 作为一个城市的紧急救援队长,你将得到一个你所在国家的特别地图.该地图显示了几条分散的城市,连接着一些道路.每个城市的救援队数量和任何一对城市之间的每条 ...

  9. PAT (Advanced Level) Practise 1003 Emergency(SPFA+DFS)

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

随机推荐

  1. Objective-C官方文档 协议

    版权声明:原创作品,谢绝转载!否则将追究法律责任. 在现实生活中,当处理某一情况的时候人们往往遵循严格的程序.执法人员他们在打官司的收集证据和询问的时候一定要遵守协议. 在面向对象的语言中,最重要的是 ...

  2. C# XtraGrid的行指示器(RowIndicator)行号以及图标设置

    以下是几种对Xtragrid的行指示器的几种操作方法,在实际场景当中,很多都需要用到,直接上效果图和源码 一.基本篇—设置表头行号 1.效果图 2.实现方法 需要对XtraGrid事件CustomDr ...

  3. sencha touch 百度地图扩展(2014-6-24)(废弃 仅参考)

    扩展代码如下: Ext.define('ux.BMap', { alternateClassName: 'bMap', extend: 'Ext.Container', xtype: 'bMap', ...

  4. [转]mii-tool与ethtool的用法详解

    1.mii-tool 配置网络设备协商方式的工具: 感谢原文作者!原文地址:http://blog.chinaunix.net/uid-20639775-id-154546.html 1.1 mii- ...

  5. 关于电信宽带wan口地址变成100.64网段的问题解决

    由于之前笔者一直在使用动态域名连接公司vpn.今天在连接vpn的时候总是失败,因动态域名及vpn配置都从未更改过. 于是首先排查动态域名,是否已更新为公司宽带对外的IP.这里笔者先通过nslookup ...

  6. Android - 安装应用(APP) 不显示图标

    装应用(APP) 不显示图标 本文地址:www.2cto.com 在启动的activity的AndroidManifest注册中,添加隐式启动的data: 删除应用图标的若干解决方案: 1.Andro ...

  7. Spring Cloud Eureka 高可用注册中心

    参考:<<spring cloud 微服务实战>> 在微服务架构这样的分布式环境中,各个组件需要进行高可用部署. Eureka Server 高可用实际上就是将自己作为服务向其 ...

  8. Scala实现乘法口诀

    object Test4 {  def main(args: Array[String]) {    for (i <- 1 to 9; j <- 1 to 9 if (j <= i ...

  9. 【转载】纵观RTX51

    对于使用RTX51的具体好处可以在实践中去体会,就象会用了C51,就不想再用汇编了.用了RTX51,说不定就感到再也离不开它了. 1.RTX51是实时多任务操作系统RTX51是一种实时操作系统既目前在 ...

  10. 无向连通图求割边(桥)hdu4738,hdu3849

    点击打开链接 题目链接:   hdu 4738 题目大意:   曹操有N个岛,这些岛用M座桥连接起来 每座桥有士兵把守(也可能没有) 周瑜想让这N个岛不连通,但只能炸掉一座桥 并且炸掉一座桥需要派出不 ...