题目是需要求任意一种货币到自身如果可以套现,就输出YES,都不能套现就输出NO。
本题用bellman-ford做正权回路也可以,即找到一条就输出YES,否则输出NO。
参考小优姐用STL容器来建议字符串与整数的一一对应关系,题目瞬间难度下降一半,真是惭愧。
<span style="font-size:18px;">#include<iostream>
#include<fstream>
#include<map>
using namespace std;
int n,m;
map<string, int> STL;
char str[50], str1[50], str2[50];
double dist[31][31],rate;
void floyd()
{
for (int k = 1; k <= n; k++)
{
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= n; j++)
{
if (dist[i][j]<dist[i][k]*dist[k][j])
{
dist[i][j] = dist[i][k] * dist[k][j];
}
}
}
}
}
int main()
{
//ifstream in("text.txt");
int cases = 0;
while (cin >> n && n != 0)
{
cases++;
for (int i = 1; i <= n; i++)
{
cin >> str;
STL[str] = i;
dist[i][i] = 1;
}
cin >> m;
for (int i = 1; i <= m; i++)
{
cin >> str1>>rate>>str2;
dist[STL[str1]][STL[str2]] = rate;
}
floyd();
//output
bool flag = false;
for (int i = 1; i <= n; i++)
{
if (dist[i][i] > 1)
{
flag = true;
break;
}
}
if (flag)
cout << "Case " << cases << ": Yes" << endl;
else
cout << "Case " << cases << ": No" << endl;
}
//system("pause");
return 0;
}</span>