echobfy

博客园 首页 新随笔 联系 订阅 管理

分析:

  考察最短路,可以使用Dijkstra算法,关键在于有相等路径时的判断。

  特别注意:题目要求的是the number of different shortest paths 和 the maximum amount of rescue teams you can possibly gather。

  1 #include <stdio.h>
  2 #include <iostream>
  3 #include <string>
  4 #include <cctype>
  5 //#include <map>
  6 
  7 using namespace std;
  8 
  9 const int Max_required = 505;
 10 const int Max_int = 0x7fffffff;
 11 
 12 int map[Max_required][Max_required];
 13 int number_of_team_in_city[Max_required];
 14 
 15 struct CITY
 16 {
 17     int dist;
 18     bool visit;
 19     int call;
 20     int number;
 21 }city[Max_required];
 22 
 23 void Dijkstra(int start, int end, int n)
 24 {
 25     for (int i = 0; i < n; i++)
 26     {
 27         city[i].dist = Max_int;
 28         city[i].visit = 0;
 29         city[i].number = 0;
 30         city[i].call = 0;
 31     }
 32     city[start].dist = 0;
 33     city[start].call = number_of_team_in_city[start];
 34     city[start].number = 1;
 35 
 36     //work......
 37     for (int i = 0; i < n; i++)
 38     {
 39         int Min = Max_int, pos = -1;
 40         for (int j = 0; j < n; j++)
 41         {
 42             if (city[j].visit == 0 && city[j].dist < Min)
 43             {
 44                 Min = city[j].dist;
 45                 pos = j;
 46             }
 47         }
 48         if (pos == -1) break;
 49         city[pos].visit = 1;
 50 
 51         for (int j = 0; j < n; j++)
 52         {
 53             if (city[j].visit == 0 && map[pos][j] != Max_int) 
 54             {
 55                 if (city[j].dist > city[pos].dist + map[pos][j])
 56                 {
 57                     city[j].dist = city[pos].dist + map[pos][j];
 58                     //-------------------------------
 59                     city[j].number = city[pos].number;    //注意!
 60                     //---------------------------------
 61                     city[j].call = city[pos].call + number_of_team_in_city[j];
 62                 }
 63                 else if (city[j].dist == city[pos].dist + map[pos][j])
 64                 {
 65                     city[j].number += city[pos].number;
 66                     if (city[j].call < city[pos].call + number_of_team_in_city[j])
 67                         city[j].call = city[pos].call + number_of_team_in_city[j];
 68                     //city[j].call += city[pos].call;
 69                 }
 70             }
 71         }
 72     }
 73 
 74     printf("%d %d\n", city[end].number, city[end].call);
 75     //printf("%d\n", city[end].dist);
 76 }
 77 
 78 int main()
 79 {
 80     int n, m, start, end;
 81 
 82     while (scanf("%d%d%d%d", &n, &m, &start, &end) != EOF)
 83     {
 84         for (int i = 0; i < n; i++)
 85         {
 86             scanf("%d", &number_of_team_in_city[i]);
 87             for (int j = 0; j < n; j++)
 88                 map[i][j] = Max_int;
 89         }
 90 
 91         while (m--)
 92         {
 93             int from, to, road_len;
 94             scanf("%d%d%d", &from, &to, &road_len);
 95 
 96             map[from][to] = road_len;
 97             map[to][from] = road_len;
 98         }
 99 
100         Dijkstra(start, end, n);
101     }
102     return 0;
103 }
View Code

 

posted on 2014-02-12 14:38  小白哥哥啊  阅读(808)  评论(0编辑  收藏  举报