Currency Exchange---poj1860 ( spfa, 回路,最长路)
题目链接:http://poj.org/problem?id=1860
题解:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <stdlib.h>
#include <math.h>
#include <queue>
#include <algorithm>
using namespace std;
#define N 210
#define INF 0xfffffff
double dist[N], V;
int cnt, Head[N], num[N], vis[N];
int n, m, s; struct Edge
{
int v, next;
double r, c;
}e[N]; void Add(int u, int v, double r, double c)
{
e[cnt].v = v;
e[cnt].r = r;
e[cnt].c = c;
e[cnt].next = Head[u];
Head[u] = cnt++;
} bool spfa()///spfa模板;
{
memset(vis, , sizeof(vis));
memset(num, , sizeof(num));
queue<int>Q;
vis[s] = ;
dist[s] = V;
Q.push(s);
num[s]++;
while(Q.size())
{
int p=Q.front();
Q.pop();
vis[p] = ;
for(int i=Head[p]; i!=-; i=e[i].next)
{
int q = e[i].v;
if(dist[q] < (dist[p] - e[i].c) * e[i].r)///注意松弛的变化;
{
dist[q] = (dist[p] - e[i].c) * e[i].r;
if(!vis[q])
{
vis[q] = ;
Q.push(q);
num[q] ++;
if(num[q]>n)
return true;///存在正权回路;
}
}
} }
if(dist[s]>V)///最长路后,实现了增值;
return true;
return false;
} int main()
{
int a, b;
double rab, rba, cab, cba;
while(scanf("%d%d%d%lf", &n, &m, &s, &V)!=EOF)
{
cnt = ;
memset(Head, -, sizeof(Head));
memset(dist, , sizeof(dist));
for(int i=; i<=m; i++)
{
scanf("%d%d%lf%lf%lf%lf", &a, &b, &rab, &cab, &rba, &cba);
Add(a, b, rab, cab);
Add(b, a, rba, cba);
}
if( spfa() )
printf("YES\n");
else
printf("NO\n");
}
return ;
}
Currency Exchange---poj1860 ( spfa, 回路,最长路)的更多相关文章
- spfa求最长路
http://poj.org/problem?id=1932 spfa求最长路,判断dist[n] > 0,需要注意的是有正环存在,如果有环存在,那么就要判断这个环上的某一点是否能够到达n点,如 ...
- XYZZY(spfa求最长路)
http://acm.hdu.edu.cn/showproblem.php?pid=1317 XYZZY Time Limit: 2000/1000 MS (Java/Others) Memor ...
- POJ 3126 --Father Christmas flymouse【scc缩点构图 && SPFA求最长路】
Father Christmas flymouse Time Limit: 1000MS Memory Limit: 131072K Total Submissions: 3007 Accep ...
- BZOJ 2019 [Usaco2009 Nov]找工作:spfa【最长路】【判正环】
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=2019 题意: 奶牛们没钱了,正在找工作.农夫约翰知道后,希望奶牛们四处转转,碰碰运气. 而 ...
- POJ 3592--Instantaneous Transference【SCC缩点新建图 && SPFA求最长路 && 经典】
Instantaneous Transference Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 6177 Accep ...
- HDU - 6201 transaction transaction transaction(spfa求最长路)
题意:有n个点,n-1条边的无向图,已知每个点书的售价,以及在边上行走的路费,问任选两个点作为起点和终点,能获得的最大利益是多少. 分析: 1.从某个结点出发,首先需要在该结点a花费price[a]买 ...
- 洛谷 P3627 [APIO2009]抢掠计划 Tarjan缩点+Spfa求最长路
题目地址:https://www.luogu.com.cn/problem/P3627 第一次寒假训练的结测题,思路本身不难,但对于我这个码力蒟蒻来说实现难度不小-考试时肛了将近两个半小时才刚肛出来. ...
- POJ 1860 Currency Exchange【SPFA判环】
Several currency exchange points are working in our city. Let us suppose that each point specializes ...
- (简单) POJ 1860 Currency Exchange,SPFA判圈。
Description Several currency exchange points are working in our city. Let us suppose that each point ...
- POJ 1860: Currency Exchange 【SPFA】
套汇问题,从源点做SPFA,如果有一个点入队次数大于v次(v表示点的个数)则图中存在负权回路,能够套汇,如果不存在负权回路,则判断下源点到自身的最长路是否大于自身,使用SPFA时松弛操作需要做调整 # ...
随机推荐
- Python调用打印机参考例子
参考资料: http://blog.csdn.net/jdh99/article/details/42585987 http://www.oschina.net/question/1438043_23 ...
- uniqid,md5,microtime
<?php header("content-type:text/html;charset=utf-8"); $str = uniqid(md5(microtime(true) ...
- css文字超出自动显示省略号
只针对单行文本有效: 01.针对块状元素 ul li{ width: 180px; text-overflow: ellipsis; white-space: nowrap;/*禁止自动换行*/ ov ...
- [java] java 线程join方法详解
join方法的作用是使所属线程对象正常执行run方法,而对当前线程无限期阻塞,直到所属线程销毁后再执行当前线程的逻辑. 一.先看普通的无join方法NoJoin.java public class N ...
- HDU 4497 GCD and LCM (分解质因数)
链接 : http://acm.hdu.edu.cn/showproblem.php?pid=4497 假设G不是L的约数 就不可能找到三个数. L的全部素因子一定包括G的全部素因子 而且次方数 ...
- Effective C++ Item 16 Use the same form in corresponding uses of new and delete
1. When you created an array and want to return the memory to system. You need to explicitly add [] ...
- apache+php生产环境错误记录
报错1: [18-Jul-2016 14:36:31 Asia/Shanghai] PHP Warning: DOMDocument::load(): I/O warning : failed to ...
- ionic安装及测试
官方教程: http://ionicframework.com/getting-started/ 官方教程写得比较简单,简单来说就是 1)安装nodejs(安装方法:http://www.cnblog ...
- eclipse中tomcat能正常启动,在浏览器中不能打开问题
问题原因:没有在eclipse中tomcat的server location设置到tomcat的安装目录. 解决办法:1.选择server点击右键,选择Open选项,然后在server locatio ...
- Unity随机Prefab,自动前往某点处理
对与U3D AI,看了下,自己做了小功能,以备后用啊! 一,在某区域随机产生某个对象 C# 文件名称为RadomAPoint.cs using UnityEngine; using System.C ...