• POJ 1860 Currency Exchange


    题目链接:https://vjudge.net/problem/POJ-1860

    转自:https://blog.csdn.net/yslcl12345/article/details/50574213

    题目大意

      我们的城市有几个货币兑换点。每个点只能兑换两种货币。可以有几个点,专门从事相同货币兑换。每个点都有自己的汇率,A 到 B 的汇率是 1A 能换 B 的数量。同时交换时必须付手续费,手续费以来源货币收取。

      例如,如果你想到俄罗斯卢布兑换点换 100 美元,那里的汇率是 29.75,而手续费是 0.39,你会得到(100 - 0.39)× 29.75=2963.3975 卢布。

      在我们的城市里你可以处理 N 种不同的货币。每个交换点用 6 个整数表描述:整数 a 和 b(两种货币类型),a 到 b 的汇率,a 到 b 的佣金,b 到 a 的汇率,b 到 a 的佣金。

      你有 V 单位货币 S,你希望能通过一些操作(在不同的兑换点兑换),增加资本。当然,最后手中的钱仍然是 S。这可行吗?

    分析

      首先如果你通过一通操作,最后换回 S,赚钱了,那么这种操作可以反复进行无数次,你就可以赚无数的钱,也就是说,如果以货币为顶点,兑换关系为边构造一张有向图,图中必然存在你可以到达的正环。用 Bellman-Ford 算法判断一下即可(利用一个节点如果被更新了 N 次及以上必然存在 正/负 环的原理)。

    代码如下

      1 #include <cmath>
      2 #include <ctime>
      3 #include <iostream>
      4 #include <string>
      5 #include <vector>
      6 #include <cstdio>
      7 #include <cstdlib>
      8 #include <cstring>
      9 #include <queue>
     10 #include <map>
     11 #include <set>
     12 #include <algorithm>
     13 #include <cctype>
     14 #include <stack>
     15 #include <deque>
     16 #include <list>
     17 #include <sstream>
     18 #include <cassert>
     19 using namespace std;
     20  
     21 #define INIT() ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
     22 #define Rep(i,n) for (int i = 0; i < (n); ++i)
     23 #define For(i,s,t) for (int i = (s); i <= (t); ++i)
     24 #define rFor(i,t,s) for (int i = (t); i >= (s); --i)
     25 #define ForLL(i, s, t) for (LL i = LL(s); i <= LL(t); ++i)
     26 #define rForLL(i, t, s) for (LL i = LL(t); i >= LL(s); --i)
     27 #define foreach(i,c) for (__typeof(c.begin()) i = c.begin(); i != c.end(); ++i)
     28 #define rforeach(i,c) for (__typeof(c.rbegin()) i = c.rbegin(); i != c.rend(); ++i)
     29  
     30 #define pr(x) cout << #x << " = " << x << "  "
     31 #define prln(x) cout << #x << " = " << x << endl
     32  
     33 #define LOWBIT(x) ((x)&(-x))
     34  
     35 #define ALL(x) x.begin(),x.end()
     36 #define INS(x) inserter(x,x.begin())
     37 #define UNIQUE(x) x.erase(unique(x.begin(), x.end()), x.end())
     38 #define REMOVE(x, c) x.erase(remove(x.begin(), x.end(), c), x.end()); // 删去 x 中所有 c 
     39 #define TOLOWER(x) transform(x.begin(), x.end(), x.begin(),::tolower);
     40 #define TOUPPER(x) transform(x.begin(), x.end(), x.begin(),::toupper);
     41  
     42 #define ms0(a) memset(a,0,sizeof(a))
     43 #define msI(a) memset(a,inf,sizeof(a))
     44 #define msM(a) memset(a,-1,sizeof(a))
     45 
     46 #define MP make_pair
     47 #define PB push_back
     48 #define ft first
     49 #define sd second
     50  
     51 template<typename T1, typename T2>
     52 istream &operator>>(istream &in, pair<T1, T2> &p) {
     53     in >> p.first >> p.second;
     54     return in;
     55 }
     56  
     57 template<typename T>
     58 istream &operator>>(istream &in, vector<T> &v) {
     59     for (auto &x: v)
     60         in >> x;
     61     return in;
     62 }
     63  
     64 template<typename T1, typename T2>
     65 ostream &operator<<(ostream &out, const std::pair<T1, T2> &p) {
     66     out << "[" << p.first << ", " << p.second << "]" << "
    ";
     67     return out;
     68 }
     69 
     70 inline int gc(){
     71     static const int BUF = 1e7;
     72     static char buf[BUF], *bg = buf + BUF, *ed = bg;
     73     
     74     if(bg == ed) fread(bg = buf, 1, BUF, stdin);
     75     return *bg++;
     76 } 
     77 
     78 inline int ri(){
     79     int x = 0, f = 1, c = gc();
     80     for(; c<48||c>57; f = c=='-'?-1:f, c=gc());
     81     for(; c>47&&c<58; x = x*10 + c - 48, c=gc());
     82     return x*f;
     83 }
     84 
     85 template<class T>
     86 inline string toString(T x) {
     87     ostringstream sout;
     88     sout << x;
     89     return sout.str();
     90 }
     91 
     92 inline int toInt(string s) {
     93     int v;
     94     istringstream sin(s);
     95     sin >> v;
     96     return v;
     97 }
     98 
     99 //min <= aim <= max
    100 template<typename T>
    101 inline bool BETWEEN(const T aim, const T min, const T max) {
    102     return min <= aim && aim <= max;
    103 }
    104  
    105 typedef long long LL;
    106 typedef unsigned long long uLL;
    107 typedef pair< double, double > PDD;
    108 typedef pair< int, int > PII;
    109 typedef pair< int, PII > PIPII;
    110 typedef pair< string, int > PSI;
    111 typedef pair< int, PSI > PIPSI;
    112 typedef set< int > SI;
    113 typedef set< PII > SPII;
    114 typedef vector< int > VI;
    115 typedef vector< double > VD;
    116 typedef vector< VI > VVI;
    117 typedef vector< SI > VSI;
    118 typedef vector< PII > VPII;
    119 typedef map< int, int > MII;
    120 typedef map< LL, int > MLLI;
    121 typedef map< int, string > MIS;
    122 typedef map< int, PII > MIPII;
    123 typedef map< PII, int > MPIII;
    124 typedef map< string, int > MSI;
    125 typedef map< string, string > MSS;
    126 typedef map< PII, string > MPIIS;
    127 typedef map< PII, PII > MPIIPII;
    128 typedef multimap< int, int > MMII;
    129 typedef multimap< string, int > MMSI;
    130 //typedef unordered_map< int, int > uMII;
    131 typedef pair< LL, LL > PLL;
    132 typedef vector< LL > VL;
    133 typedef vector< VL > VVL;
    134 typedef priority_queue< int > PQIMax;
    135 typedef priority_queue< int, VI, greater< int > > PQIMin;
    136 const double EPS = 1e-8;
    137 const LL inf = 0x3fffffff;
    138 const LL infLL = 0x3fffffffffffffffLL;
    139 const LL mod = 1e9 + 7;
    140 const int maxN = 1e2 + 7;
    141 const LL ONE = 1;
    142 const LL evenBits = 0xaaaaaaaaaaaaaaaa;
    143 const LL oddBits = 0x5555555555555555;
    144 
    145 struct Edge{
    146     // u->v
    147     int u, v;
    148     double r, c;
    149 }; 
    150 
    151 istream& operator>> (istream &in, Edge &x) {
    152     in >> x.u >> x.v >> x.r >> x.c;
    153     return in;
    154 }
    155 
    156 int N, M, S;
    157 
    158 VI V[maxN];
    159 double dist[maxN];
    160 vector< Edge > E;
    161 
    162 inline void addEdge(Edge &x) {
    163     V[x.u].PB(E.size());
    164     E.PB(x);
    165 }
    166 
    167 bool Bellman_Ford() {
    168     Rep(i, N - 1) {
    169         bool flag = 0;
    170         Rep(j, E.size()) {
    171             // 由于初始值是 0,所以并不需要考虑钱为负的情况,松弛之后还是 0,不影响答案 
    172             // 松弛
    173             dist[E[j].v] = max(dist[E[j].v], (dist[E[j].u] - E[j].c) * E[j].r);
    174             flag = 1;
    175         }
    176         if(!flag) return false; // 提前不能松弛,则可提前退出 
    177     }
    178     
    179     Rep(j, E.size()) {
    180         // 如果还可以松弛,就说明有正环 
    181         if(dist[E[j].v] < (dist[E[j].u] - E[j].c) * E[j].r) return true;
    182     }
    183     return false;
    184 }
    185 
    186 
    187 int main(){
    188     //freopen("MyOutput.txt","w",stdout);
    189     //freopen("input.txt","r",stdin);
    190     INIT();
    191     cin >> N >> M >> S;
    192     cin >> dist[S];
    193     Rep(i, M) {
    194         Edge t;
    195         cin >> t;
    196         addEdge(t);
    197         swap(t.u, t.v);
    198         cin >> t.r >> t.c;
    199         addEdge(t);
    200     }
    201     
    202     if(Bellman_Ford()) cout << "YES
    ";
    203     else cout << "NO
    ";
    204     return 0;
    205 }
    View Code
  • 相关阅读:
    Layabox记录坑
    爬虫 Beautifulsoup 常用笔记
    opencv +python 提取roi目标区域全部像素的值 得出上下限 均匀值
    opencv + python 读取像素点 BGRtoRGB 以及注意事项
    pyinstaller打包python+opencv 无法在别人电脑上正常运行 问题所在:opencv_ffmpeg341_64.dll
    python 绝对路径相对路径
    面向对象编程-OOP-一张图看懂类和实例的基本用法
    python3 _笨方法学Python_日记_DAY7
    儋州“炰米”:美味的特制粮食
    PHP学习步骤
  • 原文地址:https://www.cnblogs.com/zaq19970105/p/11286979.html
Copyright © 2020-2023  润新知