To find a circuit, from a currency to exchange a circle back to their own, see whether the exchange rate of the product is greater than 1

Source: Internet
Author: User
Tags define include printf return strcmp
 
 
  1. To find a circuit, from a currency to exchange a circle back to their own, see whether the exchange rate of the product is greater than 1
  2. Extend the General shortest road bellman, calculate to Dist (n), is counted to the loop
  3. Maxdist (k) [v] = max{maxdist (k-1) [V],maxdist (K-1) [U]*c (U,v)}
  4. #include
  5. #include
  6. #include
  7. #define MAXN 50
  8. #define MAXM 1000
  9. using namespace Std;
  10. struct Exchange
  11. {
  12. int CI,CJ;
  13. Double CIJ;
  14. }EX[MAXM];
  15. Char name[maxn][20],a[20],b[20];
  16. Double X;
  17. int n,m;
  18. Double MAXDIST[MAXN];
  19. int flag;
  20. int kase = 0;
  21. int Readcase ()
  22. {
  23. scanf ("%d", &n);
  24. if (n = = 0)
  25. return 0;
  26. for (int i = 0; i < n; i++)
  27. scanf ("%s", Name[i]);
  28. scanf ("%d", &m);
  29. for (int i = 0; i < m; i++)
  30. {
  31. int q,w;
  32. scanf ("%s%lf%s", a,&x,b);
  33. for (q = 0; strcmp (a,name[q]); q++)
  34. ;
  35. for (w = 0; strcmp (b,name[w]); w++)
  36. ;
  37. Ex[i].ci = q;
  38. EX[I].CJ = W;
  39. EX[I].CIJ = x;
  40. }
  41. return 1;
  42. }
  43. void Bellman (int v0)
  44. {
  45. Flag = 0;
  46. memset (maxdist,0,sizeof (maxdist));
  47. Maxdist[v0] = 1;
  48. for (int k = 1; k <= N; k++)//1---N
  49. {
  50. for (int i = 0; i < m; i++)
  51. {
  52. if (Maxdist[ex[i].ci]*ex[i].cij > MAXDIST[EX[I].CJ])
  53. MAXDIST[EX[I].CJ] = MAXDIST[EX[I].CI]*EX[I].CIJ;
  54. }
  55. }
  56. if (Maxdist[v0] > 1.0)
  57. flag = 1;
  58. }
  59. int main ()
  60. {
  61. while (Readcase ())
  62. {
  63. for (int i = 0; i < n; i++)
  64. {
  65. Bellman (i);
  66. if (flag)
  67. Break
  68. }
  69. if (flag)
  70. printf ("Case%d:yes\n", ++kase);
  71. Else
  72. printf ("Case%d:no\n", ++kase);
  73. }
  74. }
This article links http://www.cxybl.com/html/wyzz/JavaScript_Ajax/20130720/39222.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.