题目传送门

 /*
最短路(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的更多相关文章

  1. POJ 1860 Currency Exchange / ZOJ 1544 Currency Exchange (最短路径相关,spfa求环)

    POJ 1860 Currency Exchange / ZOJ 1544 Currency Exchange (最短路径相关,spfa求环) Description Several currency ...

  2. POJ 1860 Currency Exchange 最短路+负环

    原题链接:http://poj.org/problem?id=1860 Currency Exchange Time Limit: 1000MS   Memory Limit: 30000K Tota ...

  3. POJ 1860 Currency Exchange + 2240 Arbitrage + 3259 Wormholes 解题报告

    三道题都是考察最短路算法的判环.其中1860和2240判断正环,3259判断负环. 难度都不大,可以使用Bellman-ford算法,或者SPFA算法.也有用弗洛伊德算法的,笔者还不会SF-_-…… ...

  4. POJ 1860 Currency Exchange (最短路)

    Currency Exchange Time Limit:1000MS     Memory Limit:30000KB     64bit IO Format:%I64d & %I64u S ...

  5. POJ 1860 Currency Exchange【bellman_ford判断是否有正环——基础入门】

    链接: http://poj.org/problem?id=1860 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=22010#probl ...

  6. POJ 1860——Currency Exchange——————【最短路、SPFA判正环】

    Currency Exchange Time Limit:1000MS     Memory Limit:30000KB     64bit IO Format:%I64d & %I64u S ...

  7. POJ 1860 Currency Exchange (最短路)

    Currency Exchange Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 60000/30000K (Java/Other) T ...

  8. poj - 1860 Currency Exchange Bellman-Ford 判断正环

    Currency Exchange POJ - 1860 题意: 有许多货币兑换点,每个兑换点仅支持两种货币的兑换,兑换有相应的汇率和手续费.你有s这个货币 V 个,问是否能通过合理地兑换货币,使得你 ...

  9. POJ 1860 Currency Exchange (Bellman-Ford)

    题目链接:POJ 1860 Description Several currency exchange points are working in our city. Let us suppose t ...

随机推荐

  1. 响应式js幻灯片代码一枚

    网站搭建经常会用到js幻灯片轮播,放上几张上档次的美图,为你的爱站增添大气元素.经常看到一些js幻灯片代码,但是感觉不是很美观,有的也不支持自适应缩放,也即是响应式,现在智能手机的普及以及移动浏览器技 ...

  2. Win 7 下制作 mac 系统启动U盘

    Win 7 下制作 mac 系统启动U盘 前几天因为工作需要,在mac 上安装了win7.后来因为习惯问题将win7 分区了,后来就是进不去mac os,只能进入win7 .可恶. 苹果客服说只能用m ...

  3. shell kill掉含同一字符的关键字的进程

    如何kill掉进程名包含某个字符串的一批进程:kill -9 $(ps -ef|grep 进程名关键字|gawk '$0 !~/grep/ {print $2}' |tr -s '\n' ' ') 观 ...

  4. Android Toast 封装,避免Toast消息覆盖,替换系统Toast最好用的封装

    Android Toast 封装,避免Toast消息覆盖,无阻塞,等强大功能   ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 ...

  5. Linux内核之数据双链表

    导读 Linux 内核中自己实现了双向链表,可以在 include/linux/list.h 找到定义.我们将会首先从双向链表数据结构开始介绍内核里的数据结构.为什么?因为它在内核里使用的很广泛,你只 ...

  6. 在mac上安装nodejs

    文章转载自我的个人博客  www.iwangzheng.com node.js最初是2009年发布的,目标是为聊实现事件驱动和非阻塞I/O的web服务器,应用的场景非常的广泛,有web服务器.实时应用 ...

  7. python 之验证码

    验证码原理在于后台自动创建一张带有随机内容的图片,然后将内容通过img标签输出到页面. 安装图像处理模块: pip3 install pillow

  8. 【leetcode】Combination Sum

    Combination Sum Given a set of candidate numbers (C) and a target number (T), find all unique combin ...

  9. Scanner 和 String 类的常用方法

    Scanner类是在jdk1.5 之后有了这个: 常用格式是: Scanner sc = new Scanner(System.in); 从以下版本开始: 1.5 构造方法摘要 Scanner(Fil ...

  10. hp unix_ssh

    http://www.cyberciti.biz/faq/howto-hpux-sshd-service-startup-shutdown/ http://searchnetworking.techt ...