题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=25957

思路:由于路线为一个环,将路径上的权值改为c-p*d,那么然后建图,那么我们只需判断图中是否存在权值和为正的环,这个用spfa即可。

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#include<vector>
using namespace std;
#define MAXN 222
#define inf 1<<30 struct Edge{
int v,w;
Edge(){}
Edge(int vv,int ww):v(vv),w(ww){}
}; int n,m,flag;
int dist[MAXN],_count[MAXN];
bool mark[MAXN];
vector<vector<Edge> >g; void spfa(int st)
{
memset(mark,false,sizeof(mark));
memset(_count,,sizeof(_count));
fill(dist,dist+n+,-inf);
queue<int>que;
que.push(st);
dist[st]=;
while(!que.empty()){
int u=que.front();
que.pop();
mark[u]=false;
_count[u]++;
if(_count[u]>n){
flag=;
return ;
}
for(int i=;i<g[u].size();i++){
int v=g[u][i].v,w=g[u][i].w;
if(dist[u]+w>dist[v]){
dist[v]=dist[u]+w;
if(!mark[v]){
mark[v]=true;
que.push(v);
}
}
}
}
}
int main()
{
int _case,a,b,c,d,p,t=;
scanf("%d",&_case);
while(_case--){
scanf("%d%d%d",&n,&m,&p);
g.clear();
g.resize(n+);
while(m--){
scanf("%d%d%d%d",&a,&b,&c,&d);
g[a].push_back(Edge(b,c-d*p));
}
flag=;
for(int i=;i<n;i++)if(!flag)spfa(i);
printf("Case %d: ",t++);
flag?puts("YES"):puts("NO");
}
}

loj 1221(spfa判正环)的更多相关文章

  1. POJ 1860——Currency Exchange——————【最短路、SPFA判正环】

    Currency Exchange Time Limit:1000MS     Memory Limit:30000KB     64bit IO Format:%I64d & %I64u S ...

  2. POJ 3621 Sightseeing Cows 【01分数规划+spfa判正环】

    题目链接:http://poj.org/problem?id=3621 Sightseeing Cows Time Limit: 1000MS   Memory Limit: 65536K Total ...

  3. poj1860(spfa判正环)

    题目连接:http://poj.org/problem?id=1860 题意:有多种从a到b的汇率,在你汇钱的过程中还需要支付手续费,那么你所得的钱是 money=(nowmoney-手续费)*rat ...

  4. POJ 2240 Arbitrage spfa 判正环

    d[i]代表从起点出发可以获得最多的钱数,松弛是d[v]=r*d[u],求最长路,看有没有正环 然后这题输入有毒,千万别用cin 因为是大输入,组数比较多,然后找字符串用strcmp就好,千万不要用m ...

  5. POJ1680 Currency Exchange SPFA判正环

    转载来源:優YoU  http://user.qzone.qq.com/289065406/blog/1299337940 提示:关键在于反向利用Bellman-Ford算法 题目大意 有多种汇币,汇 ...

  6. loj 1108(spfa判负环)

    题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=26823 思路:题目的意思是求出所有的能够到达负环的点.负环很好求, ...

  7. [模板]SPFA判负环

    目录 一.BFS法判负环 二.DFS法判负环 三.SPFA判正环 一.BFS法判负环 Code: #include<bits/stdc++.h> #define re register # ...

  8. poj3621 SPFA判断正环+二分答案

    Farmer John has decided to reward his cows for their hard work by taking them on a tour of the big c ...

  9. LightOj 1221 - Travel Company(spfa判负环)

    1221 - Travel Company PDF (English) Statistics problem=1221" style="color:rgb(79,107,114)& ...

随机推荐

  1. Hbase can't get locations问题

    2016-11-08 22:21:16,875 ERROR [main-SendThread(ht-lxfx-01:24002)] client.ZooKeeperSaslClient: An err ...

  2. eclipse-统计代码行数

    使用Eclipse可以方便的统计工程或文件的代码行数,方法如下: 1.点击要统计的项目或许文件夹,在菜单栏点击Search,然后点击File...  2.选中正则表达式(Regular express ...

  3. HDU4887_Endless Punishment_BSGS+矩阵快速幂+哈希表

    2014多校第一题,当时几百个人交没人过,我也暴力交了几发,果然不行. 比完了去学习了BSGS才懂! 题目:http://acm.hdu.edu.cn/showproblem.php?pid=4887 ...

  4. C# 我理解的接口、抽象类、以及事件

    一.摘要 面试中无数次被问及到什么是接口,什么是抽象类,接口和抽象类有什么区别?什么是委托,什么是事件. 请写出猫叫了,老鼠跑了的例子..... 这些东西对于一些初学者来说可能还真的有点搞不懂,对于一 ...

  5. Commando War

    Commando War“Waiting for orders we held in the wood, word from the front never cameBy evening the so ...

  6. ZeroMQ之Request/Response (Java)

    自己最开始是在cloud foundry中接触过消息服务器(nats),或者说是消息中间件,也算是初步知道了一个消息服务器对于分布式的网络系统的重要性,后来自己也曾想过在一些项目中使用它,尤其是在一些 ...

  7. 再论pyquery

    发现对于QQ群 空间文件的抓取毫无办法. QQ空间的代码可圈可点: 做了一个js的“客户端”,第一次加载时,将文件的列表信息全部抓取出来,然后基于js进行翻页和排序. 因此,想要抓取js渲染的dom, ...

  8. jquery博客收集的IE6中CSS常见BUG全集及解决方案

    今天的样式调的纠结,一会这边一会那么把jquery博客折腾的头大,浏览器兼容性.晚上闲着收集一些常见IE6中的BUG 3像素问题及解决办法 当使用float浮动容器后,在IE6下会产生3px的空隙,有 ...

  9. 【OpenStack】OpenStack系列12之OpenStack自动化测试详解

    参考文档: https://github.com/yongluo2013/osf-openstack-training/blob/master/installation/How-to-setup-op ...

  10. 【SpringMVC】SpringMVC系列1之HelloWorld

    SpringMVC之HelloWorld 概述 SpringMVC 是基于 MVC 设计理念的优秀Web 框架,是目前最主流的 MVC 框架之一.Spring3.0 后全面超越 Struts2,成为最 ...