# [数据结构] 货币套汇（图路径）

2~n+1行，n种货币的名称。

n+2~n+m+1行，每行有3 个数据项ci，rij 和cj ，表示货币ci 和cj的兑换率为 rij。

2
3 3
USDollar
BritishPound
FrenchFranc
USDollar 0.5 BritishPound
BritishPound 10.0 FrenchFranc
FrenchFranc 0.21 USDollar
3 6
USDollar
BritishPound
FrenchFranc
USDollar 0.5 BritishPound
USDollar 4.9 FrenchFranc
BritishPound 10.0 FrenchFranc
BritishPound 1.99 USDollar
FrenchFranc 0.09 BritishPound
FrenchFranc 0.19 USDollar

YES
NO

#include <iostream>

using namespace std;

#define MAX_NUM 20

{
private:
double matrix[MAX_NUM][MAX_NUM];
string node[MAX_NUM];
int node_num;
int arc_num;
int conn_num;
double value; ///路径汇率乘积
int flag; ///是否能套汇
char type;
int Visit[MAX_NUM];
void DFS(int v);
public:
{
type = ty;
node_num = n;
conn_num=0;
flag=0;
}
int getIndex(string s);
void getMatrix();
void display();
void DFSTraverse();
};

{
for(int i=0; i<node_num; i++)
{
if(node[i]==s)
return i;
}
return -1;
}

{
for(int i=0; i<node_num; i++)
for(int j=0; j<node_num; j++)
matrix[i][j] = 0;

cin>>arc_num;
for(int i=0; i<node_num; i++)
{
string s1;
cin>>s1;
node[i] = s1;
}
for(int i=0; i<arc_num; i++)
{
string s1, s2;
double currency;
int index1, index2;
cin>>s1;
cin>>currency;
cin>>s2;
index1 = getIndex(s1);
index2 = getIndex(s2);
if(type=='D')
{
matrix[index1][index2] = currency;
}
else if(type=='U')
{ ///这题用不到
matrix[index1][index2]++;
matrix[index2][index1]++;
}
}
}

{
int v, k;
int i;

for(k=0; k<node_num; k++)
{
int counter=0;
value=1.0;
v=k;
for(i=0; i<node_num; i++)
{
if(i==v)
Visit[i] = 2; ///表示起始点
else
Visit[i] = 0;
}

do
{
//cout<<"v="<<v<<" ";
if(Visit[v]==0 || Visit[v]==2)
{
counter++;
if(counter>1) break; //表示从该点出发无法形成环，直接跳过
DFS(v);
}
v = (v+1) % node_num;
}while(v!=k);
//cout<<endl;
//cout<<counter<<endl;
}

if(flag==1)
cout<<"YES";
else
cout<<"NO";
cout<<endl;
}

{
int w, i, k;
//cout<<node[v]<<"->";
if(Visit[v]==0)
Visit[v]=1;

for(i=0; i<node_num; i++)

k = 0; ///寻找其相邻的点
for(i=0; i<node_num; i++)
{
if(matrix[v][i]!=0) ///小心
{
k++;
}
}

i=0;
{ ///访问其所有相邻的点
if(Visit[w]==0)
{
value = value*matrix[v][w];
DFS(w);
}

else if(Visit[w]==2) //如果可以回到初始点形成环
{
double temp_value = value; //判定边乘积是否大于1
temp_value = temp_value*matrix[v][w];
//cout<<temp_value<<endl;
if(temp_value>1.0)
{
flag = 1;
}
}
i++;
}

}

{
int i, j;
for(i=0; i<node_num; i++)
{ ///输出点
cout<<node[i];
if(i!=node_num-1)
cout<<"\t";
}
cout<<endl;

for(i=0; i<node_num; i++)
{
for(j=0; j<node_num; j++)
{
cout<<matrix[i][j];
if(j!=node_num-1)
cout<<"\t";
}
cout<<endl;
}
//cout<<conn_num<<endl;
}

int main()
{
int t;
cin>>t;
while(t--)
{
//char ty;
int n;
cin>>n;
test.getMatrix();
//test.display();
test.DFSTraverse();
//cout<<endl;
}
return 0;
}


• 0
点赞
• 0
评论
• 2
收藏
• 一键三连
• 扫一扫，分享海报

07-25 1961
06-15 997
12-12 1613
01-04 229
10-03 728
03-14 795