Poj1860 -- currency exchange

Source: Internet
Author: User

Reverse application of Bellman-Ford algorithm-positive loop check

/** \ Brief poj 1860 Bellman-Ford ** \ Param date 2014/7/24 * \ Param state AC * \ return memory 708 K time 141 Ms **/# include <iostream> # include <fstream> # include <cstring> using namespace STD; struct rateandcom {// public: int A; int B; double rate; double com ;}; // map [maxn]; const int maxn = 101; rateandcom map [101*2]; double dis [maxn]; int N; // Number of currency types int m; // number of exchange points int S; // hold the second currency Double V; // The second currency principal int alledge; bool Bellman_ford () {memset (DIS, 0, sizeof (DIS); DIS [s] = V;/* Relax */bool flag; For (INT I = 1; I <= N-1; I ++) {flag = false; For (Int J = 0; j <alledge; j ++) if (DIS [map [J]. b] <(DIS [map [J]. a]-map [J]. com) * map [J]. rate) {dis [map [J]. b] = (DIS [map [J]. a]-map [J]. com) * map [J]. rate; flag = true;} If (! Flag) break;} For (int K = 0; k <alledge; k ++) if (DIS [map [K]. b] <(DIS [map [K]. a]-map [K]. com) * map [K]. rate) return true; return false;} int main () {// cout <"Hello world! "<Endl; // freopen (" input.txt "," r ", stdin); // while (scanf (" % d % F ", & N, & M, & S, & V )! = EOF) while (CIN> N> m> S> V) {alledge = 0; For (INT I = 0; I <m; I ++) {int A, B; double Rab; double cab; double RBA; double CBA; // CIN> A> B> map [a] [B]. rate> map [a] [B]. commission //> map [B] [a]. rate> map [B] [a]. commission; CIN> A> B> Rab> cab> RBA> CBA; Map [alledge]. A = A; Map [alledge]. B = B; Map [alledge]. rate = Rab; Map [alledge]. COM = cab; alledge ++; Map [alledge]. A = B; Map [alledge]. B = A; Map [alledge]. rate = RBA; Map [alledge]. COM = CBA; alledge ++;} // Bellman-Ford if (bellman_ford () cout <"yes" <Endl; else cout <"no" <Endl;} return 0 ;}
Reprinted please indicate the source: http://blog.csdn.net/greenapple_shan/article/details/38307879

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.