Time Limit: 1000MS | Memory Limit: 30000K | |
Total Submissions: 27885 | Accepted: 10376 |
Description
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
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
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
Source
题意:当局资金与资金的兑换规则是b钱币=(a钱币-commission_ab)*exchange rate_ab,有个机智主角想要用钱生钱,现在他有一笔初始资金,题目会给你m种两钱币互相兑换的关系,最后请输出机智主角能否利用这套规则钱生钱。
虽然网上都说这题要用bellman-ford来做,然而在讨论版发现了一股清流用bfs做
然后写了bfs清流版本。首先不剪枝的话bfs是超时的,在每次兑换钱后检查如果现在进行兑换操作是否能让钱增加,若可以就进行兑换,否则不兑换。
若重新回到起点时发现资金相比初时来得增加的话就判真,搜索完没有发现增加则判假。。。数据比较水吧。重点还是那个剪枝,嗯。。
剪枝是精髓啊!!
学完bellman—ford再回来补完,先放上bfs版本:
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <map>
#include <set>
#include <cstdlib>
#include <queue>
#include <stack>
using namespace std;
const int maxn = 110;
const double eps = 1e-8;
int s0;
double money;
struct unit{
int to;
double rate;
double commission;
};
double d[maxn];
//unit grap[maxn][maxn];
vector<unit> save[maxn];
int n,m;
bool bfs(){
queue<int> que;
int i,j;
for(i=1;i<=n;i++){
d[i] = 0.0;
}
que.push(s0);
d[s0] = money;
bool flag = false;
while(que.size()){
int q = que.front();
//cout<<q<<endl;
que.pop();
/*if(cnt[q]>=n) {
//cout<<"aa"<<endl;
return true;
}*/
if(d[s0]-money>eps&&flag){
return true;
}
flag = true;
if(save[q].size()){
for(i=0;i<save[q].size();i++){
double now = (d[q]-save[q][i].commission)*save[q][i].rate;
if(now-d[save[q][i].to]<eps){
continue;
}
d[save[q][i].to] = now;
que.push(save[q][i].to);
}
}
}
return false;
}
int main(){
int i,j,a,b;
double rab,cab,rba,cba;
scanf("%d%d%d%lf",&n,&m,&s0,&money);
//RAB, CAB, RBA and CBA
for(i=1;i<=m;i++){
scanf("%d%d%lf%lf%lf%lf",&a,&b,&rab,&cab,&rba,&cba);
unit now;
now.to = b;
now.rate = rab;
now.commission = cab;
save[a].push_back(now);
now.to = a;
now.rate = rba;
now.commission = cba;
save[b].push_back(now);
}
if(bfs()){
printf("YES\n");
}else{
printf("NO\n");
}
return 0;
}
回来补上BellmanFord学习版本了,首先先放上几个写的比较好的博客:
http://www.cnblogs.com/tanky_woo/archive/2011/01/17/1937728.html
http://www.wutianqi.com/?p=1903
这个算法似乎和dijkstra有着相当大的关系。两者都可用来求单源最短路,但是dijkstra遇见负数权值的边就要挂了,而BellmanFord依旧可行
该算法的复杂度为O(V*E)
我还是要写几道题感悟一下吧,现在只知道套路,还是要阅读博客理解一下
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <map>
using namespace std;
const int maxn = 110;
double INF = 9999999999;
int n,m,s0,p;
double money;
struct unit {
double rate;
double comission;
};
unit save[maxn][maxn];
pair<int, int> edge[maxn*maxn];
double dis[maxn];
void init(){
int i;
for(i=1;i<=n;i++){
dis[i] = 0.0f;
}
dis[s0] = money;
}
void relax(int u,int v){
if(dis[v]<(dis[u]-save[u][v].comission)*save[u][v].rate){
dis[v] = (dis[u]-save[u][v].comission)*save[u][v].rate;
}
}
bool BellmanFord(){
int i,j;
for(i=1;i<=n-1;i++){//why??
for(j=1;j<=p;j++){
relax(edge[j].first,edge[j].second);
}
}
for(i=1;i<=p;i++){
int u = edge[i].first;
int v = edge[i].second;
if(dis[v]<(dis[u]-save[u][v].comission)*save[u][v].rate){
return true;
}
}
return false;
}
int main(){
scanf("%d%d%d%lf",&n,&m,&s0,&money);
int a,b,i;
double Rab,Cab,Rba,Cba;
init();
p = 0;
for(i=1;i<=m;i++){
scanf("%d%d%lf%lf%lf%lf",&a,&b,&Rab,&Cab,&Rba,&Cba);
save[a][b].rate = Rab;
save[a][b].comission = Cab;
save[b][a].rate = Rba;
save[b][a].comission = Cba;
edge[++p] = make_pair(a, b);
edge[++p] = make_pair(b, a);
}
if(BellmanFord()){
printf("YES\n");
}else{
printf("NO\n");
}
return 0;
}