1003. Emergency (25)

小咪咪 2022-05-31 03:39 269阅读 0赞

时间限制

400 ms

内存限制

65536 kB

代码长度限制

16000 B

判题程序

Standard

作者

CHEN, Yue

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

  1. 5 6 0 2
  2. 1 2 1 5 3
  3. 0 1 1
  4. 0 2 2
  5. 0 3 1
  6. 1 2 1
  7. 2 4 1
  8. 3 4 1

Sample Output

  1. 2 4

题目大意:

作为一个城市的紧急救援队长,你会得到一张你国家的特殊地图。这张图显示了由一些道路连成
的分散城市。在这张图上标注着每个城市救援队的数量和在每两个城市之间每条道路的长度。当你从其他城市接到紧急电话时,你的任务是把救援队员尽可能快的带到事发地,与此同时,尽可能多的给他们打电话。
输入:
每个输入文件包括一个测试用例。对于每个测试用例,第一行包含4个正整数:N(<=500)-城市的数量(城市的编号是从0到N-1),M-道路的数量,C1和C2-分别是你现在所在的城市和你必须拯救的城市。下一行包含N个整数,第i个整数是第i个城市的救援队的数目。然后接下来是M行,每一行用c1,c2,和L三个整数描述,这三个数分别表示由一条道路连接的两个城市和和那条路的长度。他保证从C1到C2至少存在一条道路。
输出:
对于每个测试用例,在一行中打印两个数字:C1和C2之间的不同最短路径的数量,以及你能聚集的最大的救援队的数量。
直线上的所有数字都必须被一个空格隔开,在一行的末尾没有多余的空格。

方法一:Dijkstra

  1. #include<bits/stdc++.h>
  2. #define INF 0x3f3f3f3f
  3. #define MAX 501
  4. int map[MAX][MAX],visited[MAX],path_number[MAX],dist[MAX],teams[MAX],team_account[MAX];
  5. int c1,c2,n,m;
  6. void Dijkstra()
  7. {
  8. int i,j,dmin,u;
  9. path_number[c1]=1;
  10. team_account[c1]=teams[c1];
  11. dist[c1]=0;
  12. for(i=0;i<n;i++)
  13. {
  14. dmin=INF;
  15. for(j=0;j<n;j++)
  16. {
  17. if(dist[j]<dmin&&visited[j]==0)
  18. {
  19. dmin=dist[j];
  20. u=j;
  21. }
  22. }
  23. if(dmin==INF||u==c2)
  24. {
  25. break;
  26. }
  27. visited[u]=1;
  28. for(j=0;j<n;j++)
  29. {
  30. if(visited[j]==0){
  31. if(dist[j]>dist[u]+map[u][j])
  32. {
  33. dist[j]=dist[u]+map[u][j];
  34. path_number[j]=path_number[u];
  35. team_account[j]=team_account[u]+teams[j];
  36. }
  37. else if(dist[j]==dist[u]+map[u][j])
  38. {
  39. path_number[j]+=path_number[u];
  40. if(team_account[j]<team_account[u]+teams[j])
  41. {
  42. team_account[j]=team_account[u]+teams[j];
  43. }
  44. }
  45. }
  46. }
  47. }
  48. }
  49. int main()
  50. {
  51. int i,j,k,t,p;
  52. scanf("%d %d %d %d",&n,&m,&c1,&c2);
  53. for(i=0;i<n;i++)
  54. {
  55. scanf("%d",&teams[i]);
  56. }
  57. for(i=0;i<n;i++)
  58. {
  59. dist[i]=INF;
  60. for(j=0;j<n;j++)
  61. {
  62. map[i][j]=INF;
  63. }
  64. }
  65. for(i=0;i<m;i++)
  66. {
  67. scanf("%d %d %d",&k,&t,&p);
  68. map[k][t]=map[t][k]=p;
  69. }
  70. Dijkstra();
  71. printf("%d %d",path_number[c2],team_account[c2]);
  72. return 0;
  73. }

方法二:DFS

  1. #include<bits/stdc++.h>
  2. #define MAX 501
  3. int map[MAX][MAX],visited[MAX],teams[MAX];
  4. int pathaccount,maxaccount,mind,n,m;
  5. void DFS(int start,int end,int pathlength,int teamaccount)
  6. {
  7. int i,j;
  8. if(start==end)
  9. {
  10. if(pathlength<mind)
  11. {
  12. pathaccount=1;
  13. mind=pathlength;
  14. maxaccount=teamaccount;
  15. }else if(pathlength==mind)
  16. {
  17. pathaccount++;
  18. if(maxaccount<teamaccount)
  19. {
  20. maxaccount=teamaccount;
  21. }
  22. }
  23. else
  24. {
  25. return ;
  26. }
  27. }
  28. if(pathlength>mind)
  29. {
  30. return ;
  31. }
  32. for(i=0;i<n;i++)
  33. {
  34. if(visited[i]==0&&map[start][i]<INT_MAX)
  35. {
  36. visited[i]=1;
  37. DFS(i,end,pathlength+map[start][i],teamaccount+teams[i]);
  38. visited[i]=0;
  39. }
  40. }
  41. }
  42. int main()
  43. {
  44. int i,j,k,t,c1,c2,l;
  45. mind=INT_MAX;
  46. scanf("%d %d %d %d",&n,&m,&c1,&c2);
  47. for(i=0;i<n;i++)
  48. {
  49. for(j=0;j<n;j++)
  50. {
  51. map[i][j]=INT_MAX;
  52. }
  53. }
  54. for(i=0;i<n;i++)
  55. {
  56. scanf("%d",&teams[i]);
  57. }
  58. for(i=0;i<m;i++)
  59. {
  60. scanf("%d %d %d",&k,&t,&l);
  61. map[k][t]=map[t][k]=l;
  62. }
  63. DFS(c1,c2,0,teams[c1]);
  64. printf("%d %d",pathaccount,maxaccount);
  65. }

发表评论

表情:
评论列表 (有 0 条评论,269人围观)

还没有评论,来说两句吧...

相关阅读