题目链接: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. Hibernate3的DetachedCriteria支持

    Hibernate3支持DetachedCriteria,这是一个非常有意义的特性!我们知道,在常规的Web编程中,有大量的动态条件查询,即用户在网页上面自由选择某些条件,程序根据用户的选择条件,动态 ...

  2. java.net.SocketException:Software caused connection abort: recv failed 异常分析 +socket客户端&服务端代码

    java.net.SocketException:Software caused connection abort: recv failed 异常分析 分类: 很多的技术 2012-01-04 12: ...

  3. (转)MFC中获得各个类的指针/句柄 ID的总结

    http://www.cnblogs.com/ylhome/archive/2009/10/06/1578478.html 一般我们使用的框架是VC提供的Wizard生成的MFC App Wizard ...

  4. [Angularjs]asp.net mvc+angularjs+web api单页应用

    写在前面 最近的工作一直在弄一些h5的单页应用,然后嵌入到app的webview中.之前一直在用angularjs+html+ashx的一套东西.实在是玩腻了.然后就尝试通过asp.net mvc的方 ...

  5. Ubuntu 中软件的安装、卸载以及查看的方法总结

    Ubuntu 中软件的安装.卸载以及查看的方法总结 博客分类: Linux UbuntuDebian配置管理CacheF#  说明:由于图形化界面方法(如Add/Remove... 和Synaptic ...

  6. Linux unzip解压文件到某个目录下面

    1,例如我想解压Metinfo5.2.zip  到某一个文件夹下,执行下面的命令就可以了 sudo unzip  MetInfo5.2.zip  -d  metinfo-bak

  7. 导出Excel之Epplus使用教程2(样式设置)

    导出Excel之Epplus使用教程1(基本介绍) 导出Excel之Epplus使用教程2(样式设置) 导出Excel之Epplus使用教程3(图表设置) 导出Excel之Epplus使用教程4(其他 ...

  8. 老项目的#iPhone6与iPhone6Plus适配#Icon适配

        本文永久地址为http://www.cnblogs.com/ChenYilong/p/4020373.html ,转载请注明出处.  这是Evernote印象笔记的链接:https://www ...

  9. 浅谈setTimeout函数和setInterval函数

    前几天学了js,看到了两个非常有趣的函数,他们分别是setTimeout函数和setInterval函数,这两个函数能使网页呈现非常一些网页中比较常见的效果,比如说图片轮播,等一些非常好玩的效果.下面 ...

  10. characterCustomezition的资源打包代码分析

    using System.Collections.Generic; using System.IO; using UnityEditor; using UnityEngine; class Creat ...