Lightoj 1221-travel company (spfa negative ring)

Source: Internet
Author: User
1221-travel company
PDF (English) Statistics Forum
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 des 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 greaterP. 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 ratioP.

Input

Input starts with an integerT (≤ 100), Denoting the number of test cases.

Each case starts with a blank line and three IntegersN, R, P (2 ≤ n ≤ 100, 0 ≤ r ≤ 9900, 1 ≤ p ≤ 100).N,RAndPRepresents number of cities, number of road links and the expected profit ratio respectively. ThenRLines follow. Each line contains four IntegersAI, Bi, II, EI (0 ≤ AI, Bi <n, 0 ≤ II ≤ 5000, 1 ≤ EI ≤ 5000).(AI, Bi)Represents directed road link from CityAIToBi.IIAndEIAre the incomes and expenses of the road link respectively. You may assume that(AI, Bi) = (AJ, BJ), IfI =jAndAi = BiFor anyI.

Output

For each case, print the case number and"Yes"If there is a cyclic route for which the profit ratio is greaterPOr"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



First, I declare that this question is the question of the SCF. I didn't understand the question at the beginning. I copied the question directly, typed a spfa, and forgot to initialize the vector for a whole afternoon .. After carefully reading English at night, I think this question is really clever. Question: A travel company has launched a new travel route, where there are n cities, 0 -- n-1, with M routes. Input four variables u v in out in each line. u v indicates that the two cities numbered U and V are connected, in represents the tourism company's income on this route, and out represents expenditure. In the first line, a profit margin P (income/expenditure) is provided. The company requires a circular route (that is, a loop ), so that the total interest rate is greater than P; we can write this original formula based on the question: (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]). To sum up, p * out [k]-in [k] <0 indicates that a negative ring exists in the graph, the proof is complete. To use spfa to determine a negative ring, make sure to judge it again from each point (if a negative ring exists, exit directly ), the principle of negative ring determination is that a negative ring exists as long as one vertex is more than N (the total number of vertices. This is because the shortest path can be obtained at most n-1 times of side relaxation. If a node is more than N, there must be a negative ring, that is, there is no shortest path.
#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 negative ring)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.