PAT_甲级_1003_C语言

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, C​1​​ 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 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 C1 to C2.

Output Specification:

For each test case, print in one line two numbers: the number of different shortest paths between C​1 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>
#include <limits.h>
typedef struct Road{
    
    
	int c1 ;
	int c2 ;
	int length;
	int visited ;
}road;
static int min = INT_MAX ;
static int maxhps = 0 ;
static int nums = 0 ;
static road rs[50005] ;
static	int hs[50005] ;

int getBorder(int cs[] ,int m , int c1){
    
    
	int i = 0, n = 0;
	for(i = 0 ; i < m ; i ++ )
	{
    
    
		if(rs[i].c1==c1 || rs[i].c2== c1){
    
    
			cs[n] = i;
			n++;
		}
	}
	return n;
}

void dfs (int m, int c1 , int c2,int sum,int maxhelps){
    
    
	int cs[m] ;

	if(c1 == c2 ){
    
    
		if(min > sum ){
    
    
			maxhps = maxhelps+hs[c1];
			nums=1;
			min = sum;
			return ;
		}else if(min == sum ){
    
    
			if(maxhelps+hs[c1]>maxhps){
    
    
				maxhps = maxhelps+hs[c1];
			}
			nums++;
		}
		return ;
	}

	int i = 0  ;
	int ns = getBorder(cs , m ,c1);

	for(i = 0 ; i < ns ; i ++ ){
    
    
		if(rs[cs[i]].visited == 1) continue;
		rs[cs[i]].visited = 1 ;
		maxhelps +=hs[c1];
		sum+=rs[cs[i]].length;

		if(rs[cs[i]].c1==c2 ||  rs[cs[i]].c2==c2 ){
    
    
			dfs(m,c2,c2,sum,maxhelps);
		}else if(rs[cs[i]].c1==c1 && rs[cs[i]].c2!=c2 ){
    
    
			dfs(m,rs[cs[i]].c2,c2,sum,maxhelps);
		}else if(rs[cs[i]].c2==c1 && rs[cs[i]].c1!=c2 ){
    
    
			dfs(m,rs[cs[i]].c1,c2,sum,maxhelps);
		}
		maxhelps -=hs[c1];
		sum-=rs[cs[i]].length;
		rs[cs[i]].visited = 0 ;
	}

}

int main(){
    
    
	int n , m , c1 , c2 ;
	int i ;
	scanf("%d" , &n);
	scanf("%d" , &m);
	scanf("%d" , &c1);
	scanf("%d" , &c2);

	for( i = 0 ; i < n;i++){
    
    
		scanf("%d" , &hs[i]);
	}
	for(i = 0 ; i < m ; i++){
    
    
		scanf("%d" , &rs[i].c1);
		scanf("%d" , &rs[i].c2);
		scanf("%d" , &rs[i].length);
		rs[i].visited = 0;
	}

	if(c1==c2){
    
    
		printf("1 %d" , hs[c1]);
		return 0;
	}
	dfs(m,c1,c2,0,0);
	printf("%d" , nums);
	printf(" %d" , maxhps);
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_43511405/article/details/108903041
今日推荐