Time Limit: 1000MS | Memory Limit: 30000K | |
Total Submissions: 16723 | Accepted: 5856 |
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
這道題我只想說,各種無情與各種的坑,已經無力吐槽了,但是依然還是AC了,這道題具體可以選擇Bellman-Ford算法,這個算法本身是求最短路徑的,但是在這道題中,負權環的利用對求解這道題灰常有幫助,我們可以把每一種幣種當成是圖論中的一個點,然後之間的匯率以及匯費計算結果當成之間的邊的權值,然後求自己所擁有的貨幣經過一系列兌換,直到兌換回原來貨幣的時候是否可以增長,這道題解題就是運用Bellman-Ford算法求最長路程,其實就是判定這個圖中是否有一條負權環,即一條環,可無限增加Nick手中的錢數,只要任意存在一條這樣的環,則Nick手中的錢可以增長,否則不可以。這樣的情況下就可以AC了,之後我在做題時候發現,如果把數組定義爲題目中所說的100稍大的105時,題目會報Runtime Error,對此我很無語,把105改成256,結果就AC了,這是一個巨坑的點,給的數據超出了他所限定的數據範圍!!!
下面是AC代碼:
#include<cstdio>
#include<iostream>
#include<cstring>
using namespace std;
double num,rate[256][256],com[256][256];
int m,n,fi;
struct Edge{
int u, v;
double com,rate;
}edge[256];
double dist[256];
void relax(int u, int v, double rate, double com)
{
if(dist[v] < ( dist[u] - com ) * rate && dist[u] > com)
dist[v] = ( dist[u] - com ) * rate;
}
bool Bellman_Ford()
{
int i;
for(i=1;i<=n;i++)
dist[i]=0;
dist[fi]=num;
for(i=1; i<=n-1; ++i)
for(int j=1; j<=2*m; ++j)
relax(edge[j].u, edge[j].v, edge[j].rate, edge[j].com);
bool flag = 1;
for(i=1; i<=2*m; ++i)
if(dist[edge[i].v] < ( dist[edge[i].u] - edge[i].com ) * edge[i].rate )
{
flag = 0;
break;
}
return flag;
}
int main()
{
int i,a,b;
while(scanf("%d%d%d%lf",&n,&m,&fi,&num)!=EOF)
{
memset(rate,0,sizeof(rate));
memset(com,0,sizeof(com));
for(i=1;i<=2*m;i+=2)
{
scanf("%d%d",&a,&b);
edge[i].u=a;
edge[i].v=b;
edge[i+1].u=b;
edge[i+1].v=a;
cin>>edge[i].rate>>edge[i].com>>edge[i+1].rate>>edge[i+1].com;
}
if(Bellman_Ford()==0)
printf("YES\n");
else
printf("NO\n");
}
return 0;
}