poj—— 1860 Currency Exchange
Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 29851   Accepted: 11245

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

题目大意:

题目中主要是说存在货币兑换点,然后现在手里有一种货币,要各种换来换去,最后再换回去的时候看能不能使原本的钱数增多,每一种货币都有对应的汇率,而货币A到货币B的汇率即为1货币A换得得货币B的数量,但兑换点是要收取佣金的,且佣金从源货币中扣除,例如,你想在汇率29.75,佣金为0.39的兑换点把100美元换成卢布,得到的卢布数即为(100-0.39)*29.75 = 2963.3975.

样例解释:

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

多组输入,第一行中N代表有N种货币可以互相兑换,M代表有M个货币兑换点,S代表这个人手中的的货币的编号,V代表这个人手中拥有的货币数量,底下M行

每行六个数,A,B代表可以交换的货币A和B,剩下的实数RAB,CAB,RBA,CBA,代表A到B的汇率,佣金,B到A的汇率,佣金。以某种兑换方式增加原本的钱数,而且必须兑换为原来的货币。

 
思路:
货币的交换是可以重复多次的,所以我们需要找出是否存在正权回路,且最后得到的s金额是增加的。
这一道题虽然需要求的是正权回路,但相应的这道题当中我们需要求的是最长路径,因此和Bellman-Ford算法中判断负环是类似的。
因此,我们只需要修改原本的松弛条件,然后先进行n-1轮松弛,最后再进行一次松弛作为检测存不存在正环就可以了。
 
链接地址:(参考博客)   http://blog.csdn.net/dgghjnjk/article/details/51684154
代码:
#include<queue>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#define N 1010
using namespace std;
int n,m,head[N],sum[N],tot,num,u,v;
bool vis[N];
double dis[N],x,y,xx,yy,money;
struct Edge
{
    int u,v,next;
    double x,y;
}edge[N<<];
int add(int u,int v,double x,double y)//注意:传上来的是double类型的,开始时一直传的是int导致wa
{
    tot++;//从1开始用,开始时一直是在后面++,导致一直wa
    edge[tot].u=u;
    edge[tot].v=v;
    edge[tot].x=x;
    edge[tot].y=y;
    edge[tot].next=head[u];
    head[u]=tot;
}
void begin()
{
    memset(head,,sizeof(head));
    memset(sum,,sizeof(sum));
    memset(dis,,sizeof(dis));
    memset(vis,false,sizeof(vis));
    tot=;
}
int spfa(int s)
{
    queue<int>q;
    dis[s]=money;
    vis[s]=true;
    q.push(s);
    while(!q.empty())
    {
        int x=q.front();
        q.pop();vis[x]=false;
        for(int i=head[x];i;i=edge[i].next)
        {
            int v=edge[i].v;
            if(dis[v]<(dis[x]-edge[i].y)*edge[i].x)
            {
                dis[v]=(dis[x]-edge[i].y)*edge[i].x;
                if(!vis[v])
                {
                    vis[v]=true;
                    q.push(v);
                }
                sum[v]++;
                if(sum[v]>n)
                 ;
            }
        }
    }
    ;
}
int main()
{
    while(scanf("%d %d %d %lf",&n,&m,&num,&money)!=EOF)
    {
        begin();
        ;i<=m;i++)
        {
            scanf("%d %d %lf %lf %lf %lf",&u,&v,&x,&y,&xx,&yy);
            add(u,v,x,y);
            add(v,u,xx,yy);
        }
        ) printf("NO\n");
        else printf("YES\n");
    }
    ;
}

Currency Exchange(最短路)的更多相关文章

  1. POJ 1860 Currency Exchange 最短路+负环

    原题链接:http://poj.org/problem?id=1860 Currency Exchange Time Limit: 1000MS   Memory Limit: 30000K Tota ...

  2. POJ 1860 Currency Exchange (最短路)

    Currency Exchange Time Limit:1000MS     Memory Limit:30000KB     64bit IO Format:%I64d & %I64u S ...

  3. poj 1860 Currency Exchange (最短路bellman_ford思想找正权环 最长路)

    感觉最短路好神奇呀,刚开始我都 没想到用最短路 题目:http://poj.org/problem?id=1860 题意:有多种从a到b的汇率,在你汇钱的过程中还需要支付手续费,那么你所得的钱是 mo ...

  4. POJ 1860 Currency Exchange 最短路 难度:0

    http://poj.org/problem?id=1860 #include <cstdio> //#include <queue> //#include <deque ...

  5. POJ1860 Currency Exchange(最短路)

    题目链接. 分析: 以前没做出来,今天看了一遍题竟然直接A了.出乎意料. 大意是这样,给定不同的金币的编号,以及他们之间的汇率.手续费,求有没有可能通过不断转换而盈利. 直接用Bellman-ford ...

  6. 最短路(Bellman_Ford) POJ 1860 Currency Exchange

    题目传送门 /* 最短路(Bellman_Ford):求负环的思路,但是反过来用,即找正环 详细解释:http://blog.csdn.net/lyy289065406/article/details ...

  7. POJ 1860 Currency Exchange (最短路)

    Currency Exchange Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 60000/30000K (Java/Other) T ...

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

    Currency Exchange DescriptionSeveral currency exchange points are working in our city. Let us suppos ...

  9. POJ1860 Currency Exchange【最短路-判断环】

    Several currency exchange points are working in our city. Let us suppose that each point specializes ...

  10. (最短路 SPFA)Currency Exchange -- poj -- 1860

    链接: http://poj.org/problem?id=1860 Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 2326 ...

随机推荐

  1. sscanf函数详解

    #if 0 ,sscanf():从一个字符串中读进与指定格式相符的数据. ,sscanf与scanf类似,都是用于输入的,只是后者以屏幕(stdin)为输入源,前者以固定字符串为输入源. ,关于正则表 ...

  2. Java语言的特点和特性

    1. Java语言的主要特点: 1. 跨平台性 所谓的跨平台性,是指软件可以不受计算机硬件和操作系统的约束而在任意计算机环境下正常运行.这是软件发展的趋势和编程人员追求的目标.之所以这样说,是因为计算 ...

  3. 如何用纯 CSS 创作一个蝴蝶标本展示框

    效果预览 在线演示 按下右侧的"点击预览"按钮可以在当前页面预览,点击链接可以全屏预览. https://codepen.io/comehope/pen/xzgZzQ 可交互视频教 ...

  4. 二段式fsm

    1.推荐在敏感表下的默认状态为X,这样描述的好处有2个: 好处1:仿真易观察bug. 好处2:综合对不定态X的处理是"Don't Care",即任何没有定义的状态寄存器向量都会被忽 ...

  5. 时间格式的处理和数据填充和分页---laravel

    时间格式文档地址:http://carbon.nesbot.com/docs/ 这是些时间格式,只需要我们这么做就可以 我们在模板层,找到对应的模型对象那里进行处理就可以啦 2018-11-08 16 ...

  6. 解决windows管理员已阻止你运行此应用问题

    按WIN+R键,打开“运行”,然后输入“gpedit.msc",就是打开组策略,这个在控制面板中也可以打开. 在组策略里找到“计算机配置”-“Windows设置”-“安全设置”-“本地策略” ...

  7. 163 AJAX

    // 163 AJAX Tab // update 2006.10.18 // 增加鼠标延迟感应特性. // update 2006.10.8 // A 标签 href 属性将保持原有HTML功能.增 ...

  8. HDU-4612 Warm up,tarjan求桥缩点再求树的直径!注意重边

    Warm up 虽然网上题解这么多,感觉写下来并不是跟别人竞争访问量的,而是证明自己从前努力过,以后回头复习参考! 题意:n个点由m条无向边连接,求加一条边后桥的最少数量. 思路:如标题,tarjan ...

  9. HDU-4825 Xor Sum,字典树好题!

    Xor Sum 一遍A了之后大呼一声好(keng)题!debug了两小时~~~~百度之星资格赛,可以. 题意:给你一个n个元素的数组,m次查询,每次输入一个数k要求从数组中找到一个数与k异或值最大,输 ...

  10. EasyUI 动态生成列加分页

    <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat=&qu ...