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

    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
    

    思路

    题意是,让你求C1->C2 最短路径的个数,然后输出你能调动救援队的最大值。
    网上很多人是基于dijkstra做出来的,我只写了个简单的搜索。
    用数组记录城市之间的道路长度。注意有个坑,可能出现自己救自己的情况(即 C1=C2)。

    #include <stdio.h>
    #include <string>
    #include <stdlib.h>
    #include <iostream>
    #include <vector>
    #include <string.h>
    #include <algorithm>
    #include <limits.h>
    #include <cmath>
    #include <map>
    using namespace std;
    
    int m[505][505];
    int n, M, c1, c2;
    int num[505];
    int vis[505];
    int road_num;
    int max_n = - 1;
    int min_dis = INT_MAX;
    
    void dfs(int index, int sum, int dis){
    	if(dis > min_dis)	return;
    	if(index == c2){
    		if(dis < min_dis){
    			min_dis = dis;
    			road_num = 1;
    			max_n = sum;
    		}
    		else if(dis == min_dis){
    			road_num++;
    			max_n = max(sum, max_n);
    		}
    		return;
    	}
    	for(int i = 0; i < n; i++){
    		if(m[index][i] && !vis[i]){
    			vis[i] = 1;
    			dfs(i, sum + num[i], dis + m[index][i]);
    			vis[i] = 0;
    		}
    	}
    }
    
    int main() {
    	cin >> n >> M >> c1 >> c2;
    	for(int i = 0; i < n; i++){
    		cin >> num[i];
    	}
    	for(int i = 0; i < M; i++){
    		int s = 0, e = 0, l = 0;
    		cin >> s >> e >> l;
    		m[s][e] = l;
    		m[e][s] = l;
    	}
    	if(c1 != c2)	vis[c1] = 1;
    	dfs(c1, num[c1], 0);
    	
    	cout << road_num << " " << max_n;
    	return 0;
    }
    
  • 相关阅读:
    【转】ps墨镜哥教你怎么变成合影帝
    【转】MySQL修改密码方法总结
    【技术贴】五分钟解决打开软件提示windows正在设置Microsoft Office Profes
    【技术贴】IIS相关问题解决笔记。
    【技术贴】五分钟解决打开软件提示windows正在设置Microsoft Office Profes
    【转】小生我怕怕工具包[2010.06.17](转自52破解论坛)
    【技术贴】Realtek HD声卡下QQ语音话筒没声音解决办法
    【技术贴】所有好友的QQ空间都打不开进不去的超简单解决办法!
    ASP.NET IIS 注册工具
    关于服务器如何设置动易系统数据库路径的方法
  • 原文地址:https://www.cnblogs.com/woxiaosade/p/12318953.html
Copyright © 2011-2022 走看看