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++程序设计
    类与类之间的两种关系------新标准c++程序设计
    复制构造函数被调用的三种情况------新标准c++程序设计
    Dynamics CRM2011 隐藏sub-grid 新建项和添加现有项按钮
    Dynamics CRM Odata QueryUrl中的SetName问题
    Dynamics CRM 修改自定义实体名字及属性前缀(架构名称)
    Dynamics CRM 请求服务时报access is denied错误
    Dynamics CRM2011中通过JS脚本方式显示和隐藏ribbon中的自定义按钮
    (转载)表服务器无法打开与报表服务器数据库的连接。所有请求和处理都要求与数据库建立连接。
    如何将sqlserver的windows验证模式改为SQL Server 和 Windows 混合身份验证模式
  • 原文地址:https://www.cnblogs.com/ha-chuochuo/p/13435569.html
Copyright © 2011-2022 走看看