题目链接: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. [实战]MVC5+EF6+MySql企业网盘实战(27)——应用列表

    写在前面 本篇文章将实现应用列表,同样和其他列表的不同之处,在于查询条件的不同. 系列文章 [EF]vs15+ef6+mysql code first方式 [实战]MVC5+EF6+MySql企业网盘 ...

  2. GCC 编译详解

    GNU CC(简称为Gcc)是GNU项目中符合ANSI C标准的编译系统,能够编译用C.C++和Object C等语言编写的程序.Gcc不仅功能强大,而且可以编译如C.C++.Object C.Jav ...

  3. PDP 有多种定义,具体哪一种还需研究!!!!

    PDP (用户面进行隧道转发的信息的保存协议) 编辑 本词条缺少名片图,补充相关内容使词条更完整,还能快速升级,赶紧来编辑吧! 即PDP上下文,保存用户面进行隧道转发的所有信息,包括RNC/GGSN的 ...

  4. apue第六章学习总结

    apue第六章学习总结 1.关于阴影文件与口令 在口令文件当中,常见的字段有(以root为例): root(用户名):x(加密口令):0(uid):0(gid):root(注释字段):/root(用户 ...

  5. javascript模板插件amaze.js

    摘要: 最近在开发项目时,异步接口需要前端渲染数据,js拼接太低级,必然要用模板插件.之前用过基于jQuery的和juicer等插件,考虑到以后公司项目上的统一,移动端和pc端上的统一,以及可维护性, ...

  6. iOS多线程GCD(转)

    转自:http://www.cnblogs.com/pure/archive/2013/03/31/2977420.html Grand Central Dispatch (GCD)是Apple开发的 ...

  7. 安装 openSUSE Leap 42.1 之后要做的 8 件事

    导读 openSUSE Leap 确实是个巨大的飞跃,它允许用户运行一个和 SUSE Linux 企业版拥有同样基因的发行版.和其它系统一样,为了实现最佳的使用效果,在使用它之前需要做些优化设置. 下 ...

  8. linux之eval用法(高级bash程序员的必修之技)

    1. eval command-line 其中command-line是在终端上键入的一条普通命令行.然而当在它前面放上eval时,其结果是shell在执行命令行之前扫描它两次.如: pipe=&qu ...

  9. String to Integer

    Implement function atoi to convert a string to an integer. If no valid conversion could be performed ...

  10. iOS 中的Push Notifications简单实现(APNS)

    Android中的通知只有一种,就是Local Notifications,而iOS中除了Local Notifications外,还有一种Push Notifications.ios的这2种noti ...