Skip to main content

ARBITRAG - Arbitrage

Problem Link

#include <iostream>
#include <map>
#include <vector>
using namespace std;

struct edge
{
     int s, d;
     double c;
};

map<string, int> nodes;
double v[50];
vector<edge> e;

bool bellmanFord(int s, int n)
{
     v[s] = 1;

     int source, dest;
     double rate;

     for(int j = 0; j < n - 1; j++)
     {
          for(int i = 0; i < e.size(); i++)
          {
               source = e[i].s;
               dest = e[i].d;
               rate = e[i].c;

               if(v[dest] < v[source] * rate)
               {
                    v[dest] = v[source] * rate;
               }
          }
     }

     for(int i = 0; i < e.size(); i++)
     {
               source = e[i].s;
               dest = e[i].d;
               rate = e[i].c;

               if(v[dest] < v[source] * rate)
               {   
                    v[dest] = v[source] * rate;
                    return true;
               }
     }

     return false;
}

int main()
{
     string a, b, c;
     int i, j, n, m, source, dest;
     double rate;
     int t = 1;
     bool result;

     while(true)
     {
          nodes.clear();
          e.clear();
          result = false;

          cin >> n;

          if(n == 0)
               break;

          for(i = 0; i < n; i++)
          {
               cin >> a;
               nodes[a] = i;
          }

          for(i = 0; i < n; i++)
          {
               v[i] = 0.0;
          }

          cin >> m;
          for(i = 0; i < m; i++)
          {
               cin >> a >> rate >> b;
               source = nodes[a];
               dest = nodes[b];

               edge temp;
               temp.s = source;
               temp.d = dest;
               temp.c = rate;
               e.push_back(temp);
          }

          for(int i = 0; i < n; i++)
          {
               if(v[i] == 0)
               {
                    v[i] = 1;
                    result = bellmanFord(i, n);
                    if(result)
                    {
                         break;
                    }
               }
          }

          cout << "Case " << t << ": ";
          if(!result)
               cout << "No" << endl;
          else
               cout << "Yes" << endl;
          t++;

     }
}

Comments

Popular posts from this blog

ANARC09A

ANARC09A - Seinfeld #include <iostream> #include <stack> using namespace std; stack<char> st; int main() {     string s;     int c, k;     k = 1;     while(true)     {         cin >> s;         c = 0;         if(s[0] == '-')             break;         for(int i = 0; i < s.size(); i++)         {             st.push(s[i]);             //stack contains } and { then we can safely remove them from stack             if(st.top() == '}' && st.size() != 1)             {                 char a = st.top();               ...

Maximum Weight Difference -MAXDIFF

Problem Link #include <iostream> #include <algorithm> using namespace std; int arr[101]; int main() {     int t, n, k;     long sum1, sum2, res;         cin >> t;     while(t--)     {         cin >> n >> k;         for(int i = 0; i < n; i++)         {             cin >> arr[i];         }                 if(k > n / 2)         {             k = n - k;         }                 sort(arr, arr + n);         sum1 = sum2 = 0;                 for(int i = 0; i < k; i++)         {             sum1 ...

MKUHAR - Most Servings Meal

Problem Link #include <iostream> #include <cmath> using namespace std; struct node { int x, y, pm, sm, pv, sv; node(){} node(int x, int y, int pm, int sm, int pv, int sv) { this->x = x; this->y = y; this->pm = pm; this->sm = sm; this->pv = pv; this->sv = sv; } }; int n; node arr[101]; /* Find out if it is possible to have given number of servings for each ingredient with given money */ bool isServingPossible(int totalServings, int money) { int required = 0; /* check if we can have totalServings of each ingredient */ for(int i = 0; i < n; i++) { required = totalServings * arr[i].x; required -= arr[i].y; int minMoneySpent = 99999999; int limit = ceil(required / (double)arr[i].sm); for(int numSmallItem = 0; numSmallItem <= limit; numSmallItem++) { int totalSmallItem = numSmallItem * arr[i].sm; int numLargeItem = (required > totalSmallItem)? ceil((required - totalSmal...