Currency Exchange
Time Limit: 1000MS | Memory Limit: 30000K | |
Total Submissions: 17890 | Accepted: 6314 |
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.
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.
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(1 ~ 100),M(1 ~ 100),S(1 ~ N),V(0 ~ 1000),分别代表有 N 种钱币,M 条钱币关系,当前所拥有的钱币种类 S,当前拥有钱币种类 S 的总值。后给出 M 跳关系,每条关系共有六个数,首先给出 A,B,代表这是A和B直接的关系。后有四个数 Rab,Cab,Rba,Cba,分别代表前者兑换后者的比率和税制,比如A要换成B的话,则 B = (A - Cab) * Rab。问是否有方法可以使最初拥有的S钱币升值,即使拥有S的钱币大于V。
思路:
最短路。Bellman Ford 判断是否有正环,时间条件满足迭代的写法。
理解题目理解很久,Let us call some sequence of the exchange operations simple if no exchange point is used more than once in this sequence ,因为这句话以为每个节点最多只能用 1 次,于是死活分析不出样例,后来才知道可以重复运用多次后使之升值。后来理解题目之后还是没有想到用Bellman Ford,竟然把 Dijkstra 当成 SPFA 来用虽然AC了,但是代码略丑,补上 Bellman Ford 简洁的迭代做法。
AC:
Once:
#include <cstdio> #include <queue> #include <utility> #include <iostream> #include <vector> #include <string.h> #define MAX 300 using namespace std; typedef pair<double,int> pii; typedef struct { double r,c; }edge; edge w[MAX]; double d[105],mon; int fir[105],v[MAX],next[MAX],vis[105],time[105]; int n,m,s,ind; double calculate(double mon,edge a) { return (mon - a.c) * a.r; } void add_edge(int f,int t,double r,double c) { v[ind] = t; w[ind].r = r; w[ind].c = c; next[ind] = fir[f]; fir[f] = ind; ind++; } int Dijkstra() { memset(d,0,sizeof(d)); memset(vis,0,sizeof(vis)); memset(time,0,sizeof(time)); d[s] = mon; priority_queue<pii,vector<pii>,greater<pii> > q; q.push(make_pair(d[s],s)); time[s]++; while(!q.empty()) { pii k = q.top();q.pop(); int x = k.second; vis[x] = 0; for(int e = fir[x];e != -1;e = next[e]) { int y = v[e]; double res = calculate(d[x],w[e]); if(d[y] < res) { d[y] = res; if(!vis[y]) { q.push(make_pair(d[y],y)); time[y]++; vis[y] = 1; if(time[y] > n) return 1; } } } } return 0; } int main() { scanf("%d%d%d%lf",&n,&m,&s,&mon); ind = 0; memset(fir,-1,sizeof(fir)); while(m--) { int a,b; double r,c; scanf("%d%d%lf%lf",&a,&b,&r,&c); add_edge(a,b,r,c); scanf("%lf%lf",&r,&c); add_edge(b,a,r,c); } if(Dijkstra()) puts("YES"); else puts("NO"); return 0; }
Twice:
#include <stdio.h> #include <string.h> #define MAX 500 typedef struct { double r,c; }node; node w[MAX]; int u[MAX],v[MAX]; double d[205]; int n,ind; void add_edge(int f,int t,double r,double c) { u[ind] = f; v[ind] = t; w[ind].r = r; w[ind].c = c; ind++; } double cal(double x,node a) { return (x - a.c) * a.r; } int Bellman_ford(int s,double mon) { memset(d,0,sizeof(d)); d[s] = mon; for(int i = 1;i < n;i++) for(int j = 0;j < ind;j++){ int x = u[j],y = v[j]; double res = cal(d[x],w[j]); if(d[x] > 0 && d[y] < res) d[y] = res; } for(int j = 0;j < ind;j++) { int x = u[j],y = v[j]; double res = cal(d[x],w[j]); if(d[y] < res) return 1; } return 0; } int main() { int m,s; double mon; scanf("%d%d%d%lf",&n,&m,&s,&mon); ind = 0; while(m--) { int a,b; double r,c; scanf("%d%d%lf%lf",&a,&b,&r,&c); add_edge(a,b,r,c); scanf("%lf%lf",&r,&c); add_edge(b,a,r,c); } if(Bellman_ford(s,mon)) puts("YES"); else puts("NO"); return 0; }
相关推荐
解决了dijkstra算法不能计算负权图最短路问题,当然对有负回路的图同样没有办法,呵呵
图论- 最短路- Bellman-Ford 算法与 SPFA.rar
bellman ford 算法, 可以手动输入边数和节点数
Bellman-Ford模板 Bellman-Ford模板 Bellman-Ford模板 Bellman-Ford模板 Bellman-Ford模板
北大POJ1860-Currency Exchange【Bellman】 解题报告+AC代码
最短路径算法—Bellman-Ford(贝尔曼-福特)算法分析与实现(CC++),希望对你能有所帮助!
这里面的内容是个PPT,介绍的很好,如果你想更加的清楚 SPFA 和Bellman_ford.ppt 最短路算法的原理,这是个不错的选择
bellman-ford算法的C++实现,邻接表
Bellman-Ford算法与差分约束系统
C#,图论与图算法,有向图单源最短路径的贝尔曼·福特(Bellman Ford)算法与源代码 贝尔曼·福特(Bellman Ford)算法是一种用于计算带权有向图中单源最短路径(SSSP:Single-Source Shortest Path)的算法。该算法...
Bellman-Ford——解决负权边
Neural Bellman-Ford Networks: A General Graph Neural Network Framework for Link Prediction 【NeurIPS 2021】神经Bellman-Ford网络:用于链路预测的一般图神经网络框架 链接预测是图的一项非常基础的任务。在...
Bellman Ford SPFA 算法的相关资料~~~~
基于Bellman-Ford最短路径算法的演示程序 提供源代码
使用C++实现的Queue improved Bellman-Ford单源最短路算法,在国内还被叫做SPFA。这个程序输入一个图,找到图中的一个点,这个点到最远点的长度最短。图使用邻接表保存。
bellman-Ford算法的具体实现,用c++的语法实现,简单易懂。
经典算法书籍《算法导论》之图算法章节中Bellman-ford算法的C++版。
分步介绍了bellman-ford算法的详细步骤和分析方法,最后给出了例题进行了说明
Java实现的Dijkstra算法和Bellman-ford算法代码,带UI界面,已封装的可执行jar文件,以及源代码。