http://bailian.openjudge.cn/practice/2240/
#include <iostream>
#include <string>
#include <map>
using namespace std;
std::map<std::string, int> currency;
void floyd(float ER[31][31],int n,int cnt)
{
for (int k=0;k<n;k++)
{
for (int i=0;i<n;i++)
{
for (int j=0;j<n;j++)
{
if (ER[i][j]<ER[i][k]*ER[k][j])
{
ER[i][j] = ER[i][k] * ER[k][j];
}
}
}
}
for (int i=0;i<n;i++)
{
if (ER[i][i]>1)
{
std::cout << "Case " + to_string(cnt) +": Yes" << std::endl;
return;
}
}
std::cout << "Case " + to_string(cnt) + ": No" << std::endl;
return;
}
int main()
{
int n;
int cnt = 1;
while (cin >> n)
{
float ER[31][31] = {0.0};
if (n == 0)
{
return 0;
}
for (int i = 0; i < n; i++)
{
std::string tempString;
cin >> tempString;
currency.emplace(tempString, i);
}
int m;
cin >> m;
for (int i = 0; i < m; i++)
{
std::string tempFirstCurrency, tempSecondCurrency;
float er;
cin >> tempFirstCurrency >> er >> tempSecondCurrency;
ER[currency[tempFirstCurrency]][currency[tempSecondCurrency]] = er;
}
floyd(ER, n, cnt);
cnt++;
}
return 0;
}
可以参考这篇文章和这个题解
https://www.cnblogs.com/wangyuliang/p/9216365.html
https://www.luogu.org/problemnew/solution/SP9340