poj1860(spfa)
来源:互联网 发布:商务数据分析与应用 编辑:程序博客网 时间:2024/06/11 08:26
题目
题意:题意就是给你一些钱和一些货币兑换点,问你能否经过一系列兑换使自己钱的价值增加(好神奇的操作)
题解:这题好久之前好像做过,但是当时太菜了应该看了题解草草过了,这样来看就是要看所给的图中是否有一个正环。
用spfa解决就好了,还可以用floyd或者贝尔曼福德。
刺激。
#include<cstdio>#include<algorithm>#include<cstring>#include<queue>using namespace std;#define inf 99999999const int maxn = 105;double dis[maxn],rate[maxn][maxn],cost[maxn][maxn];bool vis[maxn];int n,m,f,cnt[maxn];double money;void init(){ for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if(i == j) rate[i][j] = 0,cost[i][j] = 0; else rate[i][j] = inf,cost[i][j] = inf; } } memset(cnt,0,sizeof cnt);}int spfa(){ for (int i = 0; i < n; i++) dis[i] = 0,vis[i] = 0; queue<int> q; q.push(f); dis[f] = money,vis[f] = 1; while (!q.empty()) { int now = q.front(); q.pop(); vis[now] = 0; for (int i = 1; i<= n; i++) { if(rate[now][i] != inf&&dis[i] < (dis[now] - cost[now][i]) * rate[now][i]) { dis[i] = (dis[now] - cost[now][i]) * rate[now][i]; if(!vis[i]) { vis[i] = 1; q.push(i); } cnt[i]++; if(dis[f] > money||cnt[i] >= 10000) { return 1; } } } } return 0;}int main(){ while (~scanf("%d %d %d %lf",&n,&m,&f,&money)) { init(); int a,b; double r1,r2,c1,c2; for (int i = 0; i < m; i++) { scanf("%d %d %lf %lf %lf %lf",&a,&b,&r1,&c1,&r2,&c2); rate[a][b] = r1; cost[a][b] = c1; rate[b][a] = r2; cost[b][a] = c2; } if(spfa()) { printf("YES\n"); } else printf("NO\n"); }}
阅读全文
0 0
- poj1860(spfa)
- poj1860(spfa)
- spfa && poj1860
- poj1860 spfa
- POJ1860 SPFA
- poj1860 Currency Exchange(spfa)
- 【POJ1860】Currency Exchange【spfa判正环】
- poj1860 spfa判断负环
- POJ1860 SPFA判断正环
- poj1860 Currency Exchange(bellman+spfa解法)
- poj1860 货币转换 spfa+Bellman-Ford
- 1poj1860(bellman_ford)
- (POJ1860)兑换货币
- poj1860
- poj1860
- poj1860
- poj1860
- poj1860
- oracle 解析XML
- GetLastError函数使用及返回代码对照
- 用秦九昭公式计算多项式
- AT命令
- 由浅入深理解傅里叶—— 看完你还不懂的话,你可以回来掐死我(原作者说的,要掐掐死他)
- poj1860(spfa)
- llvm源码父类的设计方式以及其他设计方式
- loadrunner Web_类函数之web_set_timeout()
- AJAX学习笔记(六)_重构
- Tez动态图重构详解
- 写在微信公众号开通原创之后
- Boosting and AdaBoost
- iOSalertviewcontroller的action回调对横竖屏切换有影响
- MATLAB基本操作