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. 树dp...吧 ZOJ 3949

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5568 Edge to the Root Time Limit: 1 Secon ...

  2. call_user_func 具体使用方法,实例说明

    <?php class Person{ public $name="jack"; public static function say(){ echo "ok&qu ...

  3. D - Binary Lexicographic Sequence URAL - 1081 (贪心)

    题目链接:https://cn.vjudge.net/contest/275079#problem/D 具体思路:首先,我们可以观察到1-n位数的种数连起来是一个很有规律的数列,然后我们开始倒序建立. ...

  4. [Gym-100625J] 搜索

    题目链接:https://cn.vjudge.net/problem/Gym-100625J 具体思路:首先,具体思路是两个人一起走到一个点,然后两个人按照同样的道路走出去,听了别人的思路,还有一种特 ...

  5. okhttp3使用详解

    http://blog.csdn.net/itachi85/article/details/51190687

  6. bootstrap分页查询传递中文参数到后台(get方式提交)

    <!--分页 --> <div style="width: 380px; margin: 0 auto; margin-top: 50px;"> <u ...

  7. linux下生成core dump文件方法及设置【转】

    转自:http://blog.csdn.net/mrjy1475726263/article/details/44116289 源自:http://andyniu.iteye.com/blog/196 ...

  8. 挂载cifs报错mount error(13): Permission denied(域账号访问时报错)

    Linux挂载Windows共享时,报以下错误: mount error(13): Permission deniedRefer to the mount.cifs(8) manual page (e ...

  9. 深入解析Mysql 主从同步延迟原理及解决方案

    MySQL的主从同步是一个很成熟的架构,优点为:①在从服务器可以执行查询工作(即我们常说的读功能),降低主服务器压力;②在从主服务器进行备份,避免备份期间影响主服务器服务;③当主服务器出现问题时,可以 ...

  10. 定位、判断、cookie的脚本案例

    Action(){ lr_think_time(20); lr_start_transaction("µã»÷ÊÂÏî°ìÀíÇé¿ö°´Å¥"); web_url("L ...