Arbitrage &&汇率转换问题有向图

本文深入探讨了建图在解决复杂问题中的核心作用,并详细介绍了如何构建和优化图结构,通过实例展示了其在实际场景中的应用。文章还强调了算法优化在提高效率方面的重要性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

这一题重点是建图,,越来越发现建图是解决问题的核心啊,,,,,,

#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <string.h>
#include <math.h>
int main()
{
    int n,m,x,y,i,j,k;
    char name[32][100],temp1[100],temp2[100];
    double map[32][32];
    int count = 0;
    while( ~scanf("%d",&n)!=EOF && n )
    {
        for(i=1; i<=n; i++)
        scanf("%s",name[i]);
        scanf("%d",&m); 
       for(i=1; i<=n; i++)
        {
            for(j=1;j<=n; j++)
            map[i][j] = 0.0;
            map[i][i] = 1.0;
        }
        while(m--)
        {    double price;
            int x=0,y=0;
            scanf("%s%lf%s",temp1,&price,temp2);
            for(i=1;i<=n; i++)
            {   if(x&&y) break;
                if( strcmp(temp1,name[i]) == 0 ) x = i;
                if( strcmp(temp2,name[i]) == 0 ) y = i;
            }
            map[x][y] = price;
        }
        for(k=1; k<=n; k++)
            for(i=1; i<=n; i++)
                for(j=1; j<=n; j++)
                    if( map[i][j] < map[i][k] * map[k][j] )
                        map[i][j] = map[i][k] * map[k][j];
            int success = 0;
        for(k=1; k<=n; k++)
            if( map[k][k] > 1 )
            {success = 1;break; } 
        if( success == 0 )
        printf("Case %d: No\n",++count);
        else
        printf("Case %d: Yes\n",++count);
        }
return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值