最短路(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 ...
随机推荐
- Unity3D中定时器的使用
源地址:http://unity3d.9tech.cn/news/2014/0402/40149.html 在游戏设计过程中定时器是必不可少的工具,我们知道update方法是MonoBehavior中 ...
- linux 使用 ionice 限制 Xen 虚拟机磁盘 IO
作为 VPS 服务商我们需要保证每个 VPS 公平的使用 host(服务器)的资源,避免某个 VPS 因为程序死循环.挂起.滥用等因素 “拖累” 其他 VPS,如果出现这个情况如何临时限制这个 VPS ...
- Linux 实现rsyslog日志里面的IP地址记录 未测试
之前我是在bashrc中添加了一句,让系统操作日志时向rsyslog发送一份内容,现在只要在发送的时候,自己再获取下当前的远程登录IP加进去就可以,像这样 /etc/bashrc sshClientI ...
- Android程序启动加载动画实现
package com.example.bmob_test.ui;//程序启动动画,图片颜色由浅到深,方法一 import com.example.bmob_test.LogActivity; imp ...
- HDOJ 2088
#include<cstdio> int main() { ],i,j,n,x,k=; int sum,ans; while(scanf("%d",&n)!=E ...
- 基础知识《二》java的基本类型
一.java基本数据类型 Java基本类型共有八种,基本类型可以分为三类,字符类型char,布尔类型boolean以及数值类型byte.short.int.long.float.double.数值类型 ...
- 【leetcode】Subsets
Subsets Given a set of distinct integers, S, return all possible subsets. Note: Elements in a subset ...
- 当年的文曲星cc800
你还记得当年的cc800吗?还记得黄金英雄传说吗?还记得用cc800编程的日子吗... 今天突然想起了我的cc800,好怀念那段爬在家里的阳台的木架子上,挠着头,编程序的日子...可惜,当时比较穷,没 ...
- Java for LeetCode 174 Dungeon Game
The demons had captured the princess (P) and imprisoned her in the bottom-right corner of a dungeon. ...
- poj 2421 Constructing Roads 解题报告
题目链接:http://poj.org/problem?id=2421 实际上又是考最小生成树的内容,也是用到kruskal算法.但稍稍有点不同的是,给出一些已连接的边,要在这些边存在的情况下,拓展出 ...