1003. Emergency

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


题目大意
根据例子,给定5个城市(点),6条道路(边) ,以及每个城市救援队数,以及道路的权值,如下图所示

题目的意思就是一张图,各边都有权值而且个点也有权值,求出给定两点之间最短路的条数并输出此路径上的点权值和的最大值。就是求出最短路的条数,若有多条相同的最短路输出最大的那个点权值和。


解题思路

先用Dijstra算法算出最短路径,然后dfs寻找最优解

AC代码

#include<iostream>
#include <iomanip>
#include<vector>
#include<set>
#include<string>
#include<algorithm>
using namespace std;
const int INF = 100000000;
int team_num[1000];//记录每一节点的权值

int road[1000][1000];//图的邻接矩阵
int dis[1000];

bool isVisited[1000];//标记某一节点是否被访问过
//最短路径的数目
int path_num = 0;

//最大的医疗队的数目
int g_max_team_num = 0;
void initData(){
    int i, j;
    for ( i = 0; i < 1000; i++)
    {    
        isVisited[i] = false;
        dis[i] = INF;
        for ( j = 0; j < 1000; j++)
        {
            road[i][j] = INF;
        }

    }
}

//单源最短路径算法
void Dijstra(int n, int src, int  des){
    int i, j;
    for ( i = 0; i < n; i++)
    {
        dis[i] = road[src][i];
    }
    isVisited[src] = true;
    for ( i = 0; i < n-1; i++)  //最多循环n-1次就足够了,选n-1个最小值
    {
        int minDis = INF;
        int cur = 0;
        for ( j = 0; j < n; j++)
        {
            if (!isVisited[j] && dis[j] < minDis){
                minDis = dis[j];
                cur = j;
            }
        }
        if (minDis == INF){ //已经完成了连通路径的遍历。
            return;
        }
        isVisited[cur] = true;
//更新Dis数组的内容.
        for ( j = 0; j < n; j++)
        {
            if (road[cur][j] < INF&&dis[cur] + road[cur][j] < dis[j]){
                dis[j] = dis[cur] + road[cur][j];
            }
        }
    }

}
void dfs(int n, int src, int des, int curdis, int curteamNum){


    isVisited[src] = true;
    if (src == des){
        if (curdis == dis[des]){  //找到一条最短路径
            path_num++;
            if (curteamNum > g_max_team_num){
                g_max_team_num = curteamNum;
            }
        }
        return;
    }
    if (curdis > dis[des]){
        return;//当前的路径长度已经超过最短路径,就没有必要继续搜索了。
    }
    int i;
    for ( i = 0; i < n; i++)
    {
        if (!isVisited[i] && road[src][i] < INF){
            dfs(n,i,des,road[src][i]+curdis,curteamNum+team_num[i]);
            isVisited[i] = false;
        }
    }
}
int main()
{   
    int i,k,j,n,m,c1,c2,src,des;
    initData();
    cin >> n >> m >> src >> des;
    for ( i = 0; i < n; i++)
    {
        cin >> team_num[i];
    }
    int t1, t2, w;
    for ( i = 0; i < m; i++)
    {
        cin >> t1 >> t2 >> w;
        road[t1][t2] = w;
        road[t2][t1] = w;
    }
    Dijstra(n, src, des);
    for ( i = 0; i < n; i++)
    {
        isVisited[i] = false; 
              //重置各city的被访问状态
    }
    dis[src] = 0;
    dfs(n, src, des, 0, team_num[src]);

    cout << path_num << " " << g_max_team_num << endl;
    cin >> i;
}







  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
对于计算机专业的学生而言,参加各类比赛能够带来多方面的益处,具体包括但不限于以下几点: 技能提升: 参与比赛促使学生深入学习和掌握计算机领域的专业知识与技能,如编程语言、算法设计、软件工程、网络安全等。 比赛通常涉及实际问题的解决,有助于将理论知识应用于实践中,增强问题解决能力。 实践经验: 大多数比赛都要求参赛者设计并实现解决方案,这提供了宝贵的动手操作机会,有助于积累项目经验。 实践经验对于计算机专业的学生尤为重要,因为雇主往往更青睐有实际项目背景的候选人。 团队合作: 许多比赛鼓励团队协作,这有助于培养学生的团队精神、沟通技巧和领导能力。 团队合作还能促进学生之间的知识共享和思维碰撞,有助于形成更全面的解决方案。 职业发展: 获奖经历可以显著增强简历的吸引力,为求职或继续深造提供有力支持。 某些比赛可能直接与企业合作,提供实习、工作机会或奖学金,为学生的职业生涯打开更多门路。 网络拓展: 比赛是结识同行业人才的好机会,可以帮助学生建立行业联系,这对于未来的职业发展非常重要。 奖金与荣誉: 许多比赛提供奖金或奖品,这不仅能给予学生经济上的奖励,还能增强其成就感和自信心。 荣誉证书或奖状可以证明学生的成就,对个人品牌建设有积极作用。 创新与研究: 参加比赛可以激发学生的创新思维,推动科研项目的开展,有时甚至能促成学术论文的发表。 个人成长: 在准备和参加比赛的过程中,学生将面临压力与挑战,这有助于培养良好的心理素质和抗压能力。 自我挑战和克服困难的经历对个人成长有着深远的影响。 综上所述,参加计算机领域的比赛对于学生来说是一个全面发展的平台,不仅可以提升专业技能,还能增强团队协作、沟通、解决问题的能力,并为未来的职业生涯奠定坚实的基础。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值