首页 技术 正文
技术 2022年11月15日
0 收藏 560 点赞 3,807 浏览 1138 个字

题意:

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

解析:

判断是否存在正环即可  用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 = , 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,);
mem(vis,);
d[s] = num;
Q.push(s);
vis[s] = ;
while(!Q.empty())
{
int x = Q.front();Q.pop();
vis[x] = ;
for(int i=head[x]; i!=-; 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] = ;
if(++cnt[e.v] > n) return ; //判断是否进队n次
} } }
}
return ; //千万别忘了写这个 wa了n次}int main()
{
while(cin>>n>>m>>mon>>num)
{
mem(Node,);
mem(cnt,);
mem(head,-);
int ans = ;
for(int i=; 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;
else
cout<<"NO"<<endl; } return ;
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,955
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,479
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,291
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,108
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,740
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,774