http://poj.org/problem?id=1860

题意:  题目中主要是说存在货币兑换点,然后现在手里有一种货币,要各种换来换去,最后再换回去的时候看能不能使原本的钱数增多,每一种货币都有对应的汇率,而货币A到货币B的汇率即为1货币A换得得货币B的数量,但兑换点是要收取佣金的,且佣金从源货币中扣除,例如,你想在汇率29.75,佣金为0.39的兑换点把100美元换成卢布,得到的卢布数即为(100-0.39)*29.75 = 2963.3975.

样例解释:

3 2 1 20.0
1 2 1.00 1.00 1.00 1.00
2 3 1.10 1.00 1.10 1.00

多组输入,第一行中N代表有N种货币可以互相兑换,M代表有M个货币兑换点,S代表这个人手中的的货币的编号,V代表这个人手中拥有的货币数量,底下M行

每行六个数,A,B代表可以交换的货币A和B,剩下的实数RAB,CAB,RBA,CBA,代表A到B的汇率,佣金,B到A的汇率,佣金。以某种兑换方式增加原本的钱数,而且必须兑换为原来的货币。

解法:用spfa和Bellman都可以,我用的是spfa,改了一下原模板就过了

 #include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
using namespace std;
int u,v,w;
const int maxn = ;
const int maxm = ;
const int oo = <<;
struct node
{
int u;
int v;
double x,y ;
int next;
}edge[maxm];
double dis[maxn];
int m,n,num;
double ount;
int head[maxn],cnt,sum[maxn];
int vis[maxn] = {};
queue<int>qu;
void add(int u,int v,double x,double y)
{
edge[cnt].u = u ;
edge[cnt].v = v ;
edge[cnt].x = x ;
edge[cnt].y = y ;
edge[cnt].next = head[u];
head[u] = cnt++ ;
}
int spfa(int s)
{
for(int i = ; i < m ; i++)
{
dis[i] = ;
vis[i] = ;
}
dis[s] = ount;
qu.push(s);
vis[s] = ;
while(!qu.empty())
{
int u = qu.front();
qu.pop();
vis[u] = ;
for(int i = head[u] ; i != - ; i = edge[i].next)
{
int v = edge[i].v;
if((dis[u]-edge[i].y)*edge[i].x > dis[v])
{
dis[v] = (dis[u]-edge[i].y)*edge[i].x;
if(!vis[v])
{
vis[v] = ;
qu.push(v);
}
sum[v]++;
if(sum[v] > m)
return -;
}
}
}
return ;
}
void Init()
{
cnt = ;
memset(head,-,sizeof(head));
memset(sum,,sizeof(sum));
while(!qu.empty())
qu.pop();
}
int main()
{
while(scanf("%d %d %d %lf",&m,&n,&num,&ount)!=EOF)
{
Init();
int u,v;
double x,y,xx,yy ;
for(int i = ; i < n ; i++)
{
scanf("%d %d %lf %lf %lf %lf",&u,&v,&x,&y,&xx,&yy);
add(u,v,x,y);
add(v,u,xx,yy);
}
if(spfa(num) > )
cout<<"NO"<<endl;
else cout<<"YES"<<endl;
}
return ;
}

http://blog.csdn.net/lyy289065406/article/details/6645778

这个大神用的是Bellman

POJ1860Currency Exchange(SPFA)的更多相关文章

  1. 模板C++ 03图论算法 1最短路之单源最短路(SPFA)

    3.1最短路之单源最短路(SPFA) 松弛:常听人说松弛,一直不懂,后来明白其实就是更新某点到源点最短距离. 邻接表:表示与一个点联通的所有路. 如果从一个点沿着某条路径出发,又回到了自己,而且所经过 ...

  2. 最短路(SPFA)

    SPFA是Bellman-Ford算法的一种队列实现,减少了不必要的冗余计算. 主要思想是: 初始时将起点加入队列.每次从队列中取出一个元素,并对所有与它相邻的点进行修改,若某个相邻的点修改成功,则将 ...

  3. Bellman-Ford算法及其队列优化(SPFA)

    一.算法概述 Bellman-Ford算法解决的是一般情况下的单源最短路径问题.所谓单源最短路径问题:给定一个图G=(V,E),我们希望找到从给定源结点s属于V到每个结点v属于V的最短路径.单源最短路 ...

  4. Poj 3903 Stock Exchange(LIS)

    一.Description The world financial crisis is quite a subject. Some people are more relaxed while othe ...

  5. Currency Exchange(SPFA判负环)

    Several currency exchange points are working in our city. Let us suppose that each point specializes ...

  6. POJ 1860 Currency Exchange(SPFA+邻接矩阵)

    ( ̄▽ ̄)" #include<iostream> #include<cstdio> #include<cmath> #include<algori ...

  7. poj 1860 Currency Exchange (SPFA、正权回路 bellman-ford)

    链接:poj 1860 题意:给定n中货币.以及它们之间的税率.A货币转化为B货币的公式为 B=(V-Cab)*Rab,当中V为A的货币量, 求货币S通过若干此转换,再转换为原本的货币时是否会添加 分 ...

  8. sgu 240 Runaway (spfa)

    题意:N点M边的无向图,边上有线性不下降的温度,给固定入口S,有E个出口.逃出去,使最大承受温度最小.输出该温度,若该温度超过H,输出-1. 羞涩的题意 显然N*H的复杂度dp[n][h]表示到达n最 ...

  9. codevs 1021 玛丽卡(spfa)

    题目描述 Description 麦克找了个新女朋友,玛丽卡对他非常恼火并伺机报复. 因为她和他们不住在同一个城市,因此她开始准备她的长途旅行. 在这个国家中每两个城市之间最多只有一条路相通,并且我们 ...

随机推荐

  1. 【Newtonsoft.Json】Window Phone Json解析开发包

    WP从服务器.API交换数据一般都是用JSON格式字符串. 下面介绍用Newtonsoft.Json来处理JSON. 准备 1.到 http://json.codeplex.com/ 下载Newton ...

  2. activiti搭建(一)初始化数据库

    转载请注明源地址:http://www.cnblogs.com/lighten/p/5876681.html activiti-engine.jar包中自带了创建activiti工作流数据库表的SQL ...

  3. MotionEvent中getX()和getRawX()的区别

    http://blog.csdn.net/ztp800201/article/details/17218067 public class Res extends Activity implements ...

  4. php curl post

    public static function curlPost($url, $data=array()) {        $ch = curl_init();        if (is_array ...

  5. PHP弱类型安全问题笔记

    一.类型转换问题    intval(); var_dump(intval('1asdfasd'));  //1 var_dump(intval('awqw12'));  //0 var_dump(i ...

  6. struts2结构图

  7. UDP HelloWord

    Client.cpp #include <stdio.h> #include <winsock2.h> #pragma comment (lib,"ws2_32&qu ...

  8. jquery实现ajax,返回json数据

    jquery实现ajax可以调用几种方法 我经常用的是$get(url,data,callback,type)方法 其中url是异步请求的页面(可以是.ashx文件),data是参数,callback ...

  9. Android中表示尺寸的六种度量单位

    本文章来自:http://my.eoe.cn/lakeside/archive/4831.html in:Inches - 表示英寸,是屏幕的物理尺寸,每英寸等于2.54厘米.平时说手机屏幕4(英)寸 ...

  10. python 安装 setuptools Compression requires the (missing) zlib module 的解决方案

    背景: 虚拟机centos下安装python辅助工具 setuptools报错,错误信息大概如下: Traceback (most recent call last): File "setu ...