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

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

结尾无空行

#include<iostream>
#include<string>
using namespace std;
#include<cmath>
#include<algorithm>
#include<ctype.h>
#pragma warning (disable:4996)
using namespace std;
int rodes[510][510];
#define inf 99999999
int city_people[100000];
bool books[100000];
int dis[10000];
int shortest[10000];
int peoples[10000];
int main() {
	int N, M, C1, C2;
	cin >> N >> M >> C1 >> C2;
	for (int cnt = 0; cnt < N; cnt++) {
		cin >> city_people[cnt];
	}
	fill(rodes[0], rodes[0] + 510 * 510, inf);
	for (int cnt = 0; cnt < M; cnt++) {
		int c1, c2, L;
		cin >> c1 >> c2 >> L;
		rodes[c1][c2] = rodes[c2][c1] = L;
	}
	fill(dis, dis + 1000, inf);
	dis[C1] = 0;
	peoples[C1] = city_people[C1];
	shortest[C1] = 1;
	for (int cnt = 0; cnt < N; cnt++) {
		int i = -1;
		int min = inf;
		for (int cnt1 = 0; cnt1 < N; cnt1++) {
			if (min > dis[cnt1]&& books[cnt1]==false) {
				i = cnt1;
				min = dis[cnt1];
			}
		}
		books[i] = true;
		if (i == -1)break;
		for (int cnt1 = 0; cnt1 < N; cnt1++) {
			if (rodes[i][cnt1] != inf && books[cnt1] == false) {
				if (dis[i] + rodes[i][cnt1] < dis[cnt1]) {
					peoples[cnt1] = peoples[i] + city_people[cnt1];
					shortest[cnt1] = shortest[i];
					dis[cnt1] = dis[i] + rodes[i][cnt1];
				}
				else if (dis[i] + rodes[i][cnt1] == dis[cnt1] ) {
					if (peoples[cnt1] < peoples[i] + city_people[cnt1])
						peoples[cnt1] = peoples[i] + city_people[cnt1];
					shortest[cnt1] = shortest[cnt1]+ shortest[i];
				}
			}
		}

	}
	cout << shortest[C2] << " " << peoples[C2];
	return 0;
}

踩过的坑:人数和最短路径的条数都是在找到有更短路径时进行更新的,而且因为它的信息都在点上,所以前一个点的全部信息都赋值到这个点上,如果相同,则是将这个点原来的值和前一个节点相加,还有注意下,min的值要变,不然你可能会出现1,2正确,但是其他全部错误的情况,实际上就是寻找最小值的一个过程.

把代码重写了一遍,发现用fill的时候要注意后面填充的应该是数组的大小,不要想当然觉得它会给你个正方形出来

#include <iostream>
using  namespace std;
#include<string>
#include<vector>
#include<algorithm>
#pragma warning (disable:4996)
int city[1000000];
int rodes[550][550];
int dis[100000];
int most[100000];
int path[100000];
#define inf 99999999
bool book[100000];
int main() {
	int N, M, C1, C2;
	cin >> N >> M >> C1 >> C2;
	for (int cnt = 0; cnt < N; cnt++)
		cin >> city[cnt];
	fill(rodes[0], rodes[0] + 550 * 550,inf);
	for (int cnt = 0; cnt < M; cnt++) {
		int c1, c2, L;
		cin >> c1 >> c2 >> L;
		rodes[c1][c2] = L;
		rodes[c2][c1] = L;
	}
	path[C1] = 1, most[C1] = city[C1];
	fill(dis, dis + 550, inf);
	dis[C1] = 0;
	for (int cnt = 0; cnt < N; cnt++) {
		int i=-1, min = inf;
		for (int cnt1 = 0; cnt1 < N; cnt1++) {
			if (dis[cnt1] < min && !book[cnt1])
				min = dis[cnt1], i = cnt1;
		}
		if (i == -1)break;
		book[i] = true;
		for (int cnt1 = 0; cnt1 < N; cnt1++) {
			if (dis[cnt1] > dis[i] + rodes[i][cnt1] && !book[cnt1]) {
				most[cnt1] = city[cnt1] + most[i];
				dis[cnt1] = dis[i] + rodes[i][cnt1];
				path[cnt1] = path[i];
			}
			else if(dis[cnt1]==dis[i]+rodes[i][cnt1] &&!book[cnt1])
			{
				if (most[cnt1] < city[cnt1] + most[i])
					most[cnt1] = city[cnt1] + most[i];
				path[cnt1] += path[i];
			}

		}

	}
	cout << path[C2] << " " << most[C2];
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

cjz-lxg

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值