fork download
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. #define ll long long
  5. #define inf 1e15
  6. vector<pair<pair<ll,ll> ,ll> >edge;
  7.  
  8. ll vis[1005];
  9. ll cost[1005];
  10.  
  11. int main()
  12. {
  13. ll i,j,n,m,t,a,b,c,p,d,e;
  14. scanf("%lld",&t);
  15. for(ll cz=1;cz<=t;cz++){
  16. printf("Case %lld: ",cz);
  17. memset(cost,0,sizeof(cost));
  18. scanf("%lld%lld%lld",&n,&m,&p);
  19. edge.clear();
  20.  
  21. for(i=1;i<=m;i++){
  22. scanf("%lld%lld%lld%lld",&a,&b,&d,&e);
  23. c=p*e-d;
  24. edge.push_back(make_pair(make_pair(a,b) ,c));
  25. }
  26.  
  27. ll len=edge.size();
  28. ll u,v,chk=0;
  29. for(j=1;j<=n;j++)
  30. for(i=0;i<len;i++){
  31. u=edge[i].first.first;
  32. v=edge[i].first.second;
  33. c=edge[i].second;
  34.  
  35. if(cost[v]>cost[u]+c){
  36. cost[v]=cost[u]+c;
  37. }
  38. }
  39.  
  40. for(i=0;i<len;i++){
  41. u=edge[i].first.first;
  42. v=edge[i].first.second;
  43. c=edge[i].second;
  44.  
  45. if(cost[v]>cost[u]+c&&vis[u]==0){
  46. //cout<<cost[v]<<" "<<v<<endl;
  47. cost[v]=cost[u]+c;
  48. //cout<<cost[v]<<" "<<v<<endl;
  49. chk=1;
  50. }
  51. }
  52.  
  53. if(chk==1)printf("YES\n");
  54. else printf("NO\n");
  55. }
  56. }
Success #stdin #stdout 0s 4684KB
stdin
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
stdout
Case 1: YES
Case 2: NO
Case 3: YES