zoukankan      html  css  js  c++  java
  • POJ 1860

    我们的城市有几个货币兑换点。让我们假设每一个点都只能兑换专门的两种货币。可以有几个点,专门从事相同货币兑换。每个点都有自己的汇率,外汇汇率的A到B是B的数量你1A。同时各交换点有一些佣金,你要为你的交换操作的总和。在来源货币中总是收取佣金。 例如,如果你想换100美元到俄罗斯卢布兑换点,那里的汇率是29.75,而佣金是0.39,你会得到(100 - 0.39)×29.75=2963.3975卢布。 你肯定知道在我们的城市里你可以处理不同的货币。让每一种货币都用唯一的一个小于N的整数表示。然后每个交换点,可以用6个整数表描述:整数a和b表示两种货币,a到b的汇率,a到b的佣金,b到a的汇率,b到a的佣金。 nick有一些钱在货币S,他希望能通过一些操作(在不同的兑换点兑换),增加他的资本。当然,他想在最后手中的钱仍然是S。帮他解答这个难题,看他能不能完成这个愿望。
    Input
    第一行四个数,N,表示货币的总数;M,兑换点的数目;S,nick手上的钱的类型;V,nick手上的钱的数目;1<= S <= N <= 100, 1 <= M <= 100, V 是一个实数0 <= V <= 103. 接下来M行,每行六个数,整数a和b表示两种货币,a到b的汇率,a到b的佣金,b到a的汇率,b到a的佣金(0<=佣金<=102,10-2 <= 汇率 <= 102).

    Output

    如果nick能够实现他的愿望,则输出YES,否则输出NO。

    Sample Input

    3 2 1 20.0
    1 2 1.00 1.00 1.00 1.00
    2 3 1.10 1.00 1.10 1.00

    Sample Output

    YES

    题目大意:

    你有一种货币,两种货币兑换需要一定的佣金,然后进行货币转换,假设你有货币a,要转换为b,佣金为c,汇率为d,那么转换公式为:b = (a - c) * d。
    第一行给出4个数,依次为货币的种类数 n ,兑换点的个数m,手中现有的货币类型s和手中现有货币的数量v。接下来有 m 行,对于每一行输入有6个值,前两个数a b 表示a和b能进行转换,然后后四个数分别为a 到 b 的佣金,汇率,b 到 a 的佣金和汇率,判断是否能通过货币转换实现套现(货币转换后最终货币大于本金)。

    解题思路:

    抽象成图论题,求是否能套现即判断一下是否存在正环,建图的时候需要注意一下技巧。开两个数组记录权值,一个是佣金,一个是汇率,之后判断条件也要相应的改成上面的公式,只有换算完以后数变大了才可松弛,由于已经给出起点,直接用起点跑spfa判正环即可。

    Code:

    #include <cstdio>
    #include <iostream>
    #include <algorithm>
    #include <cstring>
    #include <iomanip>
    #include <sstream>
    #include <cmath>
    #include <vector>
    #include <queue>
    #include <stack>
    #include <map>
    #include <set>
    #define lowbit(x) x & (-x)
    
    using namespace std;
    
    typedef long long ll;
    typedef pair<int, int> pii;
    
    const int mod = 1e9 + 7;
    const int inf = 0x3f3f3f3f;
    const int N = 1e3 + 50;
    const int M = 1e4 + 50;
    
    int h[N], ne[M], e[M], idx;
    int n, m, s;
    double v;
    double dis[N], w[M], w1[M];
    int cnt[N];
    bool vis[N];
    
    void add(int a, int b, double c, double d)
    {
    	e[idx] = b;
    	w[idx] = c;
    	w1[idx] = d;
    	ne[idx] = h[a];
    	h[a] = idx++;
    }
    
    void init()
    {
    	idx = 0;
    	memset(h, -1, sizeof h);
    }
    bool spfa(int s)
    {
        queue<int > q;
    
    	for (int i = 1; i <= n; i ++)
    	{
    		dis[i] = 0;
    		vis[i] = false;
    	}
    
    	q.push(s);
    	vis[s] = true;
    	dis[s] = v;
        
        while (!q.empty())
        {
            int t = q.front();
            q.pop();
            vis[t] = false;
            
            for (int i = h[t]; ~i; i = ne[i])
            {
                int j = e[i];
                if ((dis[t] - w1[i]) * w[i] > dis[j])
                {
                    dis[j] = (dis[t] - w1[i]) * w[i];
                    cnt[j] = cnt[t] + 1;
                    if (cnt[j] >= n) return true;
                    if (!vis[j])
                    {
                        q.push(j);
                        vis[j] = true;
                    }
                }
            }
        }
        
        return false;
    }
    
    int main()
    {
    	memset(h, -1, sizeof h);
    
    	cin >> n >> m >> s >> v;
    
    	while (m --)
    	{
    		int a, b;
    		double hl, yj;
    		cin >> a >> b;
    
    		cin >> hl >> yj;
    		add(a, b, hl, yj);
    
    		cin >> hl >> yj;
    		add(b, a, hl, yj);
    	}
    
    	puts(spfa(s) ? "YES" : "NO");
    
    	//system("pause");
    	return 0;
    }
    
  • 相关阅读:
    程序员最艰难的十大任务
    ssh(安全外壳协议)
    数据库备份 计划任务
    计划任务 crontab
    数据库设计二
    mysql存储过程详解[转]
    Java开发必装的IntelliJ IDEA插件
    SQLYog快捷键大全
    HTTP深入浅出 http请求
    浏览器HTTP请求分析
  • 原文地址:https://www.cnblogs.com/Hayasaka/p/14294126.html
Copyright © 2011-2022 走看看