1003 Emergency (Dijkstra)

题目描述

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.

输入格式

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

输出格式

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 2
1 2 1 5 3
0 1 1
0 2 2
0 3 1
1 2 1
2 4 1
3 4 1

输出样例

2 4

题目大意

无向图中,给出每个点的点权,每个边的边权(即两点之间距离),要使起点到终点距离最短,求最短路径数,并求出最大点权之和。

思路

单源最短路径问题。采用Dijkstra算法,该算法的思想为,设置数组d,代表源点与各顶点的最短距离。经过n(点数)次遍历,每次遍历找到当前与源点s距离最短的顶点u,并以该顶点u为中介点,找到其经过并未访问的结点v,判断其能否使d[v]更优(即length[s->u]+d[u]<d[v])。本题目新增点权wm、最短路径数目num两个量,和d的处理方法类似。即:

  • length[s->u]+d[u]<d[v]时,num[v]更新为num[u],wm[v]更新为wm[u]+w[v]。
  • length[s->u]+d[u]=d[v]时,num[v]更新为num[u] + num[v]。而只有wm[u]+w[v]>wm[v]时(以u为中介使wm更优),才更新wm[v]。

该题一开始没通过,因为我忘了这是个无向图,输入数据时需要考虑到。

AC代码

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

struct node{
	int v, dis; //目标顶点和边权 
};
const int maxv = 510, INF = 1000000000;
vector<node> Adj[maxv];
int d[maxv]; //源点到各顶点最短路径长度
int num[maxv]; //最短路径条数
int wm[maxv]; //最多救援队数
int w[maxv]; //各点救援队数 
bool vis[maxv] = {false}; 
int n, m; //城市数 路径数 
int s, e; //起点和终点

void Dijkstra(int s){
	fill(d, d + maxv, INF);
	fill(num, num + maxv, 0);
	fill(wm, wm + maxv, 0);
	d[s] = 0;
	num[s] = 1;
	wm[s] = w[s];
	for(int i = 0; i < n; i++){
		int u = -1, min = INF;
		for(int j = 0; j < n; j++){ //找到最小d[u] 
			if(vis[j] == false && d[j] < min){
				u = j;
				min = d[u];
			} 
		}
		if(u == -1) return;
		vis[u] = true;
		for(int j = 0; j < Adj[u].size(); j++){
			int v = Adj[u][j].v; //u能到达的顶点
			if(vis[v] == false){
				if(d[v] > d[u] + Adj[u][j].dis){
					//以u为中介点使d[v]更优 
					d[v] = d[u] + Adj[u][j].dis;
					num[v] = num[u];
					wm[v] = wm[u] + w[v];
				}
				else if(d[v] == d[u] + Adj[u][j].dis){
					num[v] += num[u];
					if(wm[v] < wm[u] + w[v])
						wm[v] = wm[u] + w[v];
				}
			}
		}
	}
}

int main(){
	cin>>n>>m>>s>>e;
	for(int i = 0; i < n; i++)
		cin>>w[i];
	int c1, c2, l;
	node tmp;
	for(int i = 0; i < m; i++){
		cin>>c1>>c2>>l;
		tmp.v = c2;
		tmp.dis = l;
		Adj[c1].push_back(tmp);
		tmp.v = c1;
		Adj[c2].push_back(tmp);
	}
	Dijkstra(s);
	cout<<num[e]<<" "<<wm[e]; 
	return 0;
}

在这里插入图片描述

发布了9 篇原创文章 · 获赞 0 · 访问量 80

猜你喜欢

转载自blog.csdn.net/qq_43072010/article/details/104793548
今日推荐