1. 程式人生 > 實用技巧 >【最短路-判斷正權環 Floyd】Currency Exchange POJ - 1860

【最短路-判斷正權環 Floyd】Currency Exchange POJ - 1860

Currency Exchange POJ - 1860

題意:

這題和Arbitrage POJ - 2240一個意思,區別在於本題給定了起始貨幣種類及具體數量,並且在兌換其他貨幣前需要扣一筆手續費,以及是雙向邊。

思路:

Arbitrage POJ - 2240一樣的Floyd解法。

Floyd

int n, m, s;
double v;
double edges[maxn][maxn];
double com[maxn][maxn];
double d[maxn];
double temp[maxn];

bool floyd() {
    for (int i = 1; i <= n; i++) {
        temp[i] = d[i];
    }
    for (int k = 1; k <= n; k++) {
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= n; j++) {
                if ((d[i]-com[i][j]) * edges[i][j] > d[j]) {
                    d[j] = (d[i] - com[i][j]) * edges[i][j];
                }
            }
        }
    }
    for (int i = 1; i <= n; i++) {
        if (d[i] > temp[i]) return true;
    }
    return false;
}

int main()
{
    // ios::sync_with_stdio(false);
    /// int t; cin >> t; while (t--) {
    cin >> n >> m >> s >> v;
    memset(edges, INF, sizeof(edges));
    memset(d, 0, sizeof(INF));
    for (int i = 1; i <= m; i++) {
        int a, b;
        double ab_rate, ab_com, ba_rate, ba_com;
        cin >> a >> b >> ab_rate >> ab_com >> ba_rate >> ba_com;
        edges[a][b] = ab_rate;
        com[a][b] = ab_com;
        edges[b][a] = ba_rate;
        com[b][a] = ba_com;
    }
    d[s] = v;
    floyd();
    if (floyd()) cout << "YES";
    else cout << "NO";
    return 0;
}