poj 2240 zoj1092 Arbitrage

#include <iostream>
#include <cstdio>
#include <cstring>

using namespace std;

const int MAXN = 35;
const int MAXM = 1000;

struct exchange
{
    int ci, cj;
    double cij;
}Ex[MAXM];

char name[MAXN][20], a[20], b[20];
double x;

double maxdist[MAXN];
int n, m;

int ReadCase()
{
    int i, j, k;
    cin>>n;
    if(n == 0)
        return 0;
    for(i = 0; i < n; ++i)
        cin>>name[i];
    cin>>m;
    for(i = 0; i < m; ++i)
    {
        cin >> a >> x >> b;
        for(j = 0; strcmp(a, name[j]); ++j)
            ;
        for(k = 0; strcmp(b, name[k]); ++k)
            ;
        Ex[i].ci = j, Ex[i].cij = x, Ex[i].cj = k;
    }
    return 1;
}

bool Bellman_Ford( int v0 )
{
    bool flag = false;
    memset(maxdist, 0, sizeof(maxdist));
    maxdist[v0] = 1;
    for(int k = 1; k <= n; ++k)
    {
        for(int i = 0; i < m; ++i)
            if(maxdist[Ex[i].ci] * Ex[i].cij > maxdist[Ex[i].cj] )
                maxdist[Ex[i].cj] = maxdist[ Ex[i].ci ] * Ex[i].cij;
    }
    if(maxdist[v0] > 1.0)
        flag = true;
    return flag;
}

int main()
{
    int kcase = 1;
    while(ReadCase())
    {
        bool flag = false;
        for(int i = 0; i < n; ++i)
        {
            flag = Bellman_Ford( i );
            if(flag)
                break;
        }
        if(flag)
            cout<<"Case "<<kcase++<<": Yes"<<endl;
        else
            cout<<"Case "<<kcase++<<": No"<<endl;
    }
    return 0;
}

你可能感兴趣的:(最短路径)