Saturday, May 13, 2017

【UVa】11463 - Commandos

Problem here

Solution

floyd刷所有點到其他點的最短路
再在上面找開始點到集合點的最大值
  1. #include <iostream>
  2. #include <memory.h>
  3. #define MAX_NUM 1<<29
  4. using namespace std;
  5. int main(){
  6. int kase;
  7. cin >> kase;
  8. int cnt = 1;
  9. while(kase--){
  10. int n, r;
  11. int rat[101][101];
  12. cin >> n >> r;
  13. for(int i = 0; i < n; i++)
  14. for(int j = 0; j < n; j++)
  15. if(i==j)
  16. rat[i][j] = rat[j][i] = 0;
  17. else
  18. rat[i][j] = MAX_NUM;
  19. int s, e;
  20. while(r--){
  21. int x, y;
  22. cin >> x >> y;
  23. rat[x][y] = rat[y][x] = 1;
  24. }
  25. for(int i = 0; i < n; i++)
  26. for(int j = 0; j < n; j++)
  27. for(int k = 0; k < n; k++)
  28. if(rat[j][i] + rat[i][k] < rat[j][k])
  29. rat[j][k] = rat[j][i] + rat[i][k];
  30. cin >> s >> e;
  31. int ans = 0;
  32. for(int i = 0; i < n; i++)
  33. if(ans < rat[s][i] + rat[i][e])
  34. ans = rat[s][i] + rat[i][e];
  35. cout << "Case " << cnt++ << ": " << ans << endl;
  36. }
  37. return 0;
  38. }

No comments:

Post a Comment