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

分析:此題需要求出最短路徑的條數和救援隊的族大數目,先用鄰接矩陣的表示法構造出圖,然後用單源最短路徑算法求出最短路徑,在用深度搜索算法找出最短路徑的數目。

code:

#include<iostream>
#include<cstring>
using namespace std;

const int N = 1000;
const int INF = 100000000;

//圖的鄰接矩陣
int road[N][N];

//標記某一節點是否被訪問過
bool isVisited[N];

//記錄每一節點的權值
int team_num[N];

int dis[N];

//最短路徑的數目
int path_num = 0;

//最大的醫療隊的數目
int g_max_team_num = 0;

void initData()
{
  int i, j;
  for (i = 0; i<N; i++)
  {
    isVisited[i] = false;
    team_num[i] = 0;
    dis[i] = INF;//初始化爲無窮大.
    for (j = 0; j<N; j++)
    {
      if (j != i)
      {
        road[i][j] = INF;
        road[j][i] = INF;
      }
      //當i==j的時候,一定要賦值爲0,否則當起點src和終點des相同的時候,test case是過不了的。
      else
        road[i][j] = 0;
    }
  }
}

//單源最短路徑算法。
void Dijstra(int n, int src, int des)
{
  int i, j;
  for (i = 0; i<n; i++)
    dis[i] = road[src][i];
  isVisited[src] = true;

  for (i = 0; i<n - 1; i++)//最多循環n-1次就足夠了,選n-1個最小值。
  {
    int minDis = INF;
    int cur = 0;
    for (j = 0; j<n; j++)
      if (!isVisited[j] && dis[j]<minDis)
      {
        minDis = dis[j];
        cur = j;
      }
    if (minDis == INF) //已經完成了連通路徑的遍歷。
      return;
    //dis[cur]爲dis數組中的最小值,訪問節點cur.
    isVisited[cur] = true;
    //更新Dis數組的內容.
    for (j = 0; j<n; j++)
      if (road[cur][j] <INF && dis[j] > dis[cur] + road[cur][j])
        dis[j] = dis[cur] + road[cur][j];
  }
}

//深度搜索來得到最短路徑的數目。
void dfs(int n, int cId, int des, int curDis, int curTeamsNum)
{
  isVisited[cId] = true;
  if (cId == des)
  {
    if (curDis == dis[des]) //找到一條最短路徑
    {
      path_num++;//最短路徑數目加1
      if (curTeamsNum > g_max_team_num)
        g_max_team_num = curTeamsNum;
    }
    return;
  }
  if (curDis > dis[des]) //當前的路徑長度已經超過最短路徑,就沒有必要繼續搜索了。
    return;
  //從城市cId開始搜索
  for (int i = 0; i<n; i++)
  {

    if (!isVisited[i] && road[cId][i] < INF)//如果城市i沒有被訪問過,且cId到i連通。      
    {
      //isVisited[i] = true;
      dfs(n, i, des, curDis + road[cId][i], curTeamsNum + team_num[i]);
      isVisited[i] = false;
    }

    /*
    //這樣的剪枝比上一種更加強大。
    if(dis[cId] + road[cId][i] == dis[i])
    dfs(n,i,des,curDis+road[cId][i],curTeamsNum+team_num[i]);
    */
  }
}   

int main()
{
  int i, j, n, m, c1, c2, L, src, des;
    
  initData();
  ios::sync_with_stdio(false);
  cin >> n >> m >> src >> des;
  for (i = 0; i<n; i++)
    cin >> team_num[i];
  for (i = 0; i<m; i++)
  {
    cin >> c1 >> c2 >> L;
    road[c1][c2] = L;
    road[c2][c1] = L;
  }

  Dijstra(n, src, des);

  //重置各city的被訪問狀態。
  for (i = 0; i<n; i++)
    isVisited[i] = false;

  dfs(n, src, des, 0, team_num[src]);

  cout << path_num << " " << g_max_team_num << endl;
  return 0;
}

=================================================================================================

很久沒寫過了,今天回來,慢慢訓練,慢慢提升。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章