文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>Floyd算法

Floyd算法

时间:2010-04-17  来源:buptstehc

题目来源:Arbitrage

    第一次写floyd, 跟Currency Exchange有些类似。


#include <stdio.h>
#include <string.h>

double d[31][31], r;
char cur[31][50], src[50], dest[50];
int n, m;

int find(char *str)
{
    int i;

    for(i = 0; i < n; i++)
        if(!strcmp(str, cur[i]))
            return i;
}

void floyd(void)
{
    int u, v, w;

    for(u = 0; u < n; u++)
        for(v = 0; v < n; v++)
            for(w = 0; w < n; w++)
            {
                if(d[v][w] < d[v][u] * d[u][w])
                    d[v][w] = d[v][u] * d[u][w];
            }
}

int main(void)
{
    int i, j, sr, dt, flag, sum;

    sum = 0;
    while(scanf("%d", &n) != EOF && n)
    {
        sum++;
        for(i = 0; i < n; i++)
            for(j = 0; j < n; j++)
                d[i][j] = 0;

        for(i = 0; i < n; i++)
            scanf("%s", cur[i]);

        flag = 0;
        scanf("%d", &m);
        for(i = 0; i < m; i++)
        {
            scanf("%s%lf%s", src, &r, dest);
            sr = find(src);
            dt = find(dest);

            if(d[sr][dt] < r)
                d[sr][dt] = r;

            /*if adding value after transforming itself, then no need to floyd. */
            if(!flag && (sr == dt) && (r > 1))
                flag = 1;
        }

        if(flag)
            printf("Case %d: Yes\n", sum);
        else
        {
            floyd();

            for(i = 0; i < n; i++)
            {
                if(d[i][i] > 1)
                {
                    printf("Case %d: Yes\n", sum);
                    break;
                }
            }

            if(i == n)
                printf("Case %d: No\n", sum);
        }
    }

    return 0;
}


相关阅读 更多 +
排行榜 更多 +
辰域智控app

辰域智控app

系统工具 下载
网医联盟app

网医联盟app

运动健身 下载
汇丰汇选App

汇丰汇选App

金融理财 下载