题目链接: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. controller传值view

    400错误是请求错误 Model是map格式 @Controller public class HelloController { //view的值传给controller @RequestMappi ...

  2. 用开源Look&Feel (Substance)写 漂亮的Swing应用程序

    今天用Swing 做了一个模仿QQ2009的登录界面,用到了开源的Look&Feel (Substance),在使用的过程中遇到了一些问题,也学到了一些技巧.Substance (https: ...

  3. 微信新版支持读取iPhone M7/M8协处理器运动数据 与好友PK一下运动量吧

    iPhone的创新是有目共睹的,Healthkit的推出预示着苹果进军健康领域,iPhone M7/M8协处理器可以收集和分析用户的健康数据,那么好的硬件自然不会被势在打造完整生态圈的微信给错过,这不 ...

  4. 黑色30s高并发IIS设置

    在这篇博文中,我们抛开对阿里云的怀疑,完全从ASP.NET的角度进行分析,看能不能找到针对问题现象的更合理的解释. “黑色30秒”问题现象的主要特征是:排队的请求(Requests Queued)突增 ...

  5. 九度 OJ1008 hdu 3790

    #include<stdio.h> #include<string.h> struct node { int d; int p; }g[][]; #define inf 0x3 ...

  6. HTML前端——CSS样式

    使用CSS样式的方式: HTML<!DOCTYPE> 声明标签 内链样式表<body style="background: green; margin: 0; paddin ...

  7. Hibernate与 MyBatis的比较

    希望大家指出不对之处. 第一章     Hibernate与MyBatis Hibernate 是当前最流行的O/R mapping框架,它出身于sf.net,现在已经成为Jboss的一部分. Myb ...

  8. 多表利用DIH批量导入数据并建立索引注意事项

    如果希望同时对多个表进行全文检索,那我们该如何处理呢?利用DIH导入数据并建立索引时.schema.xml中配置了uniqueKey为id <uniqueKey>id</unique ...

  9. libavcodec/dxva2.h:40:5: error: unknown type name 'IDirectXVideoDecoder'

    gcc 4.9.2 编译 ffmpeg-git-1aeb88b 是出现如下错误 > FFmpeg fails to make with: > > CC libavcodec/dxva ...

  10. Java for LeetCode 151 Reverse Words in a String

    Given an input string, reverse the string word by word. For example, Given s = "the sky is blue ...