POJ 1860 Currency Exchange【SPFA判环】
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<=10 3.
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.0 1 2 1.00 1.00 1.00 1.00 2 3 1.10 1.00 1.10 1.00
Sample Output
YES
【题意】
钱的种类为N,M条命令,拥有种类为S这类钱的数目为V,命令为将a换成b,剩下的四个数为a对b的汇率和a换成b的税,b对a的汇率和b换成a的税,公式为(钱数-税)*汇率,问最后钱的数目是否会增多
【分析】
建图,一种货币就是一个点,货币交换作为有向边。边的权值需要小心,A到B的权值为(V(A) - C)*R。看到正权回路,应该想到负权回路,那思考一下是不是能用Bellman-Ford来做呢,其实这个问题刚刚好是相反的,这里需要求最长路,那么把dist初始化为0,dist[s]=v,松弛条件相反,利用Bellman-Ford的思想就能解决这道题了。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include<cstdio> #include<string> #include<cstdlib> #include<cmath> #include<iostream> #include<cstring> #include<set> #include<queue> #include<algorithm> #include<vector> #include<map> #include<cctype> #include<stack> #include<sstream> #include<list> #include<assert.h> #include<bitset> #include<numeric> #define debug() puts("++++") #define gcd(a,b) __gcd(a,b) #define lson l,m,rt<<1 #define rson m+1,r,rt<<1|1 #define fi first #define se second #define pb push_back #define sqr(x) ((x)*(x)) #define ms(a,b) memset(a,b,sizeof(a)) #define sz size() #define be begin() #define mp make_pair #define pu push_up #define pd push_down #define cl clear() #define lowbit(x) -x&x #define all 1,n,1 #define rep(i,x,n) for(int i=(x); i<=(n); i++) #define in freopen("in.in","r",stdin) #define out freopen("out.out","w",stdout) using namespace std; typedef long long LL; typedef unsigned long long ULL; typedef pair<int,int> P; const int INF = 0x3f3f3f3f; const LL LNF = 1e18; const int maxn = 1e5+20; const int maxm = 1e6 + 10; const double PI = acos(-1.0); const double eps = 1e-8; const int dx[] = {-1,1,0,0,1,1,-1,-1}; const int dy[] = {0,0,1,-1,1,-1,1,-1}; int dir[4][2] = {{0,1},{0,-1},{-1,0},{1,0}}; const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; int tot,n,m,x,s; int u,w; double v; double ab1,ab2,ba1,ba2; double dis[maxn]; int cnt[maxn],vis[maxn]; struct cmp { bool operator()(int a,int b) { return dis[a] > dis[b]; } }; int head[maxn]; struct node { int v,nxt; double r,c,w; }e[maxn]; void init() { tot=0; ms(head,-1); ms(dis,0);//求最长路径开始设为0 ms(vis,0); } void add(int u,int v,double r,double c) { e[tot].r=r; e[tot].c=c; e[tot].v=v; e[tot].w=w; e[tot].nxt=head[u]; head[u]=tot++; } int spfa(int s) { queue<int> q; dis[s]=v; vis[s]=1; cnt[s]++; q.push(s); while(!q.empty()) { int u = q.front(); q.pop(); vis[u]=0; // for(int i=head[u];~i;i=e[i].nxt) { int v = e[i].v; //本金 减去利息 再乘汇率; e[i].w = (dis[u] - e[i].c)*e[i].r-dis[u]; if(dis[v] < dis[u] + e[i].w) { dis[v] = dis[u] + e[i].w; if(!vis[v])//防止出现环,也就是进队列重复了 { vis[v]=1; q.push(v); //如果一个点能变大n次以上说明还能继续增大&说明原值已经可以通过转换增大 if(++cnt[v]>n) return -1;//有负环 } } } } return 1; } int main() { while(~scanf("%d%d%d%lf",&n,&m,&s,&v)) { init(); int a,b; for(int i=1;i<=m;i++) { scanf("%d%d%lf%lf",&a,&b,&ab1,&ab2); add(a,b,ab1,ab2);//双向链表 scanf("%lf%lf",&ba1,&ba2); add(b,a,ba1,ba2); } if(spfa(s)==-1) puts("YES"); else puts("NO"); } } /* 【题意】 【类型】 SPFA判断负环变形 【分析】 【时间复杂度&&优化】 【trick】 【数据】 */