poj1860 bellman—ford队列优化 Currency Exchange

Currency Exchange

Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 22123   Accepted: 7990

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

Source

解析

题意:

有多种汇币,汇币之间可以交换,这需要手续费,当你用100A币交换B币时,A到B的汇率是29.75,手续费是0.39,那么你可以得到(100 - 0.39) * 29.75 = 2963.3975 B币。问s币的金额经过交换最终得到的s币金额数能否增加

货币的交换是可以重复多次的,所以我们需要找出是否存在正权回路,且最后得到的s金额是增加的

怎么找正权回路呢?(正权回路:在这一回路上,顶点的权值能不断增加即能一直进行松弛)

分析:

一种货币就是一个点

一个“兑换点”就是图上两种货币之间的一个兑换方式,是双边,但A到B的汇率和手续费可能与B到A的汇率和手续费不同。

唯一值得注意的是权值,当拥有货币A的数量为V时,A到A的权值为K,即没有兑换

而A到B的权值为(V-Cab)*Rab

本题是“求最大路径”,之所以被归类为“求最小路径”是因为本题题恰恰与bellman-Ford算法的松弛条件相反,求的是能无限松弛的最大正权路径,但是依然能够利用bellman-Ford的思想去解题。

因此初始化dis(S)=V   而源点到其他点的距离(权值)初始化为无穷小(0),当s到其他某点的距离能不断变大时,说明存在最大路径;如果可以一直变大,说明存在正环。判断是否存在环路,用Bellman-Ford和spfa都可以。

spfa算法:

 1 #include<stdio.h>
 2 #include<string.h>
 3 #include<iostream>
 4 #include<queue>
 5 #include<algorithm>
 6 using namespace std;
 7 double cost[105][105],rate[105][105];
 8 int n,vis[105];
 9 double v,dis[105];
10 bool bellman_ford(int start){
11    memset(dis,0,sizeof(dis));
12    memset(vis,0,sizeof(vis));
13    dis[start]=v;
14    queue<int>q;
15    q.push(start);
16    vis[start]=1;
17    while(!q.empty()){
18       int x=q.front();
19       q.pop();
20       vis[x]=0;
21       for(int i=1;i<=n;i++){
22          if(dis[i]<(dis[x]-cost[x][i])*rate[x][i]){
23              dis[i]=(dis[x]-cost[x][i])*rate[x][i];
24              if(dis[start]>v)
25              return true;
26              if(!vis[i]){
27              q.push(i);
28                vis[i]=1;
29              }
30          }
31       }
32    }
33    return false;
34 }
35 int main(){
36    int m,s;
37    while(scanf("%d%d%d%lf",&n,&m,&s,&v)!=EOF){
38        memset(cost,0,sizeof(vis));
39        memset(rate,0,sizeof(rate));
40
41        for(int i=1;i<=n;i++){
42           for(int j=1;j<=n;j++)
43           if(i==j)
44           rate[i][j]=1.0;
45        }
46        int x,y;
47        double rab,rba,cab,cba;
48        for(int i=1;i<=m;i++){
49           cin>>x>>y>>rab>>cab>>rba>>cba;
50           cost[x][y]=cab;
51           cost[y][x]=cba;
52           rate[x][y]=rab;
53           rate[y][x]=rba;
54        }
55        if(bellman_ford(s))
56        printf("YES\n");
57        else  printf("NO\n");
58    }
59    return 0;
60 }
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<queue>
#include<algorithm>
using namespace std;
double cost[105][105],rate[105][105];
int n,vis[105];
double v,dis[105];
bool bellman_ford(int start){
   memset(dis,0,sizeof(dis));
   memset(vis,0,sizeof(vis));
   dis[start]=v;
   queue<int>q;
   q.push(start);
   vis[start]=1;
   while(!q.empty()){
      int x=q.front();
      q.pop();
      vis[x]=0;
      for(int i=1;i<=n;i++){
         if(dis[i]<(dis[x]-cost[x][i])*rate[x][i]){
             dis[i]=(dis[x]-cost[x][i])*rate[x][i];
             if(dis[start]>v)
             return true;
             if(!vis[i]){
             q.push(i);
               vis[i]=1;
             }
         }
      }
   }
   return false;
}
int main(){
   int m,s;
   while(scanf("%d%d%d%lf",&n,&m,&s,&v)!=EOF){
       memset(cost,0,sizeof(vis));
       memset(rate,0,sizeof(rate));

       for(int i=1;i<=n;i++){
          for(int j=1;j<=n;j++)
          if(i==j)
          rate[i][j]=1.0;
       }
       int x,y;
       double rab,rba,cab,cba;
       for(int i=1;i<=m;i++){
          cin>>x>>y>>rab>>cab>>rba>>cba;
          cost[x][y]=cab;
          cost[y][x]=cba;
          rate[x][y]=rab;
          rate[y][x]=rba;
       }
       if(bellman_ford(s))
       printf("YES\n");
       else  printf("NO\n");
   }
   return 0;
}

  

时间: 2024-11-10 10:36:15

poj1860 bellman—ford队列优化 Currency Exchange的相关文章

POJ 1860 Currency Exchange (Bellman ford)

Currency Exchange Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 22405   Accepted: 8095 Description Several currency exchange points are working in our city. Let us suppose that each point specializes in two particular currencies and pe

POJ1860——Currency Exchange(BellmanFord算法求最短路)

Currency Exchange DescriptionSeveral 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 sp

poj1860——Currency Exchange(Eellman-Ford+权值为正的环路)

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

Currency Exchange(POJ1860)

我们的城市有几个货币兑换点.让我们假设每一个点都只能兑换专门的两种货币.可以有几个点,专门从事相同货币兑换.每个点都有自己的汇率,外汇汇率的A到B是B的数量你1A.同时各交换点有一些佣金,你要为你的交换操作的总和.在来源货币中总是收取佣金. 例如,如果你想换100美元到俄罗斯卢布兑换点,那里的汇率是29.75,而佣金是0.39,你会得到(100 - 0.39)×29.75=2963.3975卢布. 你肯定知道在我们的城市里你可以处理不同的货币.让每一种货币都用唯一的一个小于N的整数表示.然后每个

POJ1860 Currency Exchange【BellmanFord算法】【求正权回路】

Currency Exchange Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 20994 Accepted: 7522 Description Several currency exchange points are working in our city. Let us suppose that each point specializes in two particular currencies and perfor

POJ1860:Currency Exchange(BF)

http://poj.org/problem?id=1860 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 sev

POJ-1860 Currency Exchange 【spfa判负环】

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

POJ-1860 Currency Exchange( Bellman_Ford, 正环 )

题目链接:http://poj.org/problem?id=1860 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 b

Bellman_ford 算法 Currency Exchange POJ1860

Bellman_ford算法用于寻找正环或者负环! 算法导论: 24.1 The Bellman-Ford algorithm The Bellman-Ford algorithm solves the single-source shortest-paths problem in the general case in which edge weights may be negative. Given a weighted, directed graph G = (V, E) with sou