POJ-2240 arbitrage Bellman

Source: Internet
Author: User

Basically the same as POJ-1860, bellman asks whether there is a ring. Here, we learned other people's code to add the External Loop of the bellman algorithm directly, and then determine whether there is any update or release in this process.

The Code is as follows:

#include <cstring>#include <cstdlib>#include <cstdio>#include <map>#include <string>using namespace std;int N, pos, cnt;double dis[35];map<string,int>mp;struct Node{    int x, y;    double rate;}e[905];bool bellman(){    int flag;    fill(dis, dis+35, 1000);    for (int i = 1; i <= N; ++i) {        flag = 0;        for (int j = 1; j <= pos; ++j) {            if (dis[ e[j].x ] * e[j].rate - dis[ e[j].y ] > 1e-6) {                dis[ e[j].y ] = dis[ e[j].x ] * e[j].rate;                flag = 1;            }        }        if (!flag) {            break;        }    }    if (flag) {        return true;    }    else {        return false;    }}int main(){    int M, ca = 0;    char a[105], b[105];    double rate;    while (scanf("%d", &N), N) {        mp.clear();        pos = cnt = 0;        for (int i = 1; i <= N; ++i) {            scanf("%s", a);            mp[a] = ++cnt;        }        scanf("%d", &M);        for (int i = 1; i <= M; ++i) {            scanf("%s %lf %s", a, &rate, b);            ++pos;            e[pos].x = mp[a], e[pos].y = mp[b];            e[pos].rate = rate;        }        printf("Case %d: ", ++ca);        printf(bellman() ? "Yes\n" : "No\n");    }    return 0;}
Related Keywords:
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.