poj 1860Currency Exchange(bellman 最短路)
来源:互联网 发布:酒店网络布线方案 编辑:程序博客网 时间:2024/04/29 02:56
Currency ExchangeTime Limit:1000MS Memory Limit:30000KB 64bit IO Format:%I64d & %I64u
Appoint description:
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 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.
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<=103.
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.
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.01 2 1.00 1.00 1.00 1.002 3 1.10 1.00 1.10 1.00
Sample Output
YES
#include<stdio.h>#include<string.h>struct node{ int a,b; double rat,cos;}p[1000];double dis[1000];int m,n,s,ss;double v;bool Bellman_Ford(){ int i,j,flag; memset(dis,0,sizeof(dis)); dis[s]=v; int x=0; for(i=1; i<=n; i++) { flag=0; for( j = 0; j < ss; j++) { int a=p[j].a,b=p[j].b; double r=p[j].rat,c=p[j].cos; if(dis[b] < (dis[a] - c) * r) { dis[b] = (dis[a] - c) * r; flag=1; } }if(flag) x++; } flag=0; if(x==n) flag=1; return flag;}int main(){ int i,j; while(~scanf("%d%d%d%lf",&n,&m,&s,&v)) { int a,b; double rab,cab,rba,cba; ss=0; for(i=0;i<m;i++) { scanf("%d%d%lf%lf%lf%lf",&a,&b,&rab,&cab,&rba,&cba); p[ss].a=a; p[ss].b=b; p[ss].rat=rab; p[ss++].cos=cab; p[ss].a=b; p[ss].b=a; p[ss].rat=rba; p[ss++].cos=cba; } if(Bellman_Ford()) printf("YES\n"); else printf("NO\n"); }return 0;}
0 0
- poj 1860Currency Exchange(bellman 最短路)
- POJ 1860 Currency Exchange(最短路 Bellman-Ford)
- poj 1860 Currency Exchange (最短路,Bellman-Ford算法)
- PKU 1860 Currency Exchange 最短路 bellman
- POJ 1860:Currency Exchange:bellman最短路变形进行盈利判环
- poj 1860 -- Currency Exchange(最短路)
- poj 1860 Currency Exchange (最短路)
- POJ 1860 Currency Exchange(最短路)
- POJ 1860 Currency Exchange(最短路SPFA)
- POJ 1860Currency Exchange BFS最短路
- POJ - 1860 Currency Exchange(最短路)
- poj 1860 Currency Exchange(最短路)
- POJ 1860 Currency Exchange(最短路)
- POJ-1860-Currency Exchange [最短路][BellmanFord]
- POJ1860 Currency Exchange 最短路bellman-ford
- POJ 1860 Currency Exchange Bellman
- poj 1860 Currency Exchange (bellman)
- poj-1860-Currency Exchange-Bellman
- java 选择排序
- (六)ArcGIS Server之发布GP服务-返回矢量数据
- HTML DOM 对象
- bzoj1217: [HNOI2003]消防局的设立
- C语言--排序算法
- poj 1860Currency Exchange(bellman 最短路)
- 软件工程
- HDU3342-Legal or Not
- hdu5875(取模的性质,线段树,二分)
- openCV3.1.0与 openCV2.4.13的安装配置 (基于VS2015)
- 块设备驱动程序
- NanoPi M2 gpio驱动编写
- JAVA进阶 面向对象程序设计——第1周 类与对象(自己定义类,然后用自己定义的类来创建对象)
- 三角形外接圆与内切圆