poj 2240 Arbitrage 最短路

简介:

    其实就是找到乘积最大的一条回路,如果大于1就是YES否则就是NO,用bellman N4都能过……


/*
author:jxy
lang:C/C++
university:China,Xidian University
**If you need to reprint,please indicate the source**
*/
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <queue>
#include <map>
#define INF 1E9
using namespace std;
map<string,int> hash;
double dis[31][31];
double d[31];
int n,m;
bool input()
{
    hash.clear();
    scanf("%d",&n);
    if(!n)return 0;
    int i;
    string a,b;
    double t;
    for(i=0;i<n;i++)
    {
        cin>>a;
        hash[a]=i;
    }
    scanf("%d",&m);
    for(i=0;i<m;i++)
    {
        cin>>a>>t>>b;
        dis[hash[a]][hash[b]]=t;
    }
    return 1;
}
bool bellman(int V0)
{
    memset(d,0,sizeof(d));
    d[V0]=1;
    int v,u,i;
    double t;
    for(i=0;i<n;i++)
        for(v=0;v<n;v++)
         for(u=0;u<n;u++)
           if(d[v]<(t=d[u]*dis[u][v]))
               d[v]=t;
    if(d[V0]>1)return 1;
    return 0;
}
bool solve()
{
    int i,v,u;
    for(i=0;i<n;i++)
        if(bellman(i))return 1;
    return 0;
}
int main()
{
    int T=0;
    while(input())
        printf("Case %d: %s\n",++T,solve()?"Yes":"No");
}


目录
相关文章
|
8月前
poj 1990 MooFest 树状数组
题意就是有N头牛,每头牛都有一个坐标和声调值(x, v),两头牛之间通讯要花费的能量是他们的距离乘以最大的一个音调值,现在要任意两头牛之间都相互通讯一次,求总共需要花费多少能量?
26 0
|
人机交互
POJ-2524,Ubiquitous Religions(并查集模板题)
POJ-2524,Ubiquitous Religions(并查集模板题)
|
人工智能 网络架构
|
并行计算 算法 Java
HDU 1874 畅通工程续【Floyd算法实现】
畅通工程续 Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 53806    Accepted Submission(s): 20092 Problem Description 某省自从实行了很多年的畅通工程计划后,终于修建了很多路。
1071 0
线段树-poj-2823
Sliding Window Description An array of size n ≤ 106 is given to you. There is a sliding window of size k which is moving from the very left of the array to the very right. You can only see the k
1064 0