1221 - Travel Company

Time Limit: 2 second(s) Memory Limit: 32 MB

A travel company is planning to launch their bus service in a new route. So they conducted a survey and made a list of all possible roads connecting different cities. Each of the roads has a certain amount of income based on current fare. But at the same
time, each road has some expenses too (this includes fuel and maintenance cost, staff payments, taxes and tribute to labor union which is recently approved by the Government). The travel company is looking for a cyclic route. That is, the bus will start from
any city, then visit one or more cities each exactly once and return to the starting city. The company is also concerned with the profit on the route. In fact the directors of the company have a strict requirement of a profit ratio strictly greater than P.
Otherwise they will not launch the service. A profit ratio for a route is the ratio between the total incomes to the total expenses for that route.

One of your friends works in that company and he asks for a little help from you. All you have to do is to determine if there exists such route, so that the company has a profit ratio of P.

Input

Input starts with an integer T (≤ 100), denoting the number of test cases.

Each case starts with a blank line and three integers N, R, P (2 ≤ N ≤ 100, 0 ≤ R ≤ 9900, 1 ≤ P ≤ 100)NR and Prepresents number of cities, number of road links and the expected profit
ratio respectively. Then R lines follow. Each line contains four integers Ai, Bi, Ii, Ei (0 ≤ Ai, Bi < N, 0 ≤ Ii ≤ 5000, 1 ≤ Ei ≤ 5000)(Ai,
Bi)
 represents directed road link from city Ai to BiIi and Ei are the incomes and expenses of the road link respectively.
You may assume that (Ai, Bi) ≠ (Aj, Bj), if i ≠ j and Ai ≠ Bi for any i.

Output

For each case, print the case number and "YES" if there is a cyclic route for which the profit ratio is greater than P or "NO", if there is no such route.

Sample Input

Output for Sample Input

3

5 8 3

0 1 17 8

1 0 10 5

1 2 11 5

1 4 5 3

2 3 13 7

3 1 9 4

4 3 11 1

3 0 11 6

5 8 3

0 1 17 8

1 0 10 5

1 2 11 5

1 4 5 3

2 3 13 7

3 1 9 4

4 3 11 2

3 0 11 6

5 8 2

0 1 17 8

1 0 10 5

1 2 11 5

1 4 5 3

2 3 13 7

3 1 9 4

4 3 11 5

3 0 11 6

Case 1: YES

Case 2: NO

Case 3: YES


首先申明这道题是看的scf的题意,一開始没读懂题,结果直接抄袭人家题意。敲了个spfa,忘了初始化vector调了一下午没弄好。。晚上认真读了一遍英语。细致一想这题真的是非常巧妙。题意:旅游公司推出了一种新的旅游路线,这里有n个城市。编号为0--n-1,有m条路线,每行输入u v in out 4个变量。u v 代表编号为u和v的两个城市相连,in代表旅游公司在这条路线上的收入,out代表支出,最開始一行给出一个利润率p(收入/支出),公司要求找到一条循环路线(即一个环)。使得总利率大于p。
我们能够依据题意写出这么一条原始的公式:(in[0]+in[1]+....in[k])/(out[0]+out[1]+...out[k])>p 即 p*(out[0]+out[1]+...out[k])<(in[0]+in[1]+....in[k]),总结一下就是对于这个环中的随意一条边,都要求
p*out[k]-in[k]<0 即在图中存在负环。证明完成。

用spfa判负环要注意要以每一个点为起点判一遍(假设存在负环能够直接退出)。判负环的原理是仅仅要有一个点入队次数大于n(点的总个数)。则存在负环。由于最短路最多对边松弛n-1次就能够求出来,假设一个点入队次数大于n,说明一定存在负环。即不存在最短路。
#include <cstdio>
#include <iostream>
#include <cstring>
#include <cctype>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <list>
using namespace std;
const int maxn=50100;
const int INF=0x3f3f3f3f;
int n,m,dis[maxn],vis[maxn],intime[maxn];
vector < pair<int,int> > eg[maxn];
int spfa(int src)
{
queue <int> Q;
memset(dis,INF,sizeof(dis));
memset(vis,0,sizeof(vis));
memset(intime,0,sizeof(intime));
dis[src]=0;
Q.push(src);
while(!Q.empty())
{
int u=Q.front();Q.pop();
vis[u]=0;intime[u]++;
if(intime[u]>n)
return 1;
int len=eg[u].size();
for(int i=0;i<len;i++)
{
int v=eg[u][i].first;
int w=eg[u][i].second;
if(dis[v]>dis[u]+w)
{
dis[v]=dis[u]+w;
if(!vis[v])
{
vis[v]=1;
Q.push(v);
}
}
}
}
return 0;
}
int main()
{
int T,p,cas=1;
scanf("%d",&T);
while(T--){
scanf("%d%d%d",&n,&m,&p);
for(int i=0;i<=n;i++)
eg[i].clear();
while(m--)
{
int u,v,in,out;
scanf("%d%d%d%d",&u,&v,&in,&out);
int tem=out*p-in;
eg[u].push_back(make_pair(v,tem));
}
printf("Case %d: ",cas++);
int flag=1;
for(int i=0;i<n;i++)
{
if(spfa(i))
{
flag=0;
printf("YES\n");
break;
}
}
if(flag)
printf("NO\n");
}
return 0;
}

LightOj 1221 - Travel Company(spfa判负环)的更多相关文章

  1. Poj 3259 Wormholes(spfa判负环)

    Wormholes Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 42366 Accepted: 15560 传送门 Descr ...

  2. POJ 3259 Wormholes(SPFA判负环)

    题目链接:http://poj.org/problem?id=3259 题目大意是给你n个点,m条双向边,w条负权单向边.问你是否有负环(虫洞). 这个就是spfa判负环的模版题,中间的cnt数组就是 ...

  3. LightOJ 1074 Extended Traffic SPFA 消负环

    分析:一看就是求最短路,然后用dij,果断错了一发,发现是3次方,有可能会出现负环 然后用spfa判负环,然后标记负环所有可达的点,被标记的点答案都是“?” #include<cstdio> ...

  4. spfa判负环

    bfs版spfa void spfa(){ queue<int> q; ;i<=n;i++) dis[i]=inf; q.push();dis[]=;vis[]=; while(!q ...

  5. poj 1364 King(线性差分约束+超级源点+spfa判负环)

    King Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 14791   Accepted: 5226 Description ...

  6. 2018.09.24 bzoj1486: [HNOI2009]最小圈(01分数规划+spfa判负环)

    传送门 答案只保留了6位小数WA了两次233. 这就是一个简单的01分数规划. 直接二分答案,根据图中有没有负环存在进行调整. 注意二分边界. 另外dfs版spfa判负环真心快很多. 代码: #inc ...

  7. BZOJ 4898 [APIO2017] 商旅 | SPFA判负环 分数规划

    BZOJ 4898 [APIO2017] 商旅 | SPFA判负环 分数规划 更清真的题面链接:https://files.cnblogs.com/files/winmt/merchant%28zh_ ...

  8. [P1768]天路(分数规划+SPFA判负环)

    题目描述 “那是一条神奇的天路诶~,把第一个神犇送上天堂~”,XDM先生唱着这首“亲切”的歌曲,一道猥琐题目的灵感在脑中出现了. 和C_SUNSHINE大神商量后,这道猥琐的题目终于出现在本次试题上了 ...

  9. poj 2049(二分+spfa判负环)

    poj 2049(二分+spfa判负环) 给你一堆字符串,若字符串x的后两个字符和y的前两个字符相连,那么x可向y连边.问字符串环的平均最小值是多少.1 ≤ n ≤ 100000,有多组数据. 首先根 ...

随机推荐

  1. 如何求先序排列和后序排列——hihocoder1049+洛谷1030+HDU1710+POJ2255+UVA548【二叉树递归搜索】

    [已知先序.中序求后序排列]--字符串类型 #1049 : 后序遍历 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 小Ho在这一周遇到的问题便是:给出一棵二叉树的前序和 ...

  2. 性能测试篇:LoadRunner11 压力测试实例笔记

    最近在学习用loadrunner做web性能测试,简单记录一下一个自学实例流程. 1.录制测试脚本 (1).打开LR11,点击create/edit Script来打开VUgen (2).点击新建 ( ...

  3. Hibernate 与Spring整合出现 hibernate.HibernateException: createCriteria is not valid without active transaction

    当 Hibernate 和 Spring 整合时,在 Spring 中指定的 Hibernate.cfg.xml 文件内容中要注释掉以下内容: <!-- Enable Hibernate's a ...

  4. CocurrentHashMap和HashTable区别分析

    集合是编程中最常用的数据结构.而谈到并发,几乎总是离不开集合这类高级数据结构的支持.比如两个线程需要同时访问一个中间临界区(Queue),比如常会用缓存作为外部文件的副本(HashMap).这篇文章主 ...

  5. Struts2与Servlet之间的关系

    在struts2.0中,可以通过ServletActionContext.getRequest()获取request对象. 在action的方法中return一个字符串,该字符串对应struts.xm ...

  6. 初雪-Diary?

    who care ------------2018 11 6-------------- 终于AK一场啦 ------------2018 10 18-------------- 嗯....今天T2多 ...

  7. 【线段树】洛谷 P3372 【模板】线段树 1

    动态开结点线段树板子. #include<cstdio> using namespace std; typedef long long ll; ll sumv[400005],delta[ ...

  8. 【贪心】【堆】bzoj2590 [Usaco2012 Feb]Cow Coupons

    每个物品有属性a,b 考虑在仅仅用光优惠券时的最优方案. 显然是按照b排序,取前K个. 但是我们还要尽可能去取剩余的. 假设朴素地取剩余的话,应该把剩余的对a排序,然后尽量去取. 但是有可能对其用优惠 ...

  9. bzoj 2665: [cqoi2012]编号

    题目中说任意两个数至少要有3个位上数不相同,那么其实也就是从7个数中选出5个这样任意的组合全部不同,用数组f[i][j][k][l][m][n]记一下就好了,i为第几种组合,一共C(7,5)种,最后爆 ...

  10. [转]java中JSONObject与JSONArray的使用详细说明及有关JSON的工具类

    JSONObject与JSONArray的使用 一.JAR包简介 要使程序可以运行必须引入JSON-lib包,JSON-lib包同时依赖于以下的JAR包: 1.commons-lang.jar 2.c ...