Currency Exchange POJ - 1860 (spfa)
题目链接:Currency Exchange
题意:
钱的种类为N,M条命令,拥有种类为S这类钱的数目为V,命令为将a换成b,剩下的四个数为a对b的汇率和a换成b的税,b对a的汇率和b换成a的税,公式为(钱数-税)*汇率,问最后钱的数目是否会增多
题解:
这是我第一道SPFA,这题算是SPFA的模板题吧。找令价值最大的最长路径。
1 #include<cstdio> 2 #include<iostream> 3 #include<vector> 4 #include<algorithm> 5 #include<queue> 6 #include<cstring> 7 using namespace std; 8 typedef pair<int,int> P; 9 const int MAX_N = 1e3+9; 10 struct node{ 11 int to; 12 double huilv,shui; 13 node(int a,double b,double c){ 14 to = a; 15 huilv = b; 16 shui = c; 17 } 18 }; 19 vector<node> vec[MAX_N]; 20 double res[MAX_N]; 21 int vis[MAX_N]; 22 queue< int> que; 23 int N,M,T,nick; 24 int a,b; 25 double huilv1,huilv2; 26 double shui1,shui2; 27 double val; 28 void init() 29 { 30 memset(res,0,sizeof(res)); 31 memset(vis,0,sizeof(vis)); 32 while(!que.empty()) que.pop(); 33 for(int i=0;i<MAX_N;i++) vec[i].clear(); 34 } 35 int spfa(int x) 36 { 37 res[x] = val; 38 vis[x] = 1; 39 que.push(x); 40 while(!que.empty()) 41 { 42 int t = que.front(); 43 que.pop(); 44 vis[t] = 0; 45 for(int i=0;i<vec[t].size();i++) 46 { 47 node temp = vec[t][i]; 48 49 //if(temp.to == 1) cout<<temp.to<<" "<<temp.huilv<<" "<<temp.shui<<endl; 50 51 if(res[temp.to] < (res[t] - temp.shui)*temp.huilv) 52 { 53 res[temp.to] = (res[t] - temp.shui)*temp.huilv; 54 if(vis[temp.to] == 0) 55 { 56 que.push(temp.to); 57 vis[temp.to] = 1; 58 } 59 } 60 if(res[x] > val) 61 return 1; 62 } 63 64 } 65 //cout<<"......."<<res[2]<<endl; 66 return 0; 67 } 68 int main() 69 { 70 while(~scanf("%d%d%d%lf",&N,&M,&nick,&val)) 71 { 72 init(); 73 for(int i=0;i<M;i++) 74 { 75 scanf("%d%d%lf%lf%lf%lf",&a,&b,&huilv1,&shui1,&huilv2,&shui2); 76 vec[a].push_back(node(b,huilv1,shui1)); 77 vec[b].push_back(node(a,huilv2,shui2)); 78 } 79 if(spfa(nick)) 80 { 81 cout<<"YES"<<endl; 82 } 83 else 84 { 85 cout<<"NO"<<endl; 86 } 87 } 88 return 0; 89 }