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

     使用变形的dijkstra算法  并且随时更新最短路径的数目以及能够搜集到的队伍数目。

     1 #include <iostream>
     2 #include <vector>
     3 #include <cstdlib>
     4 using namespace std;
     5 
     6 #define MaxDistance 10000
     7 vector< vector <int>> GMatrix;
     8 vector<int> dist;
     9 vector<int> teams;
    10 
    11 int N,M,start,en;
    12 
    13 void dijkstra()
    14 {
    15     vector<int> flag(N, 0);
    16     vector<int> pathcount(N, 0);
    17     vector<int> amount(N, 0);
    18     for (size_t i = 0; i < N; i++)//initial the dist
    19     {
    20         if (GMatrix[start][i] != -1)
    21             dist.push_back(GMatrix[start][i]);
    22     }
    23     amount[start] = teams[start];
    24     pathcount[start] = 1;
    25     
    26     while(1) 
    27     {
    28         int min = MaxDistance;
    29         int k;
    30         for (size_t j = 0; j < N; j++)
    31         {
    32             if ((flag[j] == 0) && (dist[j] < min))
    33             {
    34                 min = dist[j];
    35                 k = j;
    36             }
    37         }
    38         if (k == en|| min==MaxDistance) break; //can not find the mini or find the end node, then break the iterative while(1) 
    39         flag[k] = 1;
    40         for (size_t j = 0; j < N; j++)
    41         {
    42             int temp = dist[k] + GMatrix[k][j];
    43             if ((flag[j] == 0) && (temp < dist[j]))
    44             {
    45                 dist[j] = temp;
    46                 amount[j] = amount[k] + teams[j]; //update the teams you can gather
    47                 pathcount[j] = pathcount[k];// update the shortest road sums
    48             }
    49             else if ((flag[j] == 0) && (temp == dist[j]))
    50             {
    51                 pathcount[j] += pathcount[k]; //update 
    52                 if (amount[k] + teams[j]>amount[j]) 
    53                     amount[j] = amount[k] + teams[j]; //update
    54             }
    55         }
    56     }
    57     //for (size_t i = 0; i < N; i++)//output the dist for check
    58     //{
    59     //    cout << dist[i] << " ";
    60     //}
    61     //cout << endl;
    62     cout << pathcount[en] << " " << amount[en] << endl;
    63 
    64 }
    65 int main()
    66 {
    67     cin >> N >> M >> start >> en;
    68     for (size_t i = 0; i < N; i++)//input the teams
    69     {
    70         int t; cin >> t;
    71         teams.push_back(t);
    72     }
    73     for (size_t i = 0; i < N; i++)//initial the GMtraix
    74     {
    75         vector<int> arr(N, MaxDistance);
    76         GMatrix.push_back(arr);
    77         GMatrix[i][i] = 0;//for the same start and end, the dist=0; 
    78     }
    79     for (size_t i = 0; i < M; i++)//input the road to the graph
    80     {
    81         int left, right, road;
    82         cin >> left >> right >> road;
    83         GMatrix[left][right] = road; GMatrix[right][left] = road;
    84     }
    85     dijkstra();
    86 
    87 
    88     return 0;
    89 }
  • 相关阅读:
    CloudFlare Workers 反代任意网站和挂载单页代码
    排查Linux服务器是否有被入侵方法总结
    修复Windows time时间服务无法自动启动的问题
    如何删除windows服务
    linux之NetHogs与nload流量监控
    常用IPV4 DNS服务器与常用IPV6 DNS服务器
    Win10系统如何删除网络及修改网络名称
    window修改远程桌面RDP方法
    单击鼠标右键菜单显示怎么在左边?
    传统IDC机房部署网站
  • 原文地址:https://www.cnblogs.com/willwu/p/5329486.html
Copyright © 2011-2022 走看看