admin管理员组

文章数量:1577822

1003 Emergency (25 point(s))

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

 知识点:迪杰斯特拉算法。

此题在原有Dijkstra算法的基础上调整即可。

#include<iostream>
#include<vector> 
#define INF 1e9
using namespace std;
const int MAXN = 505;
struct Edge{
	int next;int w;
};
int N,M,from,to;
int emer[MAXN],dis[MAXN];	//救护队数量,距离 
int total[MAXN]={0},ways[MAXN]={0};	//到该位置总共的救护队数量,路径数量 
vector<Edge> G[MAXN];
bool mark[MAXN]={false}; 
int main(void){
	cin>>N>>M>>from>>to;	//N个城市,M条道路,出发城市,目标城市 
	fill(dis,dis+MAXN,INF);//和memset区分
	for(int i=0;i<N;i++) cin>>emer[i];	
	for(int i=0;i<M;i++){
		Edge E;
		int a,b,len;
		cin>>a>>b>>len;
		E.next=b; E.w=len;
		G[a].push_back(E);
		E.next=a;
		G[b].push_back(E);
	} 
	mark[from]=true;
	total[from]=emer[from];
	ways[from]=1;
	dis[from]=0;
	int newP=from;
	for(int k=1;k<N;k++){
		for(int j=0;j<G[newP].size();j++){
			int t = G[newP][j].next;
			int c = G[newP][j].w;
			//if(mark[t]==true) continue;
			if(dis[newP]+c<dis[t]){	//情况1,有更短的路径,更新距离、路径数、总救援队数量 
				dis[t]=dis[newP]+c;
				ways[t]=ways[newP];
				total[t]=total[newP]+emer[t];
			}
			else if(dis[newP]+c==dis[t]){	//情况2,有相同长度的路径,更新路径数、 
				ways[t]+=ways[newP]; 
				if(total[t]<total[newP]+emer[t])
					total[t]=total[newP]+emer[t];//当新路径上有更多的救援队的时候更新 
			}
		}
		
		int min = INF;
		for(int j=0;j<N;j++){
			if(mark[j]==true) continue;
			if(dis[j]==INF) continue;
			if(dis[j]<min){
				min = dis[j];
				newP = j;
			}
		}
		mark[newP]=true;
	}
	cout<<ways[to]<<" "<<total[to]<<endl;
	return 0;
} 

 

本文标签: 未完EmergencyPoint