HDU 1217 Arbitrage Two algorithms AC code, Floyd+bellman-ford flood problem A note is a map ~ ~

Source: Internet
Author: User
Tags money back

ArbitrageTime limit:2000/1000 MS (java/others) Memory limit:65536/32768 K (java/others)
Total submission (s): 4998 Accepted Submission (s): 2286


Problem Descriptionarbitrage is the use of discrepancies in currency exchange rates to transform one unit of a currency in To more than one unit of the same currency. For example, suppose that 1 US Dollar buys 0.5 British pound, 1 British pound buys 10.0 French francs, and 1 French franc Buys 0.21 US dollar. Then, by converting currencies, a clever trader can start with 1 US dollar and buy 0.5 * 10.0 * 0.21 = 1.05 US dollars, MA King a profit of 5 percent.

Your job is to write a program this takes a list of currency exchange rates as input and then determines whether arbitrage is possible or not.

Inputthe input file would contain one or more test cases. Om the first line of all test case there was an integer n (1<=n<=30), representing the number of different Currencie S. The next n lines each contain the name of one currency. Within a name no spaces would appear. The next line contains one integer m, representing the length of the table to follow. The last m lines all contain the name CI of a source currency, a real number Rij which represents the exchange rate from CI to CJ and a name CJ of the destination currency. Exchanges which do not appear in the table is impossible.
Test cases is separated from a blank line. Input is terminated by a value of zero (0) for N.

Outputfor each test case, print one line telling whether arbitrage was possible or not in the format "case Case:yes" Respe ctively "Case Case:no".

Sample Input
3usdollarbritishpoundfrenchfranc3usdollar 0.5 britishpoundbritishpound 10.0 Frenchfrancfrenchfranc 0.21 Usdollar3usdollarbritishpoundfrenchfranc6usdollar 0.5 britishpoundusdollar 4.9 frenchfrancbritishpound 10.0 Frenchfrancbritishpound 1.99 usdollarfrenchfranc 0.09 Britishpoundfrenchfranc 0.19 USDollar0

Sample Output
Case 1:yescase 2:no
The main meaning of this problem is a currency in the exchange of a circle, the exchange of money back more than the original money, the output is yes, otherwise no.Say, I do this water problem all day interesting ...Finally, the processing of the string is a map, map the map into numbers, corresponding to the subscript of the array.Two parts of the code is an AC, the problem is too water.Bellman-ford algorithm:
#include <iostream> #include <string> #include <cstring> #include <cstdio> #include <map>  #define INF 100000000#define MAX using namespace std;d ouble dis[max],graph[max][max];bool bellman_ford (int n) {for (int i = 0; I <= N; ++i) {Dis[i] = 0.0;} Dis[0] = 1; for (int i = 0, i < n-1; ++i) {for (int j = 0; J < N; ++j) {for (int k = 0; k < n; ++k) {if (graph[k][ J] > 0.000001) {if (Dis[j] < dis[k]*graph[k][j]) {dis[j] = Dis[k]*graph[k][j];}}}} for (int i = 0, i < n; ++i) {for (int j = 0; J < N; ++j) {if (Dis[i] < dis[j]*graph[j][i])//As long as one exists. {return true;}}} return false;} int main () {int n, m, C = 1; double rate;map<string,int> search;string namea,nameb,temp; while (Cin>>n & & N) {for (int i = 0; i < n; ++i) {cin>>temp; Search.insert (pair<string,int> (Temp,i));} Cin>>m;memset (graph,0,sizeof (graph)); for (int j = 0; j < m; ++j) {cin>>namea>>rate>>nameb; int x = Search[namea], y = SEARCH[NAMEB]; graph[x][y] = rate;} if (Bellman_ford (n)) {printf ("Case%d:yes\n", C + +);} else{printf ("Case%d:no\n", C + +);} Search.clear ();} return 0;}

Floyd algorithm:
<pre name= "code" class= "CPP" > #include <iostream> #include <string> #include <cstring> #include <cstdio> #include <map> #define INF 100000000#define MAX, using namespace std;d ouble graph[max][max];bool Fl Oyd (int n) {for (int k = 0, k < n; ++k) {for (int i = 0, i < n; ++i) {for (int j = 0; J < N; ++j) {if (Graph[i][j] < GRAPH[I][K]*GRAPH[K][J]) graph[i][j] = Graph[i][k]*graph[k][j];}}} for (int i = 0; i < n; ++i) {if (graph[i][i]>1) {return true;}} return false;} int main () {int n, m, C = 1; double rate;map<string,int> search;string namea,nameb,temp; while (Cin>>n & & N) {memset (graph,0,sizeof (graph)), for (int i = 0; i < n; ++i) {cin>>temp; Search.insert (pair<string,int& gt; (temp,i)); graph[i][i] = 1;} cin>>m;for (int j = 0; j < m; ++j) {Cin>>namea>>rate>>nameb;int x = Search[namea], y = search[ NAMEB]; graph[x][y] = rate;} if (Floyd (n)) {printf ("Case%d:yes\n", C + +);} else{printf ("Case%d:no\n ", C + +);} Search.clear ();} return 0;}


with June mutual encouragement.

HDU 1217 Arbitrage Two algorithms AC code, Floyd+bellman-ford flood problem A note is a map ~ ~

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.