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, C​1​​ and C​2​​ - 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 c​1​​, c​2​​ 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 C​1​​ to C​2​​.

Output Specification:

For each test case, print in one line two numbers: the number of different shortest paths between C​1​​ and C​2​​, 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个城市,然后每个城市分别有1 2 1 5 3的救援队,把城市看成点,把城市救援队的数量看成点的权重,给了几条路(把路看成边),路的距离其实看成 边的权重,然后src_city,goal_city。然后求最短路径的条数,以及最短路径中点权重最大的值。

  然后是数据结构的准备,5个城市,就需要dis[5],其中dis[src_city]初始化为0,其余都为INF,这个数组的意思是从起点到其他各城市的最小距离。

然后就是算法了:

      第一步:初始化dis[src_city]=0,num[src_city]=1,w[src_city]=weight[src_sity]因为要求最小生成树的条数,所以要多次循环;

      第二步:查找从起点src_city可以到达的点n,并更新dis[n],并保存所有可达点中的最小边值min,把这个最小边值 点标注为visit=true,意为把这个点加入最小树里面;

      第三步:就从这个标注点开始,循环查找它的相邻边,如果遇到它到另一个点n2的边权+min<dis[n2],则更新dis[n2],同时w[n2]=w[n]+weght[n2],同时num[n2]=num[n];如果遇到它到另一个点n2的边权+min=dis[n2],则不更新dis[n2],同时num[n2]=num[n],但比较w[n2]与w[n]+weght[n2]的大小,如果大于的话,更新w[n2];

接下去是代码实现:

  1. #include<stdio.h>
  2. #include<iostream>
  3. using namespace std;
  4. const int INF=100000000;
  5. int city_connection[500][500];
  6. int dis[500],num[500];
  7. int w[500],weight[500];
  8. bool visit[500];
  9. int main(){
  10.     int Ncity,Nload,src_city,goal_city;
  11.     cin>>Ncity>>Nload>>src_city>>goal_city;
  12.     for(int i=0;i<Ncity;i++)
  13.     {
  14.         cin>>weight[i];
  15.     }
  16.     for(int i=0;i<500;i++)
  17.     {
  18.         for(int j=0;j<500;j++)
  19.         {
  20.         city_connection[i][j]=INF;
  21.         }
  22.         dis[i]=INF;
  23.     }
  24.     int a,b,c;
  25.     for(int i=0;i<Nload;i++)
  26.     {
  27.         cin>>a>>b>>c;
  28.         city_connection[a][b]=city_connection[b][a]=c;
  29.     }
  30.     dis[src_city]=0;
  31.     w[src_city]=weight[src_city];
  32.     num[src_city]=1;
  33.     for(int i=0;i<Ncity;i++)
  34.     {
  35.         int u=-1,minn=INF;
  36.         for(int j=0;j<Ncity;j++)
  37.         {
  38.             if(visit[j]==false&&dis[j]<minn)
  39.             {
  40.                 u=j;
  41.                 minn=dis[j];
  42.             }
  43.         }
  44.         if(u==-1) break;
  45.         visit[u]=true;
  46.         for(int m=0;m<Ncity;m++)
  47.         {
  48.             if(visit[m]==false && city_connection[u][m]!=INF)
  49.             {
  50.                 if(dis[m]>dis[u]+city_connection[u][m])
  51.                 {
  52.                     dis[m]=dis[u]+city_connection[u][m];
  53.                     num[m]=num[u];
  54.                     w[m]=w[u]+weight[m];
  55.                 }else if(dis[m]==dis[u]+city_connection[u][m])
  56.                 {
  57.                     num[m]+=num[u];
  58.                     if(w[m]<w[u]+weight[m])
  59.                         w[m]=w[u]+weight[m];
  60.                 }
  61.             }
  62.         }
  63.    }
  64.     printf("%d %d",num[goal_city],w[goal_city]);
  65.     return 0;
  66. }

     

猜你喜欢

转载自blog.csdn.net/qq_38043117/article/details/89451457
今日推荐