POJ-1860-Currency Exchange
2016-07-30 11:27
323 查看
Currency Exchange
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.
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
Sample Output
YES
解法一:
#include<stdio.h>
#include<string.h>
#include<queue>
#include<vector>
#include<istream>
using namespace std;
const double INF = 0x3f3f3f;
const int MAXN = 105;
int n, m, s;
double dis[MAXN];
double v;
struct node
{
int to;
double r, c;
};
//bool vis[MAXN];
vector<node> graph[MAXN];
int SPFA()
{
queue<int>q;
//memset(vis, 0,sizeof(vis));
dis[s] = v;
//vis[s] = true;
q.push(s);
while(!q.empty())
{
int f = q.front();
q.pop();
//vis[f] = false;
int Size = graph[f].size();
for(int i =0;i<Size; ++i)
{
int to = graph[f][i].to;
if((dis[f]-graph[f][i].c)*graph[f][i].r>dis[to])
{
dis[to] = (dis[f]-graph[f][i].c)*graph[f][i].r;
//if(!vis[to])
//{
q.push(to);
//vis[to] = true;
//}
if(graph[f][i].to==s&&dis[s]>v)
return true;
}
}
}
return false;
}
int main()
{
int a,b;
double c,d,e,f;
while(~scanf("%d %d %d %lf", &n, &m, &s, &v))
{
for(int i = 0; i<=n;++i)
graph[i].clear();
while(m--)
{
scanf("%d%d%lf%lf%lf%lf",&a,&b,&c,&d,&e,&f);
node ver;
ver.to = b;
ver.r = c;
ver.c = d;
graph[a].push_back(ver);
ver.to = a;
ver.r = e;
ver.c = f;
graph[b].push_back(ver);
}
if(SPFA())printf("YES\n");
else printf("NO\n");
}
return 0;
}
解法二:
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<iostream>
using namespace std;
int n, m, s, top;
double v;
double dis[110];
struct node
{
int a, b;
double r, c;
}xt[1010];
void add(int a, int b, double r, double c)
{
xt[top].a = a;
xt[top].b = b;
xt[top].r = r;
xt[top].c = c;
++top;
}
int ff()
{
memset(dis, 0,sizeof(dis));
dis[s]=v;
for(int i=1;i<n;i++)
{
bool flag=false;
for(int j=0;j<top;j++)
{
int u=xt[j].a;
int v=xt[j].b;
if(dis[v] < (dis[u]-xt[j].c)*xt[j].r)
{
flag=true;
dis[v]=(dis[u]-xt[j].c)*xt[j].r;
}
}
if(!flag)return false;
}
for(int j=0;j<top;j++)
if(dis[xt[j].b]<(dis[xt[j].a]-xt[j].c)*xt[j].r)
return true;
return false;
}
int main()
{
int a,b;
double c,d,e,f;
while(scanf("%d%d%d%lf",&n,&m,&s,&v)!=EOF)
{
top=0;
while(m--)
{
scanf("%d%d%lf%lf%lf%lf",&a,&b,&c,&d,&e,&f);
add(a,b,c,d);
add(b,a,e,f);
}
if(ff()) printf("YES\n");
else printf("NO\n");
}
return 0;
}
Time Limit: 1000MS | Memory Limit: 30000K | |
Total Submissions: 26409 | Accepted: 9757 |
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
解法一:
#include<stdio.h>
#include<string.h>
#include<queue>
#include<vector>
#include<istream>
using namespace std;
const double INF = 0x3f3f3f;
const int MAXN = 105;
int n, m, s;
double dis[MAXN];
double v;
struct node
{
int to;
double r, c;
};
//bool vis[MAXN];
vector<node> graph[MAXN];
int SPFA()
{
queue<int>q;
//memset(vis, 0,sizeof(vis));
dis[s] = v;
//vis[s] = true;
q.push(s);
while(!q.empty())
{
int f = q.front();
q.pop();
//vis[f] = false;
int Size = graph[f].size();
for(int i =0;i<Size; ++i)
{
int to = graph[f][i].to;
if((dis[f]-graph[f][i].c)*graph[f][i].r>dis[to])
{
dis[to] = (dis[f]-graph[f][i].c)*graph[f][i].r;
//if(!vis[to])
//{
q.push(to);
//vis[to] = true;
//}
if(graph[f][i].to==s&&dis[s]>v)
return true;
}
}
}
return false;
}
int main()
{
int a,b;
double c,d,e,f;
while(~scanf("%d %d %d %lf", &n, &m, &s, &v))
{
for(int i = 0; i<=n;++i)
graph[i].clear();
while(m--)
{
scanf("%d%d%lf%lf%lf%lf",&a,&b,&c,&d,&e,&f);
node ver;
ver.to = b;
ver.r = c;
ver.c = d;
graph[a].push_back(ver);
ver.to = a;
ver.r = e;
ver.c = f;
graph[b].push_back(ver);
}
if(SPFA())printf("YES\n");
else printf("NO\n");
}
return 0;
}
解法二:
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<iostream>
using namespace std;
int n, m, s, top;
double v;
double dis[110];
struct node
{
int a, b;
double r, c;
}xt[1010];
void add(int a, int b, double r, double c)
{
xt[top].a = a;
xt[top].b = b;
xt[top].r = r;
xt[top].c = c;
++top;
}
int ff()
{
memset(dis, 0,sizeof(dis));
dis[s]=v;
for(int i=1;i<n;i++)
{
bool flag=false;
for(int j=0;j<top;j++)
{
int u=xt[j].a;
int v=xt[j].b;
if(dis[v] < (dis[u]-xt[j].c)*xt[j].r)
{
flag=true;
dis[v]=(dis[u]-xt[j].c)*xt[j].r;
}
}
if(!flag)return false;
}
for(int j=0;j<top;j++)
if(dis[xt[j].b]<(dis[xt[j].a]-xt[j].c)*xt[j].r)
return true;
return false;
}
int main()
{
int a,b;
double c,d,e,f;
while(scanf("%d%d%d%lf",&n,&m,&s,&v)!=EOF)
{
top=0;
while(m--)
{
scanf("%d%d%lf%lf%lf%lf",&a,&b,&c,&d,&e,&f);
add(a,b,c,d);
add(b,a,e,f);
}
if(ff()) printf("YES\n");
else printf("NO\n");
}
return 0;
}
相关文章推荐
- Oracle中添加新用户并赋予权限
- 为什么资源文件名以类型开头
- JDBC-Result/Sets
- Spark1.5堆内存分配
- Query简单的一个小动画
- Ubuntu16.04TLS系统中chromium无法加载flash插件的问题
- C语言结构体结合malloc、free使用小例子
- 关于PhotoShop工具栏失灵
- Mean shift 算法
- RHEL/Centos7 安装图形化桌面
- Ubuntu更改MySQL字符集UTF-8,正确显示中文
- iOS 自适应label行高
- 李一男2003年在港湾给开发人员培训时的语录
- 近期虚拟机相关问题总结
- iOS开发UI基础—手写控件,frame,center和bounds属性
- Educational Codeforces Round 15--AMaximum Increase--求最长上升数列的长度
- DPM(Deformable Parts Model)之hog31维特征
- java运行时类信息----原理、API及应用
- spark1.6内存管理
- HDU 5087(DP 次最长子序列)