首页 技术 正文
技术 2022年11月15日
0 收藏 359 点赞 4,387 浏览 3561 个字
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 501int 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,即原点),选出后才会被标记为已经访问过。

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,105
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,582
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,429
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,200
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,836
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,919