Both of them are money exchange problems, that is, holding one currency for other currencies, through the different interest rates and finally returning money will increase. I did these two questions with the shortest floyd. I went through all the points to find the relationship, that is, to exchange money.
In fact, this kind of question can also be done by bellman, because this kind of question is judgement.
Is there a positive weight loop to maximize (much like the negative weight loop to minimize)
The difference is that bellman only needs to see if there is a negative weight loop at last. If there is no negative weight loop, then it can't. floyd is equivalent to dealing with all the values first. Finally, see if there is a greater value than the original value, then it can, otherwise the opposite is true.
First question:
Currency Exchange(poj1860)
There are many currencies that can be exchanged between currencies. Of course, the interest rates inside are different, and the money ends up different. When you exchange 100 A for B, the exchange rate between A and B is 29.75, and the handling fee is 0.39. Then you can get (100 - 0.39)* 29.75 = 2963.3975 B. Ask if the amount of s coin can be increased by the exchange of the final amount of s coin.
Code:
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int n,m,s;
double v;
double map1[105][105],map2[105][105],mapp[105];
int floyd()
{
double d[105];
for(int i=1; i<=n; i++)
d[i]=mapp[i];
for(int k=1; k<=n; k++)
{
for(int i=1; i<=n; i++)
{
for(int j=1; j<=n; j++)
{
if((mapp[i]-map2[i][j])*map1[i][j]>mapp[j])
mapp[j]=(mapp[i]-map2[i][j])*map1[i][j];
}
}
}
for(int i=1; i<=n; i++)
if(d[i]<mapp[i])
return 1;
return 0;
}
int main()
{
while(~scanf("%d%d%d%lf",&n,&m,&s,&v))
{
int a,b;
double c,d,e,f;
memset(map1,0,sizeof(map1));
memset(map2,0,sizeof(map2));
memset(mapp,0,sizeof(mapp));
for(int i=1; i<=m; i++)
{
scanf("%d%d%lf%lf%lf%lf",&a,&b,&c,&d,&e,&f);
map1[a][b]=c,map2[a][b]=d;
map1[b][a]=e,map2[b][a]=f;
}
mapp[s]=v;
floyd();
if(floyd())
printf("YES\n");
else
printf("NO\n");
}
return 0;
}
Second question:
Arbitrage(poj2240)
Question: Given the exchange rates and modes of n currencies and m currencies, can we increase wealth through currency conversion?
Just like the previous question, floyd will look it up and compare it.
Code:
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
char a[35][35];
char a1[35];
double x;
char b[35];
double mapp[35][35];
int main()
{
int n,m,cas=1;
while(~scanf("%d",&n)&&n)
{
for(int i=1; i<=n; i++)
for(int j=1; j<=n; j++)
{
if(i==j)
mapp[i][j]=1;
}
for(int i=1; i<=n; i++)
scanf("%s",a[i]);
scanf("%d",&m);
for(int i=1; i<=m; i++)
{
scanf("%s%lf%s",a1,&x,b);
int j;
for(j=1; j<=n; j++)
if(strcmp(a[j],a1)==0)
break;
int k;
for(k=1; k<=n; k++)
if(strcmp(a[k],b)==0)
break;
mapp[j][k]=x;
}
for(int k=1; k<=n; k++)
for(int i=1; i<=n; i++)
for(int j=1; j<=n; j++)
{
if(mapp[i][j]<mapp[i][k]*mapp[k][j])
mapp[i][j]=mapp[i][k]*mapp[k][j];
}
printf("Case %d: ",cas++);
int i;
for(i=1; i<=n; i++)
if(mapp[i][i]>1) break;
if(i>n) printf("No\n");
else printf("Yes\n");
}
}