Advertisement
Guest User

Untitled

a guest
Nov 21st, 2019
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.97 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. #define vi vector<int>
  6. #define pb push_back
  7. #define mp make_pair
  8. #define ii pair<int,int>
  9.  
  10. #define ll long long int
  11. #define N 1305
  12. #define oo 1e5
  13. #define pil pair<int,int>
  14.  
  15.  
  16. int getVertex(char a)
  17. {
  18. if(a == 'A'){
  19. return 1;
  20. }
  21. return (a % 65)+1;
  22. }
  23.  
  24. char getCharVertex(int a)
  25. {
  26. if(a == 1){
  27. return 'A';
  28. }
  29. return 'A' + (a-1);
  30. }
  31.  
  32. int n;
  33.  
  34. vector<vector<pil>> graph(N);
  35. vector<vector<int>> dist(N,vector<int>(N,oo));
  36.  
  37. void FloydWarshal(){
  38. for(int i=1;i<=n;i++){
  39. dist[i][i] = 0;
  40. for(pil aresta : graph[i]){
  41. dist[i][aresta.first] = aresta.second;
  42. }
  43. }
  44.  
  45. for(int k=1; k<=n; k++){
  46. for(int i=1; i<=n; i++){
  47. for(int j=1; j<=n; j++){
  48. if(dist[i][j] > dist[i][k] + dist[k][j]){
  49. dist[i][j] = dist[i][k] + dist[k][j];
  50. }
  51. }
  52. }
  53. }
  54. }
  55.  
  56. pil PegarRodoviaProxima(int i1,int i2)
  57. {
  58. for(int j=1;j<n;j++){
  59. if((dist[i1][j] != 0)&&(dist[i2][j]!=0)&&(dist[i1][j]!=oo)&&(dist[i2][j]!=oo)){
  60. if(((dist[i1][j]%79) == 0)&&((dist[i2][j]%84) == 0)){
  61. return mp(j,(dist[i1][j]/79)+(dist[i2][j]/84));
  62. }
  63. }
  64. }
  65. return mp(-1,oo);
  66. }
  67.  
  68.  
  69. int main(){
  70. char veiculo;
  71. char tipo;
  72. char va,vb;
  73. int a,b,peso;
  74. int norm;
  75. char Davis,Andre;
  76.  
  77. scanf("%d",&n);
  78.  
  79. for(int i=0;i<n;i++)
  80. {
  81. scanf("\n%c %c %c %c %d",&veiculo,&tipo,&va,&vb,&peso);
  82. a = getVertex(va);
  83. b = getVertex(vb);
  84. norm = (int)veiculo;
  85. if(tipo == 'B'){ //via unidirecional(U) ou bidirecional(B)
  86. graph[a].pb(mp(b,peso*norm));
  87. graph[b].pb(mp(a,peso*norm));
  88. }else if(tipo == 'U'){
  89. graph[a].pb(mp(b,peso*norm));
  90. }
  91. }
  92.  
  93. FloydWarshal();
  94.  
  95. scanf("\n%c %c",&Davis,&Andre);
  96. pil rodovia = PegarRodoviaProxima(getVertex(Davis),getVertex(Andre));
  97.  
  98. if(rodovia.second != oo){
  99. cout << rodovia.second << endl;
  100. printf("%c\n",getCharVertex(rodovia.first));
  101. }else{
  102. printf("-1\n");
  103. }
  104.  
  105. return 0;
  106. }
  107.  
  108.  
  109. /*
  110. compilar
  111. g++ arquivo.cpp -std=c++11 -o executavel
  112. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement