POJ 1860 - Currency Exchange

POJ - 1860

一种货币就是一个点

一个“兑换点”就是图上两种货币之间的一个兑换方式,是双边,但A到B的汇率和手续费可能与B到A的汇率和手续费不同。

唯一值得注意的是权值,当拥有货币A的数量为V时,A到A的权值为K,即没有兑换

而A到B的权值为(V-Cab)*Rab

本题是“求最大路径”,之所以被归类为“求最小路径”是因为本题题恰恰与bellman-Ford算法的松弛条件相反,求的是能无限松弛的最大正权路径,但是依然能够利用bellman-Ford的思想去解题。

因此初始化dis(S)=V 而源点到其他点的距离(权值)初始化为无穷小(0),当s到其他某点的距离能不断变大时,说明存在最大路径;如果可以一直变大,说明存在正环。判断是否存在环路,用Bellman-Ford和spfa都可以。

floyd

//#ifndef ONLINE_JUDGE
//#pragma warning(disable : 4996)
//#endif // !ONLINE_JUDGE
#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
#define ms(a,b) memset(a,b,sizeof(a));

const int N = 100 + 10;
double map[105] = { 0 }, g1[105][105] = { 0 }, g2[101][101] = { 0 }, v;
int n, m, s;

int floyd()
{
    int i, j, k;
    double d[105];
    for (i = 1; i <= n; i++)d[i] = map[i];
    for (k = 1; k <= n; k++)
        for (i = 1; i <= n; i++)
            for (j = 1; j <= n; j++)
                if ((map[i] - g2[i][j])*g1[i][j] > map[j])map[j] = (map[i] - g2[i][j])*g1[i][j];
    for (i = 1; i <= n; i++)
        if (d[i] < map[i])return 1;
    return 0;
}

//Submain/
int main() {
#ifndef ONLINE_JUDGE
    freopen("in,txt", "r", stdin);
    freopen("out,txt", "w", stdout);
#endif // !ONLINE_JUDGE
    //原来不要循环输入,搞得我WA无数次
    cin >> n >> m >> s >> v;
    int i, j, k;
    for (i = 1; i <= m; i++)
    {
        int a, b;
        double c, d, e, f;
        cin >> a >> b >> c >> d >> e >> f;
        g1[a][b] = c, g2[a][b] = d;
        g1[b][a] = e, g2[b][a] = f;
    }
    map[s] = v;
    floyd();
    if (floyd())cout << "YES\n";
    else cout << "NO\n";
#ifndef ONLINE_JUDGE
    fclose(stdin);
    fclose(stdout);
    system("out.txt");
#endif // !ONLINE_JUDGE

    return 0;
}

/End Sub//

spfa

#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;
const int maxx = 105;
bool vis[maxx];
int head[maxx], cnt[maxx];
float dis[maxx];
int ct;
struct edge //邻接表存储图
{
    int to, nxt;
    float w1, w2;
    void set(float _w1, float _w2) {
        w1 = _w1; w2 = _w2;
    }
}e[maxx << 2];

void init(int n) {
    for (int i = 1; i <= n; ++i)
        head[i] = -1;
}

void add_edge(int u, int v, float r1, float r2) {
    ++ct;
    e[ct].to = v; e[ct].nxt = head[u]; e[ct].set(r1, r2);
    head[u] = ct;
}

bool spfa(int s, int n) {
    queue<int> q;
    q.push(s);
    float tmp;

    while (!q.empty()) {
        s = q.front(); q.pop();
        vis[s] = false;
        for (int i = head[s]; i != -1; i = e[i].nxt) {
            int nxt = e[i].to;
            if (!vis[nxt] && (tmp = (dis[s] - e[i].w2) * e[i].w1) > dis[nxt]) {
                dis[nxt] = tmp;
                ++cnt[nxt];
                vis[nxt] = true;
                q.push(nxt);
                if (cnt[nxt] >= n) return true;
            }
        }
    }
    return false;
}

int main() {
    int n, m, s; float v;
    scanf("%d%d%d%f", &n, &m, &s, &v);
    init(n);
    for (int i = 0; i < m; ++i) {
        int from, to;
        float r12, r13, r21, r31;
        scanf("%d%d%f%f%f%f", &from, &to, &r12, &r13, &r21, &r31);
        add_edge(from, to, r12, r13);
        add_edge(to, from, r21, r31);
    }

    dis[s] = v;
    printf("%s\n", spfa(s, n) ? "YES" : "NO");
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值