Currency Exchange(最短路_Beelman_Ford)

Currency Exchange

Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 20482   Accepted: 7352

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

题意:一个人手中某过的货币n元,他想通过m个兑换点换取别国货币,最后在换成自己的货币,好让自己的货币增长. 
第一行输入n,m,s,money分别代表有n种钱币种类(样例中为1,2,3),m个货币兑换点,你拥有的货币种类,money为你的钱币数(注意不是int类型)
以下m行为u,v,r1,c1,r2,c2为u换取v钱币的兑换率为r1,佣金为c1,v换取u钱币的兑换率为r2,佣金为c2;
思路:找出是否存在正权回路(正权回路:在这一回路上,顶点的权值能不断增加即能一直进行松弛)一种货币就是图上的一个点,一个兑换点就是两个货币之间的一个兑换环权值为(钱数-佣金)*兑换率。
ps:原谅我个渣比好几天才理解题意

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define inf 99999999
struct node
{
    int u,v;
    double r,c;
}edge[220];
int n,m,s;
double money;
double dis[110];//保存最短路径
int cnt;
void add_edge(int uu,int vv,double rr,double cc)//加边操作
{
    edge[cnt].u=uu;
    edge[cnt].v=vv;
    edge[cnt].r=rr;
    edge[cnt].c=cc;
    cnt++;
}
int bellman_ford()
{
    int i,j;
    for(i=1;i<=n;i++)
        dis[i]=0;
    dis[s]=money;
    for(i=1;i<=n;i++)
    {
        int flag=0;
        for(j=0;j<cnt;j++)//对每个边
        {
            if(dis[edge[j].v]<(dis[edge[j].u]-edge[j].c)*edge[j].r)//进行松弛操作
                {
                dis[edge[j].v]=(dis[edge[j].u]-edge[j].c)*edge[j].r;
                flag=1;//松弛操作成功标记为1;
                }

        }
        if(!flag)//若所有的边i有没松弛成功的就退出
            break;
    }
    for(i=0;i<cnt;i++)
    {
        if(dis[edge[i].v]<(dis[edge[i].u]-edge[i].c)*edge[i].r)//进行|V|-1次操作后  有边还能进行松弛  说明
            return 1;//存在正权回路
    }
    return 0;//不存在正权回路
}
int main()
{
    int u,v;
    double r1,c1,r2,c2;
    while(~scanf("%d %d %d %lf",&n,&m,&s,&money))
    {
        cnt=0;
        while(m--)
        {
            scanf("%d %d %lf %lf %lf %lf",&u,&v,&r1,&c1,&r2,&c2);
            add_edge(u,v,r1,c1);
            add_edge(v,u,r2,c2);
        }
        if(bellman_ford())
            printf("YES\n");
        else
            printf("NO\n");
    }
    return 0;
}



时间: 2024-08-10 23:22:22

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

POJ 1860 Currency Exchange(最短路)

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

最短路(Bellman_Ford) POJ 1860 Currency Exchange

题目传送门 1 /* 2 最短路(Bellman_Ford):求负环的思路,但是反过来用,即找正环 3 详细解释:http://blog.csdn.net/lyy289065406/article/details/6645778 4 */ 5 #include <cstdio> 6 #include <iostream> 7 #include <algorithm> 8 #include <cstring> 9 #include <vector>

Dijkstra算法(求解单源最短路)详解 + 变形 之 poj 1860 Currency Exchange

/* 求解单源最短路问题:Dijkstra算法(该图所有边的权值非负) 关键(贪心): (1)找到最短距离已经确定的节点,从它出发更新与其相邻节点的最短距离: (2)此后不再关心(1)中“最短距离已经确定的节点”. 时间复杂度(大概的分析,不准确): “找到最短距离已经确定的节点” => O(|V|) "从它出发更新与其相邻节点的最短距离" => 邻接矩阵:O(|V|),邻接表:O(|E|) 需要循环以上两个步骤V次,所以时间复杂度:O(V^2) 即:在|E|较小的情况下,

Currency Exchange(最短路)

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 curre

POJ 1860 &amp; ZOJ 1544 Currency Exchange(最短路SPFA)

题目链接: ZJU:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1544 PKU: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 currenci

POJ 1860 Currency Exchange (最短路)

Currency Exchange Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 60000/30000K (Java/Other) Total Submission(s) : 4   Accepted Submission(s) : 2 Problem Description Several currency exchange points are working in our city. Let us suppose that

POJ 1860——Currency Exchange——————【最短路、SPFA判正环】

Currency Exchange Time Limit:1000MS     Memory Limit:30000KB     64bit IO Format:%I64d & %I64u Submit Status Practice POJ 1860 Description Several currency exchange points are working in our city. Let us suppose that each point specializes in two par

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

[kuangbin带你飞]专题四 最短路练习 E - Currency Exchange

E - Currency Exchang 题目链接:https://vjudge.net/contest/66569#problem/E 题目: 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 thes