fork download
  1. #include<bits/stdc++.h>
  2. #define INF 999999999
  3.  
  4. using namespace std;
  5.  
  6. int graph[100][100];
  7. int path[100][100];
  8.  
  9. void init()
  10. {
  11. for(int i=1; i<=100; i++)
  12. {
  13. for(int j=1; j<=100; j++)
  14. {
  15. if(i==j) graph[i][j] = 0;
  16. else graph[i][j] = INF;
  17. path[i][j] = i;
  18. }
  19. }
  20. }
  21.  
  22. void printpath(int i, int j)
  23. {
  24. if(i!=j)
  25. printpath(i, path[i][j]);
  26. printf(" %d", j);
  27. }
  28.  
  29. int main()
  30. {
  31. int node, caseno = 0;
  32. while(scanf("%d", &node) && node!=0)
  33. {
  34. init();
  35. int edge, v, cost;
  36. for(int u=1; u<=node; u++)
  37. {
  38. scanf("%d", &edge);
  39. for(int i=0; i<edge; i++)
  40. {
  41. scanf("%d %d", &v, &cost);
  42. graph[u][v] = cost;
  43. }
  44. }
  45. int s, d;
  46. scanf("%d %d", &s, &d);
  47. for(int i=1; i<=node; i++)
  48. {
  49. for(int j=1; j<=node; j++)
  50. {
  51. for(int k=1; k<=node; k++)
  52. {
  53. if(graph[i][j]>graph[i][k]+graph[k][j])
  54. {
  55. graph[i][j] = graph[i][k] + graph[k][j];
  56. path[i][j] = path[k][j];
  57. }
  58. }
  59. }
  60. }
  61. printf("Case %d: Path =", ++caseno);
  62. printpath(s, d);
  63. printf("; %d second delay\n", graph[s][d]);
  64. }
  65. return 0;
  66. }
  67.  
Success #stdin #stdout 0s 3540KB
stdin
4
1 2 3
0
1 3 4
0
1 2

4
2  2 1  3 2
1  4 10
1  4 1
0
1 4

4
4  1 57  2 11  3 14  4 97
4  1 45  2 17  3 10  4 80
4  1 63  2 74  3 29  4 48
4  1 7  2 42  3 52  4 10
3 2

3
1 2 5
1 3 5
1 1 5
1 1

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

2
1   2 5
1   1 6
1 2

7
4   2 5   3 13   4 8   5 18
2   3 7   6 14
1   6 6
2   3 5   5 9
3   6 2   7 9    4 6
1   7 2
0
1 7

0
stdout
Case 1: Path = 1 2; 3 second delay
Case 2: Path = 1 3 4; 3 second delay
Case 3: Path = 3 4 1 2; 66 second delay
Case 4: Path = 1; 0 second delay
Case 5: Path = 2; 0 second delay
Case 6: Path = 1 2; 5 second delay
Case 7: Path = 1 2 3 6 7; 20 second delay