Advertisement
Guest User

Untitled

a guest
Dec 18th, 2018
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.71 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #define FOR(i,a,b) for(int i=a;i<b;i++)
  3. #define eb emplace_back
  4. #define INF 10000010
  5. #define min3(a,b,c) min(a,min(b,c))
  6. #define max3(a,b,c) max(a, max(b,c))
  7. using namespace std;
  8.  
  9. int N, visitado[70];
  10.  
  11. struct edge{
  12. int src,dest,w;
  13. edge(int a, int b, int c): src(a), dest(b), w(c){}
  14. };
  15.  
  16.  
  17. bool bellman(vector<edge> &arestas, double x)
  18. {
  19. int tam = arestas.size();
  20. double d[N+10];
  21. FOR(i, 0, N+5)d[i] = 0*1.0;
  22. FOR(i,1,N)
  23. FOR(j, 0,tam)
  24. {
  25. int source = arestas[j].src;
  26. int destiny = arestas[j].dest;
  27. double weight = arestas[j].w*1.0;
  28. if (d[destiny]>d[source]+weight - x){
  29. d[destiny] = d[source]+weight -x;
  30. }
  31.  
  32. }
  33. FOR(j,0,tam)
  34. {
  35. int source = arestas[j].src;
  36. int destiny = arestas[j].dest;
  37. double weight = arestas[j].w*1.0;
  38. if (d[destiny]>d[source]+weight - x)
  39. return 1;
  40. }
  41. return 0;
  42. }
  43.  
  44. int main()
  45. {
  46. ios_base::sync_with_stdio(false);cin.tie(NULL);
  47. int t,kk=0;
  48. cin>>t;
  49. while (kk<t)
  50. {
  51. kk++;
  52. cout<<"Case #"<<kk<<": ";
  53. int M,menor=INF,maior=INF;
  54. cin>>N>>M;
  55. vector<edge> arestas;
  56. FOR(i, 0, M)
  57. {
  58. int a,b,c;
  59. cin>>a>>b>>c;
  60. arestas.eb(a,b,c);
  61. if(maior==INF) maior = c;
  62. else maior=max(c,maior);
  63. menor = min(menor,c);
  64. }
  65. double fim = maior*1.0,ini = menor*1.0,best=INF*1.0;
  66. while (fim - ini>0.001)
  67. {
  68. double mid = (fim+ini)/2.0;
  69. if (bellman(arestas, mid)) best=fim = mid;
  70. else ini = mid;
  71. }
  72. if (best==INF)
  73. {
  74. cout<<"No cycle found."<<endl;
  75. continue;
  76. }
  77. cout.precision(2);
  78. cout.setf(ios::fixed);
  79. cout<<ini<<endl;
  80. }
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement