【POJ】1860 Currency Exchange
真是气skr人。。没把d[]换成double。。。de了一上午的bug//
记得用G++提交啊
题目链接:http://poj.org/problem?id=1860
题意:告诉你n个点,m条路。起始点s,还有初始金额money。每条路表示从a->b的汇率和佣金以及b->a的汇率和佣金。你在该点所得是(本金-佣金)*汇率。问你这个人能不能赚钱。
题解:spfa套一下//。记得d[]换成double。具体的看看代码。QWQ。
代码:
1 #include<iostream> 2 #include<stack> 3 #include<vector> 4 #include<queue> 5 #include<algorithm> 6 #include<cstdio> 7 using namespace std; 8 const int maxn = 105; 9 10 11 struct node{ 12 int to; 13 double r; 14 double c; 15 }; 16 17 vector< node > e[maxn]; 18 19 int n,m,num; 20 double money; 21 double d[maxn]; 22 int inq[maxn]; 23 24 bool spfa(int s){ 25 for(int i = 1; i <= n ;i++){ 26 inq[i] = d[i] = 0; 27 } 28 queue<int>Q; 29 Q.push(s); 30 d[s] = money; 31 inq[s] = 1; 32 while( !Q.empty() ){ 33 int now = Q.front(); 34 Q.pop(); 35 inq[now] = 0; 36 for(int i = 0; i < e[now].size() ; i++){ 37 double rate = e[now][i].r; 38 double commis = e[now][i].c; 39 int v = e[now][i].to; 40 41 if(d[v] < (d[now] - commis) * rate){ 42 d[v] = (d[now] - commis) * rate; 43 if(inq[v] == 0){ 44 inq[v] = 1; 45 Q.push(v); 46 } 47 } 48 49 if(d[s] > money){ 50 return true; 51 } 52 } 53 54 55 } 56 return false; 57 } 58 59 int main() { 60 scanf("%d%d%d%lf",&n,&m,&num,&money); 61 int x,y; 62 double r,c; 63 while(m--){ 64 scanf("%d%d%lf%lf",&x,&y,&r,&c); 65 e[x].push_back((node){y,r,c}); 66 scanf("%lf%lf",&r,&c); 67 e[y].push_back((node){x,r,c}); 68 } 69 if(spfa(num)) 70 cout<<"YES"<<endl; 71 else 72 cout<<"NO"<<endl; 73 74 return 0; 75 }