zoukankan      html  css  js  c++  java
  • PAT甲级——A1003Emergency

    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 (≤) - 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

    其实就是简单地Dijkstra算法,只不过要加上权重
     1 #include <iostream>
     2 #include <vector>
     3 
     4 using namespace std;
     5 
     6 #define INF 99999
     7 int  main()
     8 {
     9     int N, M, C1, C2;
    10     cin >> N >> M >> C1 >> C2;
    11     vector<vector<int>>dis(N, vector<int>(N, INF));//-1表示路不通
    12     vector<int>v(N, 0);
    13     for (int i = 0; i < N; ++i)
    14         cin >> v[i];
    15     for (int i = 0; i < M; ++i)
    16     {
    17         int a, b, c;
    18         cin >> a >> b >> c;
    19         dis[a][b] = dis[b][a] = c;
    20     }
    21     vector<int>index(N, 1);//用来标记是否已经遍历过的点
    22     vector<int>w(N, 0);//用来更新权重
    23     vector<int>num(N, 0);//用来保存最优路径数量
    24     vector<int>D(N, INF);
    25     D[C1] = 0; // Start开始出发
    26     w[C1] = v[C1];
    27     num[C1] = 1;//最开始初始化有一条
    28 
    29     for (int i = 0; i < N; ++i)
    30     {
    31         //先找出发点去往下一个最近的点
    32         int p = -1;
    33         int minD = INF;
    34         for (int j = 0; j < N; ++j)
    35         {
    36             if (index[j] && minD > D[j])
    37             {
    38                 p = j;
    39                 minD = D[j];
    40             }
    41         }
    42         if (p == -1)
    43             break;//遍历完毕
    44         index[p] = false;//已经遍历过了
    45         //那么就遍历点p能去往的点
    46         for (int j = 0; j < N; ++j)
    47         {
    48             //更新点Start->j的距离
    49             if (index[j] && D[j] > D[p] + dis[p][j])
    50             {
    51                 D[j] = D[p] + dis[p][j];
    52                 num[j] = num[p];
    53                 w[j] = w[p] + v[j];
    54             }
    55             else if (index[j] && D[j] == D[p] + dis[p][j])//出现相同路径
    56             {
    57                 num[j] += num[p];//叠加最优路径数量
    58                 w[j] = w[j] > (w[p] + v[j]) ? w[j] : (w[p] + v[j]);//新路的权重是否更大?
    59             }
    60         }
    61     }
    62     cout << num[C2] << " " << w[C2] << endl;
    63 
    64     return 0;
    65 }


  • 相关阅读:
    Vue中改变对象的注意事项
    Object.assign简单总结
    Base64编码
    vue中prop传值时加不加v-bind(冒号:)
    内联元素的padding和margin
    flex自适应宽度显示省略号
    Http和Https
    JVisualVM 模拟一次内存泄漏场景分析
    Lucene
    布隆算法原理
  • 原文地址:https://www.cnblogs.com/zzw1024/p/11167248.html
Copyright © 2011-2022 走看看