[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 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 R
AB, C
AB, R
BA and C
BA - 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<=10
3.

For each point exchange rates and commissions are real, given with at most two digits after the decimal point, 10
-2<=rate<=10
2, 0<=commission<=10
2.

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 10
4.

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种货币可以相互转换,之间有汇率和手续费,问是否经过转换点的转换使得货币增多思路:判断图是否存在负权环,用spfa算法可以高效判断是否存在负权环,只要发现一个点入队列次数大于n,则就存在负权环使得货币增加,因为一个点的入队次数一定小于等于(n-1)

#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
const int maxn=1005;
int book[maxn],head[maxn],cnt[maxn];
int pos,n,m,s;
double d[maxn],v;
struct Edge{
    int to,next;
    double r,c;
}edge[maxn];
void add(int a,int b,double r,double c)
{
    edge[pos].to=b;
    edge[pos].r=r;
    edge[pos].c=c;
    edge[pos].next=head[a];
    head[a]=pos++;
}
bool spfa()
{
    memset(book,0,sizeof(book));
    memset(d,0,sizeof(d));
    memset(cnt,0,sizeof(cnt));
    queue<int>qu;
    qu.push(s);
    book[s]=1;
    d[s]=v;
    cnt[s]++;
    while(!qu.empty())
    {
        int now=qu.front();
        qu.pop();
        book[now]=0;
        for(int i=head[now];i!=-1;i=edge[i].next)
        {
            int toto=edge[i].to;
            double rr=edge[i].r;
            double cc=edge[i].c;
            if(d[toto]<(d[now]-cc)*rr)
            {
                d[toto]=(d[now]-cc)*rr;
                if(!book[toto])
                {
                    book[toto]=1;
                    cnt[toto]++;
                    qu.push(toto);
                }
                if(cnt[toto]>n)
                    return true;
            }
        }
    }
    return false;
}
int main()
{
    scanf("%d%d%d%lf",&n,&m,&s,&v);
    pos=0;
    memset(head,-1,sizeof(head));
    int a,b;
    double r,c;
    for(int i=0;i<m;i++)
    {
        scanf("%d%d%lf%lf",&a,&b,&r,&c);
        add(a,b,r,c);
        scanf("%lf%lf",&r,&c);
        add(b,a,r,c);
    }
    if(spfa())
        printf("YES\n");
    else
        printf("NO\n");
    return 0;
}


原文地址:https://www.cnblogs.com/Vampire6/p/11215515.html

时间: 2024-10-05 19:57:31

[kuangbin带你飞]专题四 最短路练习 E - Currency Exchange的相关文章

[kuangbin带你飞]专题四 最短路练习

A. POJ 2387  Til the Cows Come Home 模板题. #include<iostream> #include<stdio.h> #include<string.h> #include<math.h> #include<algorithm> #include<vector> #include<string> #include<map> #include<queue> #in

【算法系列学习】Dijkstra单源最短路 [kuangbin带你飞]专题四 最短路练习 A - Til the Cows Come Home

https://vjudge.net/contest/66569#problem/A http://blog.csdn.net/wangjian8006/article/details/7871889 邻接矩阵实现的单源最短路 1 #include<iostream> 2 #include<cstdio> 3 #include<string> 4 #include<cstring> 5 #include<algorithm> 6 #include

[ An Ac a Day ^_^ ][kuangbin带你飞]专题四 最短路练习 POJ 2240 Arbitrage spfa求负环

题意就是问倒腾外币能不能升值 不用spfa 用其他的最短路算法也可以 松弛条件换成dist[v]<dist[u]*e[u][i].value 当然 貌似只有spfa有这个坑…… 有A  (value>1.0) A 这种情况……我的天 用Dij Floyd都只用判断如果松弛到了自己 那么一定有环 直接跳出就行 1 #include<cstdio> 2 #include<iostream> 3 #include<algorithm> 4 #include<

[ An Ac a Day ^_^ ] [kuangbin带你飞]专题四 最短路练习 POJ 2387 Til the Cows Come Home

求1到N的最短路 注意有重边 跑一遍dijkstra就行 1 /* *********************************************** 2 Author :SunYuefeng 3 Created Time :2016/10/22 14:18:06 4 File Name :A.cpp 5 ************************************************ */ 6 7 #include<cstdio> 8 #include<io

[kuangbin带你飞]专题四 最短路练习 POJ 2253 Frogger

求第一个点到第二个点的所有通路上最长的边 dijkstra的变形 每次松弛的是每条边通路上的的最长的边 WA了好几次是因为用了%lf 改成%f就过了…… 1 /* *********************************************** 2 Author :SunYuefeng 3 Created Time :2016/10/22 14:18:06 4 File Name :A.cpp 5 ******************************************

[kuangbin带你飞]专题四 最短路练习 F - Wormholes (判断负环)

F - Wormholes 题目链接:https://vjudge.net/contest/66569#problem/F 题目: While exploring his many farms, Farmer John has discovered a number of amazing wormholes. A wormhole is very peculiar because it is a one-way path that delivers you to its destination

[kuangbin带你飞]专题四 最短路练习 I - Arbitrage(判断负环)

I - Arbitrage 题目链接:https://vjudge.net/contest/66569#problem/I 题目: Arbitrage is the use of discrepancies in currency exchange rates to transform one unit of a currency into more than one unit of the same currency. For example, suppose that 1 US Dollar

kuangbin带你飞专题一 简单搜索 题解

目录 [kuangbin带你飞]专题一 简单搜索 [kuangbin带你飞]专题一 简单搜索 总结:用时2天半终于把这个专题刷完了 对于最基础的dfs bfs 路径打印 状态转移也有了一点自己些微的理解 其实2天半可以压缩到1天半的 主要是自己太懒了...慢慢加油刷bin神的专题呀 从大二下学期开始学算法 一开始就知道这个专题 一开始对于这个专题里的所有问题感觉都好难啊..就直接放弃了 看lrj的书 现在看到这个专题还挺唏嘘的吧 突然觉得思维和想法也不是很难 果然是那个时候心不静&还是储量不够吗

[kuangbin带你飞]专题十六 KMP &amp; 扩展KMP &amp; Manacher :G - Power Strings POJ - 2406(kmp简单循环节)

[kuangbin带你飞]专题十六 KMP & 扩展KMP & Manacher G - Power Strings POJ - 2406 题目: Given two strings a and b we define a*b to be their concatenation. For example, if a = "abc" and b = "def" then a*b = "abcdef". If we think of