POJ 2240 Arbitrage——spfa判正环||flody
spfa:判正环只要遍历每一个点判断松弛次数是否大于n次即可,注意此时求得是最长路,dis数组清零,dis[s]设为1;
flody:先把dis数组设0,主对角线设1, 然后跑一边flody,最后查看主对角线元素是否有大于1的就可以
spfa:
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <queue>
#include <map>
#include <utility>
using namespace std;
typedef pair<int, double> P;
const int INF = 0x3f3f3f3f;
const int maxn = 1e3 + 10;
bool vis[maxn];
string s1, s2;
int flag, n, num[maxn];
double temp, dis[maxn];
vector<P> G[maxn];
map<string, int> m;
bool spfa(int s) {for (int i = 1; i <= n; i++) dis[i] = 0, vis[i] = false, num[i] = 0;dis[s] = 1, vis[s] = true, num[s] = 1;queue<int> q; q.push(s);while (!q.empty()) {int u = q.front(); q.pop();vis[u] = false;for (int i = 0; i < G[u].size(); i++) {int v = G[u][i].first;double val = G[u][i].second;if (dis[v] < dis[u] * val) {dis[v] = dis[u] * val;if (!vis[v]) {vis[v] = true, num[v]++;q.push(v);if (num[v] > n) return true;}}}}return false;
}
int main() {while (~scanf("%d", &n) && n) {for (int i = 1; i <= n; i++) G[i].clear();m.clear();int cnt = 0;for (int i = 1; i <= n; i++) {cin >> s1;if (!m[s1]) m[s1] = ++cnt;}int t; scanf("%d", &t);while (t--) {cin >> s1 >> temp >> s2;int u = m[s1], v = m[s2];G[u].push_back(make_pair(v, temp));//G[v].push_back(make_pair(u, 1.0 / temp));}bool ok = false;for (int i = 1; i <= n; i++) {if (spfa(i)) { ok = true; break; }}printf("Case %d: %s\n", ++flag, ok ? "Yes" : "No");}return 0;
}
flody:
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <queue>
#include <map>
using namespace std;
const int maxn = 1e3 + 10;
string s1, s2;
int flag, n;
double temp, G[maxn][maxn];
map<string, int> m;
int main() {while (~scanf("%d", &n) && n) {for (int i = 1; i <= n; i++) {for (int j = 1; j <= n; j++) {G[i][j] = (i == j) ? 1 : 0;}}m.clear();int cnt = 0;for (int i = 1; i <= n; i++) {cin >> s1;if (!m[s1]) m[s1] = ++cnt;}int t; scanf("%d", &t);while (t--) {cin >> s1 >> temp >> s2;int u = m[s1], v = m[s2];G[u][v] = temp;}for (int k = 1; k <= n; k++) {for (int i = 1; i <= n; i++) {for (int j = 1; j <= n; j++) {G[i][j] = max(G[i][j], G[i][k] * G[k][j]);}}}bool ok = false;for (int i = 1; i <= n; i++) {if (G[i][i] > 1) { ok = true; break; }}printf("Case %d: %s\n", ++flag, ok ? "Yes" : "No");}return 0;
}
POJ 2240 Arbitrage——spfa判正环||flody相关推荐
- POJ 2240 Arbitrage(SPFA判正环)
POJ 2240 Arbitrage 题目大意 套利是指利用货币汇率的差异,将一种货币的一个单位转换为同一货币的多个单位.例如,假设1美元买0.5英镑,1英镑买10.0法国法郎,1法国法郎买0.21美 ...
- POJ 2240 Arbitrage(判正环)
http://poj.org/problem?id=2240 题意: 货币兑换,判断最否是否能获利. 思路: 又是货币兑换题,Belloman-ford和floyd算法都可以的. 1 #include ...
- 算法提高课-图论-负环-AcWing 1165. 单词环:spfa判正环、二分、01分数规划
文章目录 题目分析 题目链接 题目分析 来源:acwing 分析: 如何建图? 这样建图.以样例举例.起点是前两个字母,终点是末尾两个字母,边权是字符串的长度. 我们求的是什么呢? 题目要求Σ边权Σ1 ...
- 算法提高课-图论-负环-AcWing 361. 观光奶牛:spfa判正环、负环、01分数规划、二分
文章目录 题目分析 题目链接 题目分析 来源:acwing 分析: 题目要求ΣfiΣgi\frac{\Sigma{f_i}}{\Sigma{g_i}}ΣgiΣfi的最大值,这种问题称为01分数规 ...
- POJ 3259 Wormholes SPFA判负环
思路:SPFA判负环 数组不要开太小-- (后面附一组测试数组) // by SiriusRen #include <queue> #include <cstdio> #inc ...
- poj1860(spfa判正环)
题目连接:http://poj.org/problem?id=1860 题意:有多种从a到b的汇率,在你汇钱的过程中还需要支付手续费,那么你所得的钱是 money=(nowmoney-手续费)*rat ...
- POJ1680 Currency Exchange SPFA判正环
转载来源:優YoU http://user.qzone.qq.com/289065406/blog/1299337940 提示:关键在于反向利用Bellman-Ford算法 题目大意 有多种汇币,汇 ...
- 牛客多校2 - Link with Game Glitch(spfa求正环,套汇,二分答案)
https://ac.nowcoder.com/acm/contest/33187/D 题意 给定 n 种物品,一共 m 种转换方案. 每种转换方案给出四个数 a , b , c , d a, b, ...
- poj 2049(二分+spfa判负环)
poj 2049(二分+spfa判负环) 给你一堆字符串,若字符串x的后两个字符和y的前两个字符相连,那么x可向y连边.问字符串环的平均最小值是多少.1 ≤ n ≤ 100000,有多组数据. 首先根 ...
- [APIO2017]商旅——分数优化+floyd+SPFA判负环+二分答案
题目链接: [APIO2017]商旅 枚举任意两个点$(s,t)$,求出在$s$买入一个物品并在$t$卖出的最大收益. 新建一条从$s$到$t$的边,边权为最大收益,长度为原图从$s$到$t$的最短路 ...
最新文章
- 超链接浏览meta name=format-detection/ 的用法
- 优秀的博客与文章总结链接地址
- mac 或linux上 pip 不支持ssl的问题
- 桁架工业机器人编程_工业机器人之桁架机器人
- STM32之RTT调试
- 干货 | YOLOv5在建筑工地中安全帽佩戴检测的应用
- 插件安装失败_贴片保险丝额定电流应用电路为什么会安装失败?
- Oracle的主键和外键
- vue2.0实现点击后显示,再次点击隐藏
- unity3d 求两个点长度_Unity3D实现体积光
- CCF NOI1087 第K名
- linux下apache+php配置
- centos7如何添加开机启动服务/脚本
- 【图像评价】基于matlab图像去雾质量评价【含Matlab源码 066期】
- Android 中自定义组件例子一(中级)
- 笔记-软考高项-错题笔记汇总3
- 计算机专业考研北京有哪些学校,计算机考研北京地区学校大全!
- docker中报错Error: Failed to download metadata for repo ‘appstream‘: Cannot prepare internal mirro
- Python前世今生
- 该设备正在使用中.请关闭可能使用该设备的所