POJ1860(Currency Exchange)

编程入门 行业动态 更新时间:2024-10-27 12:40:00

POJ1860(<a href=https://www.elefans.com/category/jswz/34/1744771.html style=Currency Exchange)"/>

POJ1860(Currency Exchange)

题意:

    给出一张各种货币交换的网络,问在网络中交换原有的货币,问货币能否增值?

解析:

判断是否存在正环即可  用spfa  负环和正环的判定方法一样  如果一个点的进队次数超过n次 则存在环

 

代码如下:

#include <iostream>
#include <cstring>
#include <cstdio>
#include <queue>
#include <cmath>
#include <algorithm>
#define mem(a,b) memset(a,b,sizeof(a))
using namespace std;
const int maxn = 10010, INF = 0xfffffff;
struct node{int u,v,next;double rat,cost;
}Node[maxn];
int n,m,mon;
double num;
int cnt[maxn],vis[maxn],head[maxn];
double d[maxn];
void add(int u,int v,double rat,double cost,int i)
{Node[i].u = u;Node[i].v = v;Node[i].rat = rat;Node[i].cost = cost;Node[i].next = head[u];head[u] = i;}int spfa(int s)
{queue<int> Q;mem(d,0);mem(vis,0);d[s] = num;Q.push(s);vis[s] = 1;while(!Q.empty()){int x = Q.front();Q.pop();vis[x] = 0;for(int i=head[x]; i!=-1; i=Node[i].next){node e = Node[i];if(d[e.v] < (d[x]-e.cost)*e.rat){d[e.v] = (d[x]-e.cost)*e.rat;if(!vis[e.v]){Q.push(e.v);vis[e.v] = 1;if(++cnt[e.v] > n) return 1;   //判断是否进队n次}}}}return 0;     //千万别忘了写这个  wa了n次}int main()
{while(cin>>n>>m>>mon>>num){mem(Node,0);mem(cnt,0);mem(head,-1);int ans = 0;for(int i=0; i<m; ++i){int u,v;double urat,ucost,vrat,vcost;cin>>u>>v>>urat>>ucost>>vrat>>vcost;add(u,v,urat,ucost,ans++);add(v,u,vrat,vcost,ans++);}if(spfa(mon))cout<<"YES"<<endl;elsecout<<"NO"<<endl;}return 0;
}

 

转载于:.html

更多推荐

POJ1860(Currency Exchange)

本文发布于:2024-02-12 01:52:44,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1685075.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Currency   Exchange

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!