关闭

poj 1860 Currency Exchange

标签: 最短路径spfa算法邻接表
371人阅读 评论(0) 收藏 举报
分类:
Currency Exchange
Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 23914   Accepted: 8660

Description

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.

Input

The first line of the input contains four numbers: N - the number of currencies, M - the number of exchange points, S - the number of currency Nick has and V - the quantity of currency units he has. The following M lines contain 6 numbers each - the description of the corresponding exchange point - in specified above order. Numbers are separated by one or more spaces. 1<=S<=N<=100, 1<=M<=100, V is real number, 0<=V<=103.
For each point exchange rates and commissions are real, given with at most two digits after the decimal point, 10-2<=rate<=102, 0<=commission<=102.
Let us call some sequence of the exchange operations simple if no exchange point is used more than once in this sequence. You may assume that ratio of the numeric values of the sums at the end and at the beginning of any simple sequence of the exchange operations will be less than 104.

Output

If Nick can increase his wealth, output YES, in other case output NO to the output file.

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

spfa算法。。。。。学习之中

#include<iostream>
using namespace std;
#include<string.h>
#include<queue>
int vis[1005],head[1005],n,m,s,f,h[1005];
double val,dis[1005];
struct node
{
    int e,next;
    double com,w,rate;
}edge[1005];
void add(int a,int b,double com,double rate)
{
   edge[f].e=b;
   edge[f].com=com;
   edge[f].rate=rate;
   edge[f].next=head[a];
   head[a]=f++;
}
int spfa(int s)
{
  int i,cur,k;
  queue<int>q;
  vis[s]=1;
  dis[s]=val;
  h[s]=1;
  q.push(s);
  while(!q.empty())
  {
      cur=q.front();
      q.pop();
      vis[cur]=0;
      for(i=head[cur];i!=-1;i=edge[i].next)
      {
          k=edge[i].e;
          edge[i].w=(dis[cur]-edge[i].com)*edge[i].rate-dis[cur];//换一次赚多少钱或赔多少。
          if(dis[k]<dis[cur]+edge[i].w)
          {
              dis[k]=dis[cur]+edge[i].w;
              if(!vis[k])
              {
                  q.push(k);
                  vis[k]=1;
              }
              if(dis[s]>val)//如果再换回s货币时赚了,就返回1.
          {  
              return 1;
          }
          }

      }
  }
  return 0;
}
int main()
{
    int a,b,i;
   double com1,rate1,com2,rate2;
   cin>>n>>m>>s>>val;
     f=0;
     memset(vis,0,sizeof(vis));
     memset(head,-1,sizeof(head));
     memset(h,0,sizeof(h));
     for(i=0;i<1005;i++)
        dis[i]=0;//注意初始化,求的是最大值,所以初始为0;
     for(i=0;i<m;i++)
     {
         cin>>a>>b>>rate1>>com1>>rate2>>com2;
         add(a,b,com1,rate1);
         add(b,a,com2,rate2);
     }
     if(spfa(s))
         cout<<"YES\n";
     else cout<<"NO\n";
    return 0;
}


0
0
查看评论

poj 1860 Currency Exchange (最短路)

Currency Exchange Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 17745   Accepted: 6262 Descript...
  • LYHVOYAGE
  • LYHVOYAGE
  • 2014-02-16 09:30
  • 4451

poj 1860 Currency Exchange(SPFA)

poj 1860 Currency Exchange(SPFA)
  • u012860063
  • u012860063
  • 2014-08-07 08:50
  • 1390

POJ 1860 *** Currency Exchange

题意:有N种货币类型,两种货币之间可以相互兑换。例如:价值V的货币A兑换货币B,需要扣除兑换税Cab,然后乘兑换率Rab才能得到兑换后的货币B,V'=(V-Cab)*Rab。一共有M个兑换点,每个兑换点都只可以兑换两种货币(每个兑换点的数据为A,B,Cab,Rab,Cba,Rba)。当拥有价...
  • treeshy
  • treeshy
  • 2015-11-18 17:00
  • 285

POJ 1860:Currency Exchange

Currency Exchange Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 22648   Accepted: 8180 Descrip...
  • u010885899
  • u010885899
  • 2015-08-06 10:48
  • 254

poj 1860 Currency Exchange

链接:http://poj.org/problem?id=1860 题目大意是有N个国家让你通过一定本钱在各个国家之间通过汇率转换来达到收益的目的。 最短路问题,因为出现了环,所以用SPFA来判断正环,如果出现正环,那么一定可以达到目的,否则不能。 代码: #include #include #in...
  • u011385365
  • u011385365
  • 2014-01-11 17:43
  • 800

POJ 1860 Currency Exchange

bellman_ford判断正环 A - Currency Exchange Time Limit:1000MS     Memory Limit:30000KB    ...
  • u012797220
  • u012797220
  • 2014-01-17 14:58
  • 850

POJ 1860 - Currency Exchange

Currency Exchange Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 18899   Accepted: 6743 Descript...
  • u013497151
  • u013497151
  • 2014-06-11 20:18
  • 830

Poj 1860 Currency Exchange

题目大意 有多种汇币,汇币之间可以交换,这需要手续费,当你用100A币交换B币时,A到B的汇率是29.75,手续费是0.39,那么你可以得到(100 - 0.39) * 29.75 = 2963.3975 B币。问s币的金额经过交换最终得到的s币金额数能否增加 货币的交换是可以重复多次的,所以我们...
  • Detective_Xin
  • Detective_Xin
  • 2012-01-14 16:14
  • 934

POJ 1860 Currency Exchange

我们的城市有几个货币兑换点。让我们假设每一个点都只能兑换专门的两种货币。可以有几个点,专门从事相同货币兑换。每个点都有自己的汇率,外汇汇率的A到B是B的数量你1A。同时各交换点有一些佣金,你要为你的交换操作的总和。在来源货币中总是收取佣金。 例如,如果你想换100美元到俄罗斯卢布兑换点,那里的汇率是...
  • CS33sun
  • CS33sun
  • 2018-01-15 19:08
  • 36

POJ 1860 Currency Exchange

题意:第一排输入n(货币的种类)  m(兑换货币的站点数)   t(你现在拥有的货币是第几类货币)   v(你现在拥有多少该类货币) 下面每行输入的数:a  b   就是a兑换b  rab 兑率  cab (手续费)  ...
  • u012313382
  • u012313382
  • 2014-08-01 11:07
  • 728
    个人资料
    • 访问:54902次
    • 积分:1918
    • 等级:
    • 排名:千里之外
    • 原创:138篇
    • 转载:7篇
    • 译文:0篇
    • 评论:15条
    最新评论