zoukankan      html  css  js  c++  java
  • 1003 Emergency (25分) 求最短路径的数量

    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 N1), 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
    
    题意:n个城市m条路,每个城市有救援小组,联通城市之间的距离已知。
    给定起点和终点,求从起点到终点的最短路径条数以及最短路径上的救援小组数目之和最大的那个

    题解:和求Disjktra多重权值一样,
    在更新中间节点路径更短的时候,顺带更新救援小组的数量
    当最短路径相同的时候,更新最短路径条数

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #define MAX 1000000
    using namespace std;
    int a[1005][1005];
    int num[1005];//每个村庄有多少个救援队
    int dis[1005];//起点S到村庄j的最短距离
    int val[1005];//起点S到村庄j可以聚集救援队的数量
    int vis[1005];
    int cnt[1005];//起点S到村庄j的最短路径有多少条
    void dijkstra(int start, int n)
    {
        int i, j, k, min;
        for (i = 0; i < n; i++)//(初始化)存放起点到其余顶点的距离
            dis[i] = a[start][i];
    
        dis[start] = 0;
        val[start] = num[start];
        cnt[start] = 1;
    
        for (i = 0; i < n; i++)
        {
            min = MAX;
            k = -1;
            for (j = 0; j < n; j++) //求出初始起点s直接到j点距离最短的点的下标值
            {
                if (vis[j]==0 && min > dis[j])
                {
                    min = dis[j];
                    k = j;
                }
            }
            vis[k] = 1;
            if (k == -1)
                return;
            for (j = 1; j <= n; j++)
            {
                if (dis[j] > dis[k] + a[k][j])//若找到其他途径比从1号顶点直接到目的顶点的距离短,则替换掉
                {
                    cnt[j]=cnt[k];
                    dis[j] = dis[k] + a[k][j];
                    val[j]=num[j]+val[k];
                }
    
                else if (dis[j] == dis[k] + a[k][j])//如果距离相同
                {
                    cnt[j]=cnt[j]+cnt[k];
                    val[j] = max(val[j],val[k] + num[j]);//更新救援队数量
                }
            }
        }
    }
    
    int main()
    {
        int n, m;
        int i;
        int s, e;
        cin>>n>>m>>s>>e;
        for(int i=0;i<n;i++)
            cin>>num[i];
        int t1,t2,t3;
        memset(vis, 0, sizeof(vis));
        memset(cnt, 0, sizeof(cnt));
        memset(cnt, 0, sizeof(val));
        memset(a, MAX, sizeof(a));//初始化所有点的距离/花费为无穷大
        for (i = 0; i < m; i++)
        {
            scanf("%d%d%d", &t1, &t2, &t3);
            if (a[t1][t2] > t3)//去重
                a[t1][t2] = a[t2][t1] = t3;
        }
        dijkstra(s, n);
        printf("%d %d
    ", cnt[e], val[e]);
        return 0;
    }
     
  • 相关阅读:
    聊聊“装箱”在CLR内部的实现
    Jenkins多环境持续集成架构实践
    .NET Core 学习资料精选:进阶
    .NET Core 学习资料精选:入门
    Docker 常用命令(.NET Core示例)
    Vistual Studio 安装、Sql Server 安装
    .NET项目迁移到.NET Core操作指南
    站点部署,IIS配置优化指南
    .NET Core开源:IIS集中化Web管理工具
    jenkins:一键回滚站点集群
  • 原文地址:https://www.cnblogs.com/-citywall123/p/12088433.html
Copyright © 2011-2022 走看看