zoukankan      html  css  js  c++  java
  • Currency Exchange (POJ1860)(判断正圈)(spfa) 最短路专题

    题目描述

    Several currency exchange points are working in our city. Let us suppose that each point specializes in two particular currencies and performs exchange operations only with these currencies. There can be several points specializing in the same pair of currencies. Each point has its own exchange rates, exchange rate of A to B is the quantity of B you get for 1A. Also each exchange point has some commission, the sum you have to pay for your exchange operation. Commission is always collected in source currency.
    For example, if you want to exchange 100 US Dollars into Russian Rubles at the exchange point, where the exchange rate is 29.75, and the commission is 0.39 you will get (100 - 0.39) * 29.75 = 2963.3975RUR.
    You surely know that there are N different currencies you can deal with in our city. Let us assign unique integer number from 1 to N to each currency. Then each exchange point can be described with 6 numbers: integer A and B - numbers of currencies it exchanges, and real RAB, CAB, RBA and CBA - exchange rates and commissions when exchanging A to B and B to A respectively.
    Nick has some money in currency S and wonders if he can somehow, after some exchange operations, increase his capital. Of course, he wants to have his money in currency S in the end. Help him to answer this difficult question. Nick must always have non-negative sum of money while making his operations.

    题目大意:给你n种货币,m种不同货币间相互的汇率和在交换时所需要的手续费。你可以进行多次交换,问你是否可以在交换多次后再换回原来的货币,并且使货币的数量增加。

    解题思路:要得到比原来货币的数量多,只需要在某两种可以交换到的货币间互相交换时货币数量在增加即可,即通过这两种货币的不断交换使货币数量趋向无穷大,这样在交换回原来货币时数量一定增加。用最短路思想,即寻找正环。只需要令dis[ s ]为原来所拥有的货币数量,其余都为0,在每次通过兑换得到货币的大数放入dis中即可 ,在spfa 的队列中,只要寻找到当一个点等于起始点时这是dis [x] 大于 原来拥有的货币量即返回yes。

    代码:

    #include<bits/stdc++.h>
    #define ll long long
    #define MOD 998244353 
    #define INF 0x3f3f3f3f
    #define mem(a,x) memset(a,x,sizeof(a))  
    #define ios ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
    using namespace std;
    
    const int NUM = 205 ;
    struct edge{
       int from,to;
       double r,c;
       edge(int a,int b,double x,double y){from=a;to=b;r=x;c=y;}
    };
    vector<edge>e[NUM];
    int n,m,s,cnt=0;
    int pre[NUM];
    double vv;
    bool spfa(int s)
    {
        double dis[NUM];
        bool inq[NUM];
        for(int i=1;i<=n;i++){dis[i]=0;inq[i]=false;neg[i]=0;}
        dis[s]=vv;
        queue<int>Q;
        Q.push(s);
        inq[s]=true;
        while(!Q.empty())
        {
              int u=Q.front();
              Q.pop();
              if(u==s&&dis[u]>vv)return 1;
              inq[u]=false;
              for(int i=0;i<e[u].size();i++){
                 int v=e[u][i].to;
                 double rr=e[u][i].r,pp=e[u][i].c;
                 double temp=(dis[u]-pp)*rr;
                 if(temp>dis[v]){
                    dis[v]=temp;
                    if(!inq[v]){
                      inq[v]=true;
                      Q.push(v);
                    }
                 }
              }
        }
        return 0;
    }
    int main()
    {
       cin>>n>>m>>s>>vv;
       for(int i=1;i<=m;i++){
          int a,b;
          double rab,rba,pab,pba;
          scanf("%d %d %lf %lf %lf %lf",&a,&b,&rab,&pab,&rba,&pba);
          e[a].push_back(edge(a,b,rab,pab));
          e[b].push_back(edge(b,a,rba,pba));
       }
       if(spfa(s)){
          cout<<"YES"<<endl;
       }else{
          cout<<"NO"<<endl;
       }
       return 0;
    }
    越自律,越自由
  • 相关阅读:
    【Android】【踩坑日记】RecyclerView获取子View的正确姿势
    【Thinking in Java】编写构造器时应注意:尽量避免调用其他非private方法
    为什么匿名内部类只能访问final变量【转】
    【Thinking in Java】类和对象的初始化过程
    【Thinking in Java】Java Callable的使用
    【算法与数据结构】二叉搜索树的Java实现
    【Thinking in Java】组合、继承和代理的区别
    【面试经历】腾讯一二面分享与总结
    【面试经历】腾讯、网易有道和阿里的笔试分享及自我总结
    《Unity預計算即時GI》笔记:三、Clusters和总结
  • 原文地址:https://www.cnblogs.com/ha-chuochuo/p/13435569.html
Copyright © 2011-2022 走看看