POJ1860 Currency Exchange —— spfa求正环
题目链接:http://poj.org/problem?id=1860
Time Limit: 1000MS | Memory Limit: 30000K | |
Total Submissions: 32128 | Accepted: 12228 |
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 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
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
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
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <string>
#include <set>
#define rep(i,a,n) for(int (i) = a; (i)<=(n); (i)++)
#define ms(a,b) memset((a),(b),sizeof((a)))
using namespace std;
typedef long long LL;
const double EPS = 1e-;
const int INF = 2e9;
const LL LNF = 9e18;
const int MOD = 1e9+;
const int MAXN = 1e3+; int n, m,X;
double V; struct edge
{
double r, c;
int to,next;
}edge[MAXN*MAXN];
int cnt, head[MAXN]; void addedge(int u, int v, double r, double c)
{
edge[cnt].to = v;
edge[cnt].r = r;
edge[cnt].c = c;
edge[cnt].next = head[u];
head[u] = cnt++;
} void init()
{
cnt = ;
memset(head, -, sizeof(head));
} double dis[MAXN];
int times[MAXN], inq[MAXN];
bool spfa(int st)
{
memset(inq, , sizeof(inq));
memset(times, , sizeof(times));
for(int i = ; i<=n; i++)
dis[i] = -INF; queue<int>Q;
Q.push(st);
inq[st] = ;
dis[st] = V;
while(!Q.empty())
{
int u = Q.front();
Q.pop(); inq[u] = ;
for(int i = head[u]; i!=-; i = edge[i].next)
{
int v = edge[i].to;
if(dis[v]< (dis[u]-edge[i].c)*edge[i].r )
{
dis[v] = (dis[u]-edge[i].c)*edge[i].r;
if(!inq[v])
{
Q.push(v);
inq[v] = ;
if(++times[v]>n) return true;
}
}
}
}
return false;
} int main()
{
while(scanf("%d%d%d%lf",&n,&m,&X, &V)!=EOF)
{
init();
for(int i = ; i<=m; i++)
{
int u, v;
double r1,c1,r2,c2;
scanf("%d%d%lf%lf%lf%lf", &u, &v, &r1,&c1,&r2,&c2);
addedge(u,v,r1,c1);
addedge(v,u,r2,c2);
}
if(spfa(X))
puts("YES");
else
puts("NO");
}
}
POJ1860 Currency Exchange —— spfa求正环的更多相关文章
- poj1860 Currency Exchange(spfa判断正环)
Description Several currency exchange points are working in our city. Let us suppose that each point ...
- POJ1680 Currency Exchange SPFA判正环
转载来源:優YoU http://user.qzone.qq.com/289065406/blog/1299337940 提示:关键在于反向利用Bellman-Ford算法 题目大意 有多种汇币,汇 ...
- poj1860 Currency Exchange(spfa判断是否存在正环)
题意:有m个货币交换点,每个点只能有两种货币的互相交换,且要给佣金,给定一开始的货币类型和货币数量,问若干次交换后能否让钱增加. 思路:spfa求最长路,判断是否存在正环,如果存在则钱可以在环中一直增 ...
- poj - 1860 Currency Exchange Bellman-Ford 判断正环
Currency Exchange POJ - 1860 题意: 有许多货币兑换点,每个兑换点仅支持两种货币的兑换,兑换有相应的汇率和手续费.你有s这个货币 V 个,问是否能通过合理地兑换货币,使得你 ...
- Currency Exchange POJ - 1860 (spfa判断正环)
Several currency exchange points are working in our city. Let us suppose that each point specializes ...
- Currency Exchange POJ - 1860 spfa判断正环
//spfa 判断正环 #include<iostream> #include<queue> #include<cstring> using namespace s ...
- POJ 3621 Sightseeing Cows 【01分数规划+spfa判正环】
题目链接:http://poj.org/problem?id=3621 Sightseeing Cows Time Limit: 1000MS Memory Limit: 65536K Total ...
- poj3621 SPFA判断正环+二分答案
Farmer John has decided to reward his cows for their hard work by taking them on a tour of the big c ...
- POJ 1860——Currency Exchange——————【最短路、SPFA判正环】
Currency Exchange Time Limit:1000MS Memory Limit:30000KB 64bit IO Format:%I64d & %I64u S ...
随机推荐
- Ceph纠删码编码机制
1 Ceph简述 Ceph是一种性能优越,可靠性和可扩展性良好的统一的分布式云存储系统,提供对象存储.块存储.文件存储三种存储服务.Ceph文件系统中不区分节点中心,在理论上可以实现系统规模的无限扩展 ...
- cf575A Fibonotci
Fibonotci sequence is an integer recursive sequence defined by the recurrence relation Fn = sn - 1·F ...
- css-包含块
在CSS中,有事一个元素的位置和尺寸的计算都相对于一个矩形,这个矩形被称作包含块.包含块是一个相对的概念,比如 子元素的初始化布局总是在父元素的左上角,这就是一个相对的概念.其中父元素就是一个参照物, ...
- Day 5 Linux之用户、群组和权限
Linux之用户.群组和权限 一.各文件及内容对应含义 1./etc/passwd文件 功能:存储所有用户的相关信息,该文件也被称为用户信息数据库(Database). 含义:如下图所示. 2./et ...
- 简单div遮罩
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- python--错误了就需要调试(异常处理)
python提供了两个非常重要的功能来处理python程序在运行中出现的异常和错误.你可以使用该功能来调试python程序. 我们可打开idle-->F1进行查看文档,里面很多异常类型,如图: ...
- nginx的配置文件 【nginx.conf】
#user nobody; worker_processes 1; #error_log logs/error.log; #error_log logs/error.log notice; #erro ...
- vue之监听事件
一.v-on 可以用 v-on 指令监听 DOM 事件,并在触发时运行一些 JavaScript 代码. 简写形式 用@代替 v-on: <button v-on:click="co ...
- Linux内核配置选项
http://blog.csdn.net/wdsfup/article/details/52302142 http://www.manew.com/blog-166674-12962.html Gen ...
- 【String】String.format(format,args...)的使用解析
String.format(format,args...)的使用解析 使用kotlin 中使用示例 ================================================== ...