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

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

AC参考代码:
#include <stdio.h>
#include <stdlib.h>

const int MAX = 501;	//The max number of cities
const int MAX_INT = 999999999;
int path[MAX][MAX],isVisited[MAX],weight[MAX];	//The  variable about Cities
int minDist,maxWeight,cnt,cities;	//The variable of recording the boundary value

void initGraph(int cities)
{
	for(int i=0;i<cities;i++){
		isVisited[i] = 0;	//The value of 0 means not visited 
		for(int j=0;j<cities;j++){
			path[i][j] = MAX_INT;	//The initial length of path is infinite 
		} 
	}
}

void DFS(int start,const int dest,int dist,int weit)
{
	if(start == dest){	//The boundary value
		if(dist < minDist){	//There is new minist path 
			minDist = dist;
			cnt = 1; 	//Init the new recording value
			maxWeight = weit;
		}
		else if(dist == minDist){	//More minist path
			cnt++;
			if(maxWeight < weit){
				maxWeight = weit;
			}
		}
		return;
	}
	if(dist > minDist)	return;	//pruning,optimise the algorithm
	
	for(int i=0;i<cities;i++){
		if(isVisited[i]==0 && path[start][i]!=MAX_INT){		//The city is not visited and there is a path bewteen two cites
			isVisited[i] = 1;
			DFS(i,dest,dist+path[start][i],weit+weight[i]);
			isVisited[i] = 0;	//When you back,this node could be one of another path,so remark it.
		}
	}
}

int main()
{
	int N,M,c1,c2,x,y,temp;
	scanf("%d %d %d %d",&N,&M,&c1,&c2);
	cities = N; minDist = MAX_INT,cnt = 0;
	initGraph(cities);
	
	for(int i=0;i<N;i++){
		scanf("%d",&weight[i]);
	}
	
	for(int i=0;i<M;i++){
		scanf("%d %d %d",&x,&y,&temp);
		path[x][y] =path[y][x] = temp;
	}	//Finish data input 
	
	DFS(c1,c2,0,weight[c1]);
	
	printf("%d %d",cnt,maxWeight);
	return 0;
}


此题主要考的是DFS深度优先遍历+递归的考点,涉及的变量相对多点比较综合
注意点:
1:在做DFS的时候临界点要注意:
    1、找到最短路径,更新所有信息
    2、找到同样长度的最短路径,更新cnt的计数值,同时对weight作判断
    3、其他直接return 就ok
2:if(dist > minDist) return; 实现搜索的剪枝,优化代码
3:还有初始化图的时候 path[x][y] =path[y][x] 要完全初始化
4:DFS(i,dest,dist+path[start][i],weit+weight[i]); isVisited[i] = 0;
    DFS完子路径的时候回退的时候需要将i结点重置为未访问,因为在图中深度优先遍历的时候下次可能再次用到。

其他没什么注意的,水水过了

你可能感兴趣的:(C++,c)