LightOj 1221 - Travel Company(spfa判负环)
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). N, R 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 Bi. Ii 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 |
p*out[k]-in[k]<0 即在图中存在负环。证明完成。
#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判负环)的更多相关文章
- Poj 3259 Wormholes(spfa判负环)
Wormholes Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 42366 Accepted: 15560 传送门 Descr ...
- POJ 3259 Wormholes(SPFA判负环)
题目链接:http://poj.org/problem?id=3259 题目大意是给你n个点,m条双向边,w条负权单向边.问你是否有负环(虫洞). 这个就是spfa判负环的模版题,中间的cnt数组就是 ...
- LightOJ 1074 Extended Traffic SPFA 消负环
分析:一看就是求最短路,然后用dij,果断错了一发,发现是3次方,有可能会出现负环 然后用spfa判负环,然后标记负环所有可达的点,被标记的点答案都是“?” #include<cstdio> ...
- spfa判负环
bfs版spfa void spfa(){ queue<int> q; ;i<=n;i++) dis[i]=inf; q.push();dis[]=;vis[]=; while(!q ...
- poj 1364 King(线性差分约束+超级源点+spfa判负环)
King Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 14791 Accepted: 5226 Description ...
- 2018.09.24 bzoj1486: [HNOI2009]最小圈(01分数规划+spfa判负环)
传送门 答案只保留了6位小数WA了两次233. 这就是一个简单的01分数规划. 直接二分答案,根据图中有没有负环存在进行调整. 注意二分边界. 另外dfs版spfa判负环真心快很多. 代码: #inc ...
- BZOJ 4898 [APIO2017] 商旅 | SPFA判负环 分数规划
BZOJ 4898 [APIO2017] 商旅 | SPFA判负环 分数规划 更清真的题面链接:https://files.cnblogs.com/files/winmt/merchant%28zh_ ...
- [P1768]天路(分数规划+SPFA判负环)
题目描述 “那是一条神奇的天路诶~,把第一个神犇送上天堂~”,XDM先生唱着这首“亲切”的歌曲,一道猥琐题目的灵感在脑中出现了. 和C_SUNSHINE大神商量后,这道猥琐的题目终于出现在本次试题上了 ...
- poj 2049(二分+spfa判负环)
poj 2049(二分+spfa判负环) 给你一堆字符串,若字符串x的后两个字符和y的前两个字符相连,那么x可向y连边.问字符串环的平均最小值是多少.1 ≤ n ≤ 100000,有多组数据. 首先根 ...
随机推荐
- 洛谷P1801 黑匣子
题目传送门 分析:这题和另外一个题目中位数非常相似,有兴趣可以先看看,比这一题简单.首先暴力模拟还是别想了,估计30%的数据都有点悬.正解应该是用二叉堆.但是如果用一个堆当然不方便,所以建两个堆,一个 ...
- 【BZOJ 1150】 1150: [CTSC2007]数据备份Backup (贪心+优先队列+双向链表)
1150: [CTSC2007]数据备份Backup Description 你在一家 IT 公司为大型写字楼或办公楼(offices)的计算机数据做备份.然而数据备份的工作是枯燥乏味 的,因此你想设 ...
- 【线段树】hihocoder 1586 ACM-ICPC国际大学生程序设计竞赛北京赛区(2017)网络赛 I. Minimum
题意:给你一个序列(长度不超过2^17),支持两种操作:单点修改:询问区间中最小的ai*aj是多少(i可以等于j). 只需要线段树维护区间最小值和最大值,如果最小值大于等于0,那答案就是minv*mi ...
- 【Tarjan算法】【DFS】Petrozavodsk Summer Training Camp 2016 Day 9: AtCoder Japanese Problems Selection, Thursday, September 1, 2016 Problem B. Point Pairs
这份代码可以作为找割边的模板.割边分割出来的部分是无向图的 边-双连通分量. 平面上2*n+1个点,在同一横坐标上的点之间可以任意两两匹配.同一纵坐标上的点之间也可以.问你对于所有的点i,输出i被移除 ...
- 【数论】【中国剩余定理】【LCM】hdu1788 Chinese remainder theorem again
根据题目容易得到N%Mi=Mi-a. 那么可得N%Mi+a=Mi. 两侧同时对Mi取余,可得(N+a)%Mi=0. 将N+a看成一个变量,就可以把原问题转化成求Mi的LCM,最后减去a即可. #inc ...
- 【优先队列+贪心】POJ2431-Expedition
解题方法详见<挑战程序设计竞赛(第二版)>P74-75.注意首先要对加油站以位置为关键字快排,不要遗忘把终点视作一个加油量为0的加油站,否则最终只能到达终点前的加油站. /*优先队列*/ ...
- [NOIp2017提高组]宝藏
#include<cstdio> #include<cctype> #include<algorithm> inline int getint() { regist ...
- java阶乘问题
问题描述: 编写代码求:1!+2!+3!+…+20!的值 代码 public class Demo { public static void main(String[] args) { long nu ...
- 基于tiny4412的Linux内核移植 -- PWM子系统学习(八)
作者信息 作者: 彭东林 邮箱:pengdonglin137@163.com QQ:405728433 平台简介 开发板:tiny4412ADK + S700 + 4GB Flash 要移植的内核版本 ...
- futer.get()(如果任务没执行完将等待)
/** * 获取异步任务的执行结果(如果任务没执行完将等待) */ V get() throws InterruptedException, ExecutionException; Future必要时 ...