1860的思路是将可以换得的不同种的货币的数量当作节点,每个兑换点当成边,然后我抄了个算法导论里面的Bellman-Ford算法,一次就过了。看discussion里面很多讨论精度的,我想都没想过……

2240是更简单的一个bellman-ford,基本和1860一样,就只多了个map容器的应用而已。

以下是1860:

#include <iostream>
using namespace std; struct Point{
int a, b;
double Rab, Cab, Rba, Cba;
}; int main()
{
int n, m, s;
double money;
Point point[];
cin >> n >> m >> s >> money;
for (int i = ; i < m; i++){
cin >> point[i].a >> point[i].b
>> point[i].Rab >> point[i].Cab
>> point[i].Rba >> point[i].Cba;
}
double node[];
memset(node, , sizeof(node));
node[s] = money;
for (int j = ; j <= n - ; j++){
for (int i = ; i < m; i++){
if (node[point[i].a] < (node[point[i].b] - point[i].Cba) * point[i].Rba)
node[point[i].a] = (node[point[i].b] - point[i].Cba) * point[i].Rba;
if (node[point[i].b] < (node[point[i].a] - point[i].Cab) * point[i].Rab)
node[point[i].b] = (node[point[i].a] - point[i].Cab) * point[i].Rab;
}
}
bool flag = true;
for (int i = ; i < m; i++){
if (node[point[i].a] < (node[point[i].b] - point[i].Cba) * point[i].Rba
|| node[point[i].b] < (node[point[i].a] - point[i].Cab) * point[i].Rab){
flag = false;
break;
}
}
cout << (flag ? "NO" : "YES") << endl;
return ;
}

2240:

#include <iostream>
#include <map>
#include <string>
using namespace std; struct Edge{
int type1, type2;
double rate;
}; int main()
{
int n;
int testCase = ;
while (cin >> n && n != ){
double node[];
Edge edge[];
map<string, int> currency;
for (int i = ; i < n; i++){
string type;
cin >> type;
currency[type] = i;
}
int m;
cin >> m;
for (int i = ; i < m; i++){
string type1, type2;
double r;
cin >> type1 >> r >> type2;
edge[i].type1 = currency[type1];
edge[i].type2 = currency[type2];
edge[i].rate = r;
}
for (int i = ; i < n; i++){
node[i] = ;
}
node[] = ;
for (int i = ; i <= n - ; i++){
for (int j = ; j < m; j++){
double tmp = node[edge[j].type1] * edge[j].rate;
if (node[edge[j].type2] < tmp){
node[edge[j].type2] = tmp;
}
}
}
bool flag = false;
for (int i = ; i < m; i++){
if (node[edge[i].type2] < node[edge[i].type1] * edge[i].rate){
flag = true;
break;
}
}
cout << "Case " << testCase << ": " << (flag ? "Yes" : "No") << endl;
testCase++;
}
return ;
}

poj1860 & poj2240(Bellman-Ford)的更多相关文章

  1. poj1860 bellman—ford队列优化 Currency Exchange

    Currency Exchange Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 22123   Accepted: 799 ...

  2. ACM/ICPC 之 最短路径-Bellman Ford范例(POJ1556-POJ2240)

    两道Bellman Ford解最短路的范例,Bellman Ford只是一种最短路的方法,两道都可以用dijkstra, SPFA做. Bellman Ford解法是将每条边遍历一次,遍历一次所有边可 ...

  3. uva 558 - Wormholes(Bellman Ford判断负环)

    题目链接:558 - Wormholes 题目大意:给出n和m,表示有n个点,然后给出m条边,然后判断给出的有向图中是否存在负环. 解题思路:利用Bellman Ford算法,若进行第n次松弛时,还能 ...

  4. Bellman—Ford算法思想

    ---恢复内容开始--- Bellman—Ford算法能在更普遍的情况下(存在负权边)解决单源点最短路径问题.对于给定的带权(有向或无向)图G=(V,E),其源点为s,加权函数w是边集E的映射.对图G ...

  5. Bellman - Ford 算法解决最短路径问题

    Bellman - Ford 算法: 一:基本算法 对于单源最短路径问题,上一篇文章中介绍了 Dijkstra 算法,但是由于 Dijkstra 算法局限于解决非负权的最短路径问题,对于带负权的图就力 ...

  6. poj1860 兑换货币(bellman ford判断正环)

    传送门:点击打开链接 题目大意:一个城市有n种货币,m个货币交换点,你有v的钱,每个交换点只能交换两种货币,(A换B或者B换A),每一次交换都有独特的汇率和手续费,问你存不存在一种换法使原来的钱更多. ...

  7. Dijkstra算法与Bellman - Ford算法示例(源自网上大牛的博客)【图论】

    题意:题目大意:有N个点,给出从a点到b点的距离,当然a和b是互相可以抵达的,问从1到n的最短距离 poj2387 Description Bessie is out in the field and ...

  8. POJ 2240 Arbitrage (Bellman Ford判正环)

    Arbitrage Time Limit: 1000MS   Memory Limit: 65536K Total Submissions:27167   Accepted: 11440 Descri ...

  9. poj1860(Bellman—fold)

    题目连接:http://poj.org/problem?id=1860 Description Several currency exchange points are working in our ...

  10. ACM/ICPC 之 Bellman Ford练习题(ZOJ1791(POJ1613))

    这道题稍复杂一些,需要掌握字符串输入的处理+限制了可以行走的时间. ZOJ1791(POJ1613)-Cave Raider //限制行走时间的最短路 //POJ1613-ZOJ1791 //Time ...

随机推荐

  1. 莫队+分块 BZOJ 3809

    3809: Gty的二逼妹子序列 Time Limit: 80 Sec  Memory Limit: 28 MBSubmit: 1634  Solved: 482[Submit][Status][Di ...

  2. ItemCF_基于物品的协同过滤

    ItemCF_基于物品的协同过滤 1.    概念 2.    原理 如何给用户推荐? 给用户推荐他没有买过的物品--103 3.    java代码实现思路 数据集: 第一步:构建物品的同现矩阵 第 ...

  3. jQuery网格插件 ParamQuery

    ParamQuery是一种轻量级的jQuery网格插件,基于用于用户界面控制.具有一致API的优秀设计模式jQueryUI Widget factory创建,能够在网页上展示各种类似于Excel和Go ...

  4. Java爬虫(二)

    上一篇简单的实现了获取url返回的内容,在这一篇就要第返回的内容进行提取,并将结果保存到html中.而且这个爬虫是基于python爬虫的java语言实现,其逻辑大致相同. 一 . 需求: 抓取主页面: ...

  5. cin循环输入控制问题

    之前写一个简单的输入节点值自动生成链表的测试程序,发现cin的输入控制好像在VC++6.0和VS2010中不一样,特此记录. 现在有以下代码: vector<int> ivec; int ...

  6. Vuex-Mutation

    更改 Vuex 的 store 中的状态的唯一方法是提交 mutation.Vuex 中的 mutation 非常类似于事件:每个 mutation 都有一个字符串的 事件类型 (type) 和 一个 ...

  7. inet_addr_onlink

    /* 根据指定设备的ip配置块,判断地址a,b是否在同一子网 */ /* --邻居项要求,在同一子网中的两个设备, 至少有一个接口有相同的子网配置, --也就是说对端的in_dev->ifa_l ...

  8. 【技术分享】ReBreakCaptcha:利用谷歌来破解谷歌的验证码

    概述 从2016年开始,我就在琢磨寻找一种新的绕过谷歌验证码v2的方法会有多难,如果这种方法能够适用于任何环境而不仅仅是针对特定的案例,那这种方法将是非常理想的.接下来我将向你介绍ReBreakCap ...

  9. Codeforces Round #455 (Div. 2)

    Codeforces Round #455 (Div. 2) A. Generate Login 题目描述:给出两个字符串,分别取字符串的某个前缀,使得两个前缀连起来的字符串的字典序在所有方案中最小, ...

  10. c++各种排序的简单实现

    /* 直插排序 */ void InsertSort(vector<int> &arr){ for(int i = 1;i < arr.size();++i){ for(in ...