题目链接:Currency Exchange
题意:
钱的种类为N,M条命令,拥有种类为S这类钱的数目为V,命令为将a换成b,剩下的四个数为a对b的汇率和a换成b的税,b对a的汇率和b换成a的税,公式为(钱数-税)*汇率,问最后钱的数目是否会增多
题解:
这是我第一道SPFA,这题算是SPFA的模板题吧。找令价值最大的最长路径。
#include<cstdio>
#include<iostream>
#include<vector>
#include<algorithm>
#include<queue>
#include<cstring>
using namespace std;
typedef pair<int,int> P;
const int MAX_N = 1e3+;
struct node{
int to;
double huilv,shui;
node(int a,double b,double c){
to = a;
huilv = b;
shui = c;
}
};
vector<node> vec[MAX_N];
double res[MAX_N];
int vis[MAX_N];
queue< int> que;
int N,M,T,nick;
int a,b;
double huilv1,huilv2;
double shui1,shui2;
double val;
void init()
{
memset(res,,sizeof(res));
memset(vis,,sizeof(vis));
while(!que.empty()) que.pop();
for(int i=;i<MAX_N;i++) vec[i].clear();
}
int spfa(int x)
{
res[x] = val;
vis[x] = ;
que.push(x);
while(!que.empty())
{
int t = que.front();
que.pop();
vis[t] = ;
for(int i=;i<vec[t].size();i++)
{
node temp = vec[t][i]; //if(temp.to == 1) cout<<temp.to<<" "<<temp.huilv<<" "<<temp.shui<<endl; if(res[temp.to] < (res[t] - temp.shui)*temp.huilv)
{
res[temp.to] = (res[t] - temp.shui)*temp.huilv;
if(vis[temp.to] == )
{
que.push(temp.to);
vis[temp.to] = ;
}
}
if(res[x] > val)
return ;
} }
//cout<<"......."<<res[2]<<endl;
return ;
}
int main()
{
while(~scanf("%d%d%d%lf",&N,&M,&nick,&val))
{
init();
for(int i=;i<M;i++)
{
scanf("%d%d%lf%lf%lf%lf",&a,&b,&huilv1,&shui1,&huilv2,&shui2);
vec[a].push_back(node(b,huilv1,shui1));
vec[b].push_back(node(a,huilv2,shui2));
}
if(spfa(nick))
{
cout<<"YES"<<endl;
}
else
{
cout<<"NO"<<endl;
}
}
return ;
}