当前位置:   article > 正文

Pat(Advanced Level)Practice--1003(Emergency)_pat advanced level 1003 c语言实现

pat advanced level 1003 c语言实现

Pat1003代码

题目描述:

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

题目是求最短路径数和最大权重和。。。
AC代码:
  1. /*
  2. 解题思想:
  3. 先进行最短路径算法,找出最短路径
  4. 再进行深搜,若等于最短路径,则计数加一
  5. 并累计最大救援数
  6. */
  7. #include<cstdio>
  8. #define MAX 501
  9. #define INF 0x0FFFFFFF
  10. int visited[MAX];
  11. int map[MAX][MAX];
  12. int dis[MAX],team[MAX];
  13. int paths=0,helpers=0;
  14. void Init()
  15. {
  16. int i,j;
  17. for(i=0;i<MAX;i++)
  18. {
  19. visited[i]=0;
  20. dis[i]=INF;
  21. team[i]=0;
  22. for(j=0;j<MAX;j++)
  23. {
  24. map[i][j]=INF;
  25. map[j][i]=INF;
  26. if(i==j)
  27. map[i][j]=0;
  28. }
  29. }
  30. }
  31. void Dijkstra(int v,int n)
  32. {
  33. int i,j,min,index;
  34. for(i=0;i<n;i++)
  35. dis[i]=map[v][i];
  36. visited[i]=1;
  37. for(i=0;i<n-1;i++)
  38. {
  39. min=INF;
  40. for(j=0;j<n;j++)
  41. if(!visited[j]&&dis[j]<min)
  42. {
  43. min=dis[j];
  44. index=j;
  45. }
  46. visited[index]=1;
  47. for(j=0;j<n;j++)
  48. {
  49. if(map[index][j]<INF&&dis[j]>map[index][j]+dis[index])
  50. dis[j]=dis[index]+map[index][j];
  51. }
  52. }
  53. }
  54. void DFS(int n,int s,int d,int curdis,int curnum)
  55. {
  56. int i;
  57. visited[s]=1;
  58. if(s==d)
  59. {
  60. if(curdis==dis[d])
  61. {
  62. paths++;
  63. if(curnum>helpers)
  64. helpers=curnum;
  65. }
  66. return;
  67. }
  68. if(curdis>dis[d])//当前路径超过最短路径,停止搜索
  69. return;
  70. for(i=0;i<n;i++)
  71. {
  72. if(!visited[i]&&map[s][i]<INF)//城市i未被访问,且c联通到i
  73. {
  74. DFS(n,i,d,curdis+map[s][i],curnum+team[i]);
  75. visited[i]=0;
  76. }
  77. }
  78. }
  79. int main(int argc,char *argv[])
  80. {
  81. int N,M,S,D;
  82. int C1,C2;
  83. int i,j,len;
  84. scanf("%d%d%d%d",&N,&M,&S,&D);
  85. Init();
  86. for(i=0;i<N;i++)
  87. scanf("%d",&team[i]);
  88. for(i=0;i<M;i++)
  89. {
  90. scanf("%d%d%d",&C1,&C2,&len);
  91. map[C1][C2]=len;
  92. map[C2][C1]=len;
  93. }
  94. Dijkstra(S,N);
  95. for(i=0;i<N;i++)
  96. visited[i]=0;
  97. DFS(N,S,D,0,team[S]);
  98. printf("%d %d\n",paths,helpers);
  99. return 0;
  100. }


声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/IT小白/article/detail/627369
推荐阅读
相关标签
  

闽ICP备14008679号