最短路(Bellman_Ford) POJ 1860 Currency Exchange
/*
最短路(Bellman_Ford):求负环的思路,但是反过来用,即找正环
详细解释:http://blog.csdn.net/lyy289065406/article/details/6645778
*/
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <vector>
#include <cmath>
#include <queue>
#include <map>
#include <set>
using namespace std; const int MAXN = + ;
const int INF = 0x3f3f3f3f;
const double EPS = 1e-;
struct NODE
{
int u, v;
double r, c;
}node[MAXN*];
double d[MAXN]; bool Bellman_Ford(int s, int n, int tot, double V)
{
memset (d, , sizeof (d));
d[s] = V;
bool flag;
for (int i=; i<=n-; ++i)
{
flag = false;
for (int j=; j<=tot; ++j)
{
NODE e = node[j];
if (d[e.v] < (d[e.u] - e.c) * e.r)
{
d[e.v] = (d[e.u] - e.c) * e.r; flag = true;
}
}
if (!flag) break;
} for (int i=; i<=tot; ++i)
{
NODE e = node[i];
if (d[e.v] < (d[e.u] - e.c) * e.r) return true;
} return false;
} int main(void) //POJ 1860 Currency Exchange
{
//freopen ("A.in", "r", stdin); int n, m, s;
double V;
while (~scanf ("%d%d%d%lf", &n, &m, &s, &V))
{
int tot = ;
for (int i=; i<=m; ++i)
{
int x, y;
double rab, cab, rba, cba;
scanf ("%d%d%lf%lf%lf%lf", &x, &y, &rab, &cab, &rba, &cba);
//printf ("%d %d %lf %lf %lf %lf\n", x, y, rab, cab, rba, cba);
node[++tot].u = x; node[tot].v = y;
node[tot].r = rab; node[tot].c = cab;
node[++tot].u = y; node[tot].v = x;
node[tot].r = rba; node[tot].c = cba;
} if (Bellman_Ford (s, n, tot, V)) puts ("YES");
else puts ("NO");
} return ;
}
SPFA重写一遍
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <queue>
using namespace std; const int N = 100 + 10;
struct Edge {
int v, nex;
double r, c;
Edge() {}
Edge(int v, double r, double c, int nex) : v (v), r (r), c (c), nex (nex) {}
}edge[N*2];
int head[N];
double d[N];
bool vis[N];
int cnt[N];
int n, m, e;
double val; void init(void) {
memset (head, -1, sizeof (head));
e = 0;
} void add_edge(int u, int v, double r, double c) {
edge[e] = Edge (v, r, c, head[u]);
head[u] = e++;
} bool SPFA(int s) {
memset (vis, false, sizeof (vis));
memset (d, 0, sizeof (d));
memset (cnt, 0, sizeof (cnt));
d[s] = val; cnt[s] = 0; vis[s] = true;
queue<int> que; que.push (s);
while (!que.empty ()) {
int u = que.front (); que.pop ();
vis[u] = false;
for (int i=head[u]; ~i; i=edge[i].nex) {
int v = edge[i].v; double r = edge[i].r, c = edge[i].c;
if (d[v] < (d[u] - c) * r) {
d[v] = (d[u] - c) * r;
if (!vis[v]) {
vis[v] = true; que.push (v);
if (++cnt[v] > n) return true;
}
}
}
}
return false;
} int main(void) {
int s;
while (scanf ("%d%d%d%lf", &n, &m, &s, &val) == 4) {
init ();
for (int i=1; i<=m; ++i) {
int u, v;
double r1, c1, r2, c2;
scanf ("%d%d%lf%lf%lf%lf", &u, &v, &r1, &c1, &r2, &c2);
add_edge (u, v, r1, c1); add_edge (v, u, r2, c2);
}
if (SPFA (s)) puts ("YES");
else puts ("NO");
} return 0;
}
最短路(Bellman_Ford) POJ 1860 Currency Exchange的更多相关文章
- POJ 1860 Currency Exchange / ZOJ 1544 Currency Exchange (最短路径相关,spfa求环)
POJ 1860 Currency Exchange / ZOJ 1544 Currency Exchange (最短路径相关,spfa求环) Description Several currency ...
- POJ 1860 Currency Exchange 最短路+负环
原题链接:http://poj.org/problem?id=1860 Currency Exchange Time Limit: 1000MS Memory Limit: 30000K Tota ...
- POJ 1860 Currency Exchange + 2240 Arbitrage + 3259 Wormholes 解题报告
三道题都是考察最短路算法的判环.其中1860和2240判断正环,3259判断负环. 难度都不大,可以使用Bellman-ford算法,或者SPFA算法.也有用弗洛伊德算法的,笔者还不会SF-_-…… ...
- POJ 1860 Currency Exchange (最短路)
Currency Exchange Time Limit:1000MS Memory Limit:30000KB 64bit IO Format:%I64d & %I64u S ...
- POJ 1860 Currency Exchange【bellman_ford判断是否有正环——基础入门】
链接: http://poj.org/problem?id=1860 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=22010#probl ...
- POJ 1860——Currency Exchange——————【最短路、SPFA判正环】
Currency Exchange Time Limit:1000MS Memory Limit:30000KB 64bit IO Format:%I64d & %I64u S ...
- POJ 1860 Currency Exchange (最短路)
Currency Exchange Time Limit : 2000/1000ms (Java/Other) Memory Limit : 60000/30000K (Java/Other) T ...
- poj - 1860 Currency Exchange Bellman-Ford 判断正环
Currency Exchange POJ - 1860 题意: 有许多货币兑换点,每个兑换点仅支持两种货币的兑换,兑换有相应的汇率和手续费.你有s这个货币 V 个,问是否能通过合理地兑换货币,使得你 ...
- POJ 1860 Currency Exchange (Bellman-Ford)
题目链接:POJ 1860 Description Several currency exchange points are working in our city. Let us suppose t ...
随机推荐
- 以DDD为开发模式的设计开发步骤可以是
以DDD为开发模式的设计开发步骤可以是:1)分析需求:2)画出用例图,系统中各个角色如何使用系统,也包括外部系统如何使用系统,也包括系统中到某个时间点自动启动的某些功能(此时角色就是时间):3)针对各 ...
- FOJ 1205
Problem 1205 小鼠迷宫问题 Accept: 522 Submit: 1679 Time Limit: 1000 mSec Memory Limit : 32768 KB Pro ...
- Linux统计文件个数
查看某个文件夹下的文件个数用ls列目录,用grep过虑,再用wc统计即可 用ls -l列出后, 每一行对应一个文件或目录, 如果第一个字母为’-'则为普通文件, 若为’d'则为子目录 + +grep过 ...
- 【云计算】Docker云平台—Docker基础
Docker云平台系列共三讲,此为第一讲:Docker基础 参考资料: Docker官方文档:https://docs.docker.com/ Docker从入门到实践:https://yeasy.g ...
- 【Spring】Spring系列4之Spring支持JDBC
4.Spring支持JDBC 4.1.使用JdbcTemplate简化JDBC开发 也可以这么用(不推荐): 4.2.使用NamedParameterJdbcTemplate
- 25.在从1到n的正数中1出现的次数[NumberOf1Between1_N]
[题目] 输入一个整数n,求从1到n这n个整数的十进制表示中1出现的次数.例如输入12,从1到12这些整数中包含1 的数字有1,10,11和12,1一共出现了5次. [分析] 这是一道广为流传的goo ...
- iOS 如何使用自定义字体
首先,你需要有字体文件,比如 xxx.otf,之后你需要到工程的info配置文件中加入Fonts provided by application的值,如下图 之后,就可以使用字体名和UIFont的方法 ...
- Enum:Hopscotch(POJ 3050)
跳格子 题目大意:牛像我们一样跳格子,一个5*5的方格,方格有数字,给牛跳5次,可以组成一个6个数字组合字符串,请问能组合多少个字符串? 题目规模很小,暴力枚举,然后用map这个玩具来检测存不存在就可 ...
- IOS多线程(NSThread)
1.创建方法 使用NSThread创建线程主要有两个个方法,分别如下 NSThread* myThread = [[NSThread alloc] initWithTarget:self sele ...
- hdu 1879 继续畅通工程 解题报告
题目链接:http://code.hdu.edu.cn/showproblem.php?pid=1879 这条题目我的做法与解决Constructing Roads的解法是相同的. 0 表示没有连通: ...