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;
    }
    越自律,越自由
  • 相关阅读:
    增加文章
    网站之注册
    C#常用的引用
    Session.Abandon和Session.Clear有何不同 (转)
    C#文件路径的写法
    UpdatePanel的用法详解
    [转]asp:ScriptManager
    Git 常用命令
    AJAX请求 $.post方法的使用
    a 标签中调用js的几种方法
  • 原文地址:https://www.cnblogs.com/ha-chuochuo/p/13435569.html
Copyright © 2011-2022 走看看