1003. Emergency (25)

来源:互联网 发布:我的世界mac版材质包 编辑:程序博客网 时间:2024/06/11 01:41
题目:

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 21 2 1 5 30 1 10 2 20 3 11 2 12 4 13 4 1
Sample Output
2 4
注意:
1、这道题目是Dijkstra最短路径算法的一个变型,一定要理解Dijkstra算法的处理过程,将其稍加改造便可应用在这里。
2、与原始Dijkstra算法不同的是,这里除了要计算最短路径之外,还需要计算该路径上救援队的总数,并在路径长度相同的情况下选择救援队总数最多的那条道路,因此比原始Dijkstra多了一个判断路径长度相等的过程。
3、这里要注意的是初始化team(从目前所在地到目的地所经过路径上可以召集的救援队的数目)的过程。
4、这里也可以在程序中加入pre数组来记录前向路径。

代码:
#include<iostream>using namespace std;#define MAX 500#define INF 65535struct CityGraph{int rescue[MAX];//number of rescue teams in each cityint G[MAX][MAX];//city graphint Cities, Roads;//number of cities and roads}cities;void Dijkstra(int src,int dest){int isvisited[MAX]={0};//status of each city weather its path has been visitedint len[MAX];//shortest path length from src city to other citiesint count[MAX]={0};//count is the number of shortest path from src to destint team[MAX]={0};//team is the maximum amount of rescue teams we can possibly gather//=============Initiate================for(int i=0;i<cities.Cities;++i){len[i]=cities.G[src][i]; //initiate all lengthif(len[i]<MAX)//pay attention to it{team[i]=cities.rescue[src]+cities.rescue[i];count[i]=1;}}isvisited[src]=1; //src city is visited and path length is 0len[src]=0;team[src]=cities.rescue[src];//=============Dijkstra================int k=src;//k is the new added citywhile(k!=dest){int min=INF;int v;for(int j=0;j<cities.Cities;++j)if(!isvisited[j] && len[j]<min){v=j;min=len[j];}//add v to the visitedisvisited[v]=1;k=v;for(int j=0;j<cities.Cities;++j){if(!isvisited[j] && len[v]+cities.G[v][j]<len[j]){len[j]=len[v]+cities.G[v][j];team[j]=team[v]+cities.rescue[j];count[j]=count[v];}else if (!isvisited[j] && len[v]+cities.G[v][j]==len[j]){team[j]=team[v]+cities.rescue[j]>team[j] ? team[v]+cities.rescue[j] : team[j];count[j]=count[v]+count[j];}}}cout<<count[dest]<< ' '<<team[dest]<<endl;}int main(){int dest, start;cin>>cities.Cities>>cities.Roads>>start>>dest;for(int i=0;i<cities.Cities;++i){cin>>cities.rescue[i]; //number of rescue teams in each cityfor(int j=0;j<cities.Cities;++j)cities.G[i][j]= (i==j)?0:INF;}for(int i=0;i<cities.Roads;++i){int m,n,l;cin>>m>>n>>l;cities.G[m][n]=cities.G[n][m]=l;}Dijkstra(start,dest);return 0;}


0 0