货币兑换,使得货币最后再兑换回S的时候Nick的总资产是增加的,这就要使兑换货币的路径有一个正的回路,才可以使Nick的总资产是增加的。用bellman-ford找回路,回路存在,则YES,否则NO。
http://blog.csdn.net/yslcl12345/article/details/50574213,这个题解用的floyd做的,题目翻译的挺好的
#include <iostream>
#include <cstring>
using namespace std;
int n,m,s;
double v;
int edge;
double dis[101];
struct point
{
int a,b;
double r,c;
};
point exc[202];
bool bellmanFord()
{
memset(dis,0,sizeof(dis));
dis[s] = v;
bool flag;
for(int i = 0; i < n-1; ++i)
{
flag = false;
for(int j = 0; j < edge; ++j)
{
if(dis[exc[j].b] < (dis[exc[j].a] - exc[j].c)*exc[j].r)
{
flag = true;
dis[exc[j].b] = (dis[exc[j].a] - exc[j].c)*exc[j].r;
}
}
if(!flag)
break;
}
for(int k = 0; k < edge; ++k)
if(dis[exc[k].b] < (dis[exc[k].a] - exc[k].c)*exc[k].r)
return true;
return false;
}
int main()
{
int a,b;
double rab,cab,rba,cba;
while(cin >> n >> m >> s >> v)
{
edge = 0;
for(int i = 0; i < m; ++i)
{
cin >> a >> b >> rab >> cab >> rba >> cba;
exc[edge].a = a;
exc[edge].b = b;
exc[edge].r = rab;
exc[edge++].c = cab;
exc[edge].a = b;
exc[edge].b = a;
exc[edge].r = rba;
exc[edge++].c = cba;
}
if(bellmanFord())
cout << "YES" <<endl;
else
cout << "NO" << endl;
}
return 0;
}