换钱问题:
给出n种钱,m个站点,现在有第 s种钱,身上有v 这么多;
下面 m行 站点有a,b两种钱,rab a->b的汇率,cab a-->b的手续费, 相反rba cba ; 
问在10次之内,能不能把钱换的变的多了?
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

方法1:用Bellman算法,若存在正权回路,则说明货币经转化是越来越大的,即满足题意。

     所以,这道题就是在让你求回路中是否存在正权回路。

#include "stdio.h"
#include "string.h"
#include "algorithm"
using namespace std;
int n,m,x,flag,u[220],v[220];
double y,dis[220],r1[220],c1[220],r2[220],c2[220];
void djk()
{
for(int i=1;i<n;i++)
{
int ans=0;
for(int j=1;j<=m;j++)
{
if(dis[v[j]]<(dis[u[j]]-c1[j])*r1[j])
{
dis[v[j]]=(dis[u[j]]-c1[j])*r1[j];
ans++;
}
if(dis[u[j]]<(dis[v[j]]-c2[j])*r2[j])
{
dis[u[j]]=(dis[v[j]]-c2[j])*r2[j];
ans++;
}
}
if(ans==0) break;
}
flag=0;
for(int j=1;j<=m;j++)
{
if(dis[v[j]]<(dis[u[j]]-c1[j])*r1[j])
{
flag=1;
break;
}
if(dis[u[j]]<(dis[v[j]]-c2[j])*r2[j])
{
flag=1;
break;
}
}
}
int main()
{
while(~scanf("%d%d%d%lf",&n,&m,&x,&y))
{
memset(dis,0,sizeof(dis));
dis[x]=y;
for(int i=1;i<=m;i++)
scanf("%d%d%lf%lf%lf%lf",&u[i],&v[i],&r1[i],&c1[i],&r2[i],&c2[i]);
djk();
if(flag) printf("YES\n");
else printf("NO\n");
}
}

方法2:

  SPFA方法:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#include<queue>
#define ll long long
#define INF 0x3f3f3f
using namespace std;
typedef pair<int,int> P;
const int maxn = 1e2+10;
int n,m,s,tot;
double v;
double dis[maxn];
int head[maxn],vis[maxn];
double cost[maxn][maxn],rate[maxn][maxn];
struct node
{
int to;
double w;
int next;
} edge[maxn*maxn]; void add(int u,int v)
{
edge[tot].to = v;
edge[tot].next = head[u];
head[u] = tot++;
} bool SPFA(int s,double v)
{
for(int i=1; i<=n; i++)
{
dis[i] = 0;
vis[i] = 0;
}
dis[s] = v;
vis[s] = 1;
queue<int> q;
q.push(s);
while(!q.empty())
{
int x = q.front();
q.pop();
vis[x] = 0;
for(int i=head[x]; i!=-1; i=edge[i].next)
{
int j = edge[i].to;
if(dis[j]<(dis[x]-cost[x][j])*rate[x][j])
{
dis[j] = (dis[x]-cost[x][j])*rate[x][j];
if(dis[s]>v)
return true;
if(!vis[j])
{
q.push(j);
vis[j] = 1;
}
}
}
}
return false;
} int main()
{
while(cin>>n>>m>>s>>v)
{
int i;
memset(cost,0,sizeof(cost));
memset(rate,0,sizeof(rate));
memset(head,-1,sizeof(head));
tot = 1;
for(i=0; i<m; i++)
{
int a,b;
double cab,rab,cba,rba;
scanf("%d%d%lf%lf%lf%lf",&a,&b,&rab,&cab,&rba,&cba);
cost[a][b] = cab;
cost[b][a] = cba;
rate[a][b] = rab;
rate[b][a] = rba;
add(a,b);
add(b,a);
}
if(SPFA(s,v))
printf("YES\n");
else
printf("NO\n");
}
return 0;
}

C - C(换钱问题)的更多相关文章

  1. 算法进阶面试题07——求子数组的最大异或和(前缀树)、换钱的方法数(递归改dp最全套路解说)、纸牌博弈、机器人行走问题

    主要讲第五课的内容前缀树应用和第六课内容暴力递归改动态规划的最全步骤 第一题 给定一个数组,求子数组的最大异或和. 一个数组的异或和为,数组中所有的数异或起来的结果. 简单的前缀树应用 暴力方法: 先 ...

  2. [DP]换钱的方法数

    题目三 给定数组arr, arr中所有的值都为整数且不重复.每个值代表一种面值的货币,每种面值的货币可以使用任意张,在给定一个整数aim代表要找的钱数,求换钱有多少种方法. 解法一 --暴力递归 用0 ...

  3. OptimalSolution(1)--递归和动态规划(2)矩阵的最小路径和与换钱的最少货币数问题

    一.矩阵的最小路径和 1 3 5 9 1 4 9 18 1 4 9 18 8 1 3 4 9 9 5 8 12 5 0 6 1 14 14 5 11 12 8 8 4 0 22 22 13 15 12 ...

  4. [程序员代码面试指南]递归和动态规划-换钱的方法数(DP,完全背包)

    题目描述 给定arr,arr中所有的值都为正数且不重复.每个值代表一种面值的货币,每种面值的货币可以使用任意张,再给定一个整数aim,求组成aim的方法数. 解题思路 完全背包 和"求换钱的 ...

  5. 算法之Python实现 - 003 : 换钱的方法数

    [题目]给定数组arr,arr中所有的值都为正数且不重复.每个值代表一种面值的货币,每种面值的货币可以使用任意张,再给定一个整数aim代表要找的钱数,求组成aim的方法数. [代码1]递归 impor ...

  6. 算法之Python实现 - 002 : 换钱的最少货币数补充(每种货币只能使用一次)

    [题目]:给定数组arr,arr中所有的值都为正数且不重复.每个值代表一种面值的货币,每种面值的货币仅可以使用一张,再给定一个整数aim代表要找的钱数,求组成aim的最少货币数. [代码1]:时间与额 ...

  7. 算法之Python实现 - 001 : 换钱的最少货币数

    [题目]给定数组arr,arr中所有的值都为正数且不重复.每个值代表一种面值的货币,每种面值的货币可以使用任意张,再给定一个整数aim代表要找的钱数,求组成aim的最少货币数. [代码1]:时间与额外 ...

  8. MT【227】换钱的总数

    (2012复旦)将1张面值100元的人民币全部换成面值1角,2角,5角的人民币,不同的换法有多少种? 解:即求不等式$2x+5y\le1000$的所有非负整数解的个数.由匹克公式:$S=a+\dfra ...

  9. [DP]换钱的最小货币数

    题目一 给定数组arr,数组中有N个元素,其中所有的之都为整数且不重复.每个只代表一种面值的货币,每种面值的货币可以使用任意张,在给定一个整数aim代表要找的钱数,求组成aim的最少货币数. 解法 依 ...

随机推荐

  1. 【基础】1001_Hello,World!

    题目相关 [题目描述] 编写一个能够输出"Hello,World!"的程序,这个程序常常作为一个初学者接触一门新的编程语言所写的第一个程序,也经常用来测试开发.编译环境是否能够正常 ...

  2. WPF 关于拖拽打开文件的注意事项

    由于开发需求,需要开发一个类似Win图片浏览的工具 当然也涉及到了拖拽打开的需求 按照固有思路: <Grid x:Name="grid1" AllowDrop="T ...

  3. 【Oracle】迁移表到其他的表空间

    有些时候需要将表迁移到其他的表空间,在将表空间做相关的操作 下面是命令如何迁移表空间 SQL> alter table 表名 move tablespace 表空间名; 如果有很多的表想要迁移的 ...

  4. 洛谷P1198 [JSOI2008]最大数(线段树/单调栈)

    题目链接: https://www.luogu.org/problemnew/show/P1198 题目描述 现在请求你维护一个数列,要求提供以下两种操作: 1. 查询操作. 语法:Q L 功能:查询 ...

  5. 5V-12V输入输出的限流芯片,可调限至4.8A

    可是在输出电压模式:3.6V,5V,12V 在输出3.6V模式:输入电压范围2.5V-4.5V,输入关闭电压5V,限流最大4.8A 在输出5V模式,输入电压范围3.8V-6V,输输入过电压关闭6V,限 ...

  6. 日常分享:关于时间复杂度和空间复杂度的一些优化心得分享(C#)

    前言 今天分享一下日常工作中遇到的性能问题和解决方案,比较零碎,后续会持续更新(运行环境为.net core 3.1) 本次分享的案例都是由实际生产而来,经过简化后作为举例 Part 1(作为简单数据 ...

  7. jQuery 多选与清除

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  8. Qt 使用tablib获取多媒体tag信息

    最近项目需要, 要获取音乐文件tag信息. 有两个方式, 本人偏向第二种方式. 效率比较高,可控性比较好. 一.QML方式 使用QML Audio component 进行解析. 将多媒体文件都放到P ...

  9. git commit,启动文本编辑器

    git commit中输入message的几种方式 - 简书 https://www.jianshu.com/p/ad461b99e860 在所有的git教程里,git commit肯定是一开始就会提 ...

  10. 7. A typical stream socket session

    http://publibfp.dhe.ibm.com/epubs/pdf/f1a2d400.pdf Read and write data on socket s, using the send() ...