最短路径问题

Arbitrage(套汇)

题目描述
Arbitrage is the use of discrepancies in currency exchange rates to transform one unit of a currency into more than one unit of the same currency. For example, suppose that 1 US Dollar buys 0.5 British pound, 1 British pound buys 10.0 French francs, and 1 French franc buys 0.21 US dollar. Then, by converting currencies, a clever trader can start with 1 US dollar and buy 0.5 * 10.0 * 0.21 = 1.05 US dollars, making a profit of 5 percent.
Your job is to write a program that takes a list of currency exchange rates as input and then determines whether arbitrage is possible or not.

输入
The input file will contain one or more test cases. Om the first line of each test case there is an integer n (1<=n<=30), representing the number of different currencies. The next n lines each contain the name of one currency. Within a name no spaces will appear. The next line contains one integer m, representing the length of the table to follow. The last m lines each contain the name ci of a source currency, a real number rij which represents the exchange rate from ci to cj and a name cj of the destination currency. Exchanges which do not appear in the table are impossible.
Test cases are separated from each other by a blank line. Input is terminated by a value of zero (0) for n.

输出
For each test case, print one line telling whether arbitrage is possible or not in the format “Case case: Yes” respectively “Case case: No”.

样例输入
3
USDollar
BritishPound
FrenchFranc
3
USDollar 0.5 BritishPound
BritishPound 10.0 FrenchFranc
FrenchFranc 0.21 USDollar

3
USDollar
BritishPound
FrenchFranc
6
USDollar 0.5 BritishPound
USDollar 4.9 FrenchFranc
BritishPound 10.0 FrenchFranc
BritishPound 1.99 USDollar
FrenchFranc 0.09 BritishPound
FrenchFranc 0.19 USDollar

0

样例输出
Case 1: Yes
Case 2: No

/**
    套汇, 通过不同币种的汇率互换赚取差价
    <bits/stdc++.h> 这个头文件国内OJ貌似不支持,下同
*/
#include <bits/stdc++.h>
using namespace std;
int main() {
    char name[30][300];
    char from[300], to[300];
    double g[30][30];
    double rate;
    int flag, n, m, cases = 1, i, j, k;
    while (cin >> n) {
        if (!n) break;
        memset(g, 0, sizeof(g));
        for (i = 0; i < n; i++) {
            g[i][i] = 1;
        }
        for (i = 0; i < n; i++) {
            cin >> name[i];
        }
        cin >> m;
        for (int i = 0; i < m; i++) {
            cin >> from >> rate >> to;
            for (j = 0; j < n; j++) {
                if (!strcmp(from, name[j])) {
                    break;
                }
            }
            for (k = 0; k < n; k++) {
                if (!strcmp(to, name[k])) {
                    break;
                }
            }
            g[j][k] = rate;
        }
        for (k = 0; k < n; k++) {   // Floyd算法
            for (i = 0; i < n; i++) {
                for (j = 0; j < n; j++) {
                    rate = g[i][k] * g[k][j];
                    if (g[i][j] < rate) {
                        g[i][j] = rate;
                    }
                }
            }
        }
        flag = 0;
        for (i = 0; i < n; i++) {
            if (g[i][i] > 1) {
                flag = 1;
                break;
            }
        }
        printf("Case %d: ", cases++);
        printf("%s\n", flag ? "Yes" : "No");
    }
    return 0;
}

求最近两个点的最小距离

题目描述

给定若干个二维空间的点坐标,求出最近两个点的距离
输入

输出两行,第一行点的个数n,n<10000;

第2行 到第n+1行为n个点的坐标;

输出

输出最近两个点的距离,要求结果保留四位小数。
样例输入
4
1 1
5 5
8 9
1 2
样例输出
1.0000
提示

使用分治策略

#include <bits/stdc++.h>
using namespace std;
typedef struct Node {
    int x;
    int y;
};
Node s[10009];
int cmp(Node a, Node b) {
    return a.x < b.x;
}
double binary(int left, int right) {
    if (left == right) {
        return INT_MAX;
    } else if (left + 1 == right) {
        return sqrt( (s[left].x-s[right].x)*(s[left].x-s[right].x) +
                (s[left].y-s[right].y)*(s[left].y-s[right].y) );
    }
    int mid = (left + right) / 2;
    double minLeft = binary(left, mid);
    double minRight = binary(mid + 1, right);
    double minDis = (minLeft < minRight) ? minLeft : minRight;
    double minSpan = INT_MAX;
    for (int i = left; i <= mid; i++) {         // 暴力枚举
        for (int j = mid + 1; j <= right; j++) {
            double tmp = sqrt( (s[i].x-s[j].x)*(s[i].x-s[j].x) +
                (s[i].y-s[j].y)*(s[i].y-s[j].y) );
            if (minSpan < tmp) {
                minSpan = tmp;
            }
        }
    }
    minDis = (minDis < minSpan) ? minDis : minSpan;
    return minDis;
}
int main() {
    int n;
    while (cin >> n) {
        for (int i = 0; i < n; i++) {
            cin >> s[i].x >> s[i].y;
        }
        sort(s, s+n, cmp);
        double ret = binary(0, n);
        printf("%.4lf\n", ret);
    }
    return 0;
}

邮递员

题目描述
有一个邮递员要在n个城市之间来回送信。但有的城市之间有大路相连而有的没有路。现在要由一个城市到另一个城市送信,中途最少要经过多少个其它的城市呢?

输入
第一行是n,k(1<=n<=100, 1<=k<=2000),接下来就是k行。
这k行每行有两个数a,b(1 <= a,b <= n),表示城市a和b之间有大路。
k行以后就是两个数p和q。
当n,k输入都为0的时候结束。

输出
输出从城市p到城市q之间最少要经过的其它的城市的数目。
如果p和q之间不连通则输出”No solution”

样例输入
6 6
1 4
1 2
2 3
3 4
5 4
5 6
1 6
0 0
样例输出
2

#include <bits/stdc++.h>
using namespace std;
#define TAG 999999
int n, k, d[109][109];
int main() {
    int x, y;
    while (cin >> n >> k) {
        if (n == 0 && k == 0)break;
        for (int i = 0; i < 109; i++) {
            for (int j = 0; j < 109; j++) {
                d[i][j] = TAG;
            }
        }
        for (int i = 1; i <= k; i++) {
            cin >> x >> y;
            d[x][y] = 1;
            d[y][x] = 1;
        }
        int p, q;
        cin >> p >> q;
        for (int u = 1; u <= n; u++) {      // Floyd算法
            for (int v = 1; v <= n; v++) {
                for (int w = 1; w <= n; w++) {
                    if (d[v][u] + d[u][w] < d[v][w]) {
                        d[v][w] = d[v][u] + d[u][w];
                    }
                }
            }
        }
        if (d[p][q] == TAG) {
            cout << "No solution\n";
        } else {
            cout << d[p][q] - 1 << endl;
        }
    }
    return 0;
}

鬼子进村

题目描述
抗日战争时期,岗村宁次带领一队鬼子要想占领赵庄,之前得到情报知道到赵庄的地图,和各个路上埋的地雷和陷阱的个数,以及每个地雷和陷阱能有多大的杀伤力也了如指掌,同时也知道赵庄有多少民兵。从战斗素养上看一个民兵和一个鬼子的战斗力相同。岗村宁次想知道以自己现有的兵力能否拿下赵庄(即到达赵庄后鬼子数比民兵数多)。
已知每个地雷能杀死3个鬼子,每个陷阱能杀死2个鬼子。

输入
第一行两个整数,M,N(<1000),M表示鬼子数,N表示民兵数。
第二行两个整数,一个表示出发点A,另一个表示终点B(赵庄)。
第三行一个整数N,表示A与B之间有N条路径。
接下来的N行,每行4个数,分别表示路径的起点,路径的终点,这条路径上地雷数,这条路径上陷阱数。

输出
如果鬼子可能占领赵庄则输出 “Y”,否则输出“N”。

样例输入
16 10
1 3
3
1 2 0 1
2 3 1 0
1 3 1 2

样例输出
Y

#include <bits/stdc++.h>
#define TAG 999999
using namespace std;
int d[1009][1009];
int main() {
    int m, n, a, b, k, x, y, p, q;
    while (cin >> m >> n) {
        cin >> a >> b >> k;
        for (int i = 0; i < 1009; i++) {
            for (int j = 0; j < 1009; j++) {
                d[i][j] = TAG;
            }
        }
        for (int i = 0; i < k; i++) {
            cin >> x >> y >> p >> q;
            d[x][y] = 3 * p + 2 * q;
        }
        for (int u = 0; u < n; u++) {       // Floyd算法
            for (int v = 0; v < n; v++) {
                for (int w = 0; w < n; w++) {
                    if (d[v][u] + d[u][w] < d[v][w]) {
                        d[v][w] = d[v][u] + d[u][w];
                    }
                }
            }
        }
        printf("%s\n", (m-d[a][b]) > n ? "Y" : "N");
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值