zoukankan      html  css  js  c++  java
  • 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

    这题目标是求图的最短路的路径数,并在最短路中找一个经过最多的援救队伍数。比较简单的图搜索问题,主要注意点时在搜索过程中减枝。
    #include<iostream>
    #include<stdio.h>
    #include<cstring>
    #include<algorithm>
    #include<cmath>
    #include<stack>
    #include<queue>
    using namespace std;
    const int max_city = 502;
    int visit[max_city], map[max_city][max_city];
    int re_teams[max_city];
    int N,M,C1,C2;
    int flag =0;
    int num=max_city, max_r;
    void dfs(int des, int roads_length, int teams){//des 当前遍历的位置,roads是记录路的长度,teams是路上的援救队数 
    	if(des == C2){
    		if(roads_length == num){ //road length
    			flag++;
    			if(max_r<teams){
    				max_r = teams;
    			}
    		}
    		else if(roads_length < num){
    			flag = 1;
    			max_r = teams;
    			num = roads_length;
    //			printf("%d %d
    ",max_r,num);
    		}
    		
    	}
    	if(roads_length > num){
    		return ;
    	}
    	for(int i=0;i<N;i++){
    		if(visit[i]!=1 && map[i][des]!= max_city){
    			visit[i] = 1;
    			dfs(i,roads_length+map[i][des],teams+re_teams[i]);
    			visit[i] = 0;	
    		}			
    	}
    }
    int main(){
    	scanf("%d%d%d%d",&N,&M,&C1,&C2);
    	int i,j,k;
    	for(i=0;i<N;i++){
    		visit[i]=0;
    		for(j=0;j<N;j++){
    			map[i][j]=max_city;
    			}
    		}
    	for(i=0;i<N;i++){
    		scanf("%d",&re_teams[i]);
    	}
    	int s,d,t;
    	for(i=0;i<M;i++){
    		scanf("%d%d%d",&s,&d,&t);
    		map[s][d] = map[d][s]= t;
    	}	
    	dfs(C1,0,re_teams[C1]);
    	printf("%d %d
    ",flag,max_r);
    	return 0;
    } 
    

      

  • 相关阅读:
    云计算、大数据和人工智能简单概述
    Linux 文件管理命令语法、参数、实例全汇总(一)
    C#之继承
    类和结构(二)
    类和结构(一)
    C#基础语法(二)
    C#基础语法(一)
    dotnet体系结构
    九卷读书:刘润商学院学习笔记1
    Linux内存管理
  • 原文地址:https://www.cnblogs.com/grglym/p/7634182.html
Copyright © 2011-2022 走看看