Here you will find solutions of many problems on spoj. If you want solution of some problem which is not listed in blog or have doubt regarding any spoj problem (which i have solved) or any programming concept (data structure) you can mail me @ raj.nishant360@gmail.com

And my humble request to you all that don't copy the code only try to understand the logic and algorithm behind the code. I have started this because if you tried as hard as you can and still can't find any solution to the problem then you can refer to this.
You can read my answer how to start competitive programming CLICK HERE

Sunday, February 8, 2015

ARBITRAG-Arbitrage

Arbitrage

Given below cpp code is for ARBITRAG spoj or arbitrage spoj.

Explanation :- simple Floyd-Warshall implementation.




#include <bits/stdc++.h>
using namespace std;
int main(){
    for(int test = 1; ; test++){
        int n;
        scanf("%d",&n);
        if(n == 0)
            return 0;
        map<string , int> m;
        string s , source , dest;
        for(int i = 0 ; i < n ; i++){
            cin>>s;
            m[s] = i;
        }
        int table;
        cin>>table;
        double dist[50][50] , cost ;
        for(int i = 0 ; i < n ; i++){
            for(int j = 0 ; j < n ; j++){
                dist[i][j] = 0.0;
            } 
        }
        for(int i = 0 ; i < table ; i++){
            cin>>source>>cost>>dest;
            int x , y;
            x = m[source];
            y = m[dest];
            dist[x][y] = cost;
        }
        //floyd warshall
        for(int k = 0 ; k < n ; k++)
            for(int i = 0 ; i < n ; i++)
                for(int j = 0 ; j < n ; j++)
                        dist[i][j] = max(dist[i][j] , dist[i][k] * dist[k][j]);
        int flag = 0;
        for(int i = 0 ; i < n ; i++){
                if(dist[i][i] > 1.0 ){
                    flag = 1;
                    break;
                }
        }
        if(flag)
            cout<<"Case "<<test<<": Yes\n";
        else
            cout<<"Case "<<test<<": No\n";
    }
}

No comments:

Post a Comment

Your comment is valuable to us