Ultraviolet A 11090-Going in Cycle !!

Source: Internet
Author: User

Binary + SPFA negative ring search

11090-Going in Cycle !! Time limit: 3.000 seconds

I U P C 2 0 0 6

Problem G: Going in Cycle !!

Input: standard input

Output: standard output

You are given a weighted directed graphNVertices andMEdges. each cycle in the graph has a weight, which equals to sum of its edges. there are so many cycles in the graph with different weights. in this problem we want to find a cycle with the minimum mean.

Input

The first line of input gives the number of cases,N.NTest cases follow. Each one starts with two numbersNAndM.MLines follow, each has three positive numberA, B, cWhich means there is an edge from vertexAToBWith weightC.

Output

For each test case output one line containing"Case # x:"Followed by a number that is the lowest mean cycle in graph with 2 digits after decimal place, if there is a cycle. Otherwise print"No cycle found.".

Constraints

-N ≤ 50

-A, B ≤ n

-C ≤ 10000000

Sample Input

Output for Sample Input

2
2 1
1 2 1
2 2
1 2 2
2 1 3

Case #1: No cycle found.
Case #2: 2.50

Problemsetter: Hammad Tavakoli Ghinani

Alternate Solution: Cho



#include 
 
  #include 
  
   #include 
   
    #include #include 
    
     using namespace std;const double INF=1000000000.;struct Edge{    int to,next;    double w;}edge[110000];int Adj[100],Size=0,n,m;void init(){    Size=0; memset(Adj,-1,sizeof(Adj));}void Add_Edge(int u,int v,double w){    edge[Size].to=v;    edge[Size].next=Adj[u];    edge[Size].w=w;    Adj[u]=Size++;}double dist[110];int cq[110]; bool inq[110];bool spfa(double mid){    queue
     
       q;    for(int i=1;i<=n;i++)    {        q.push(i);        dist[i]=INF; cq[i]=0; inq[i]=false;    }    while(!q.empty())    {        int u=q.front(); q.pop();        inq[u]=false;        for(int i=Adj[u];~i;i=edge[i].next)        {            int v=edge[i].to;            if(dist[v]>dist[u]+edge[i].w-mid)            {                dist[v]=dist[u]+edge[i].w-mid;                if(!inq[v])                {                    inq[v]=true;                    cq[v]++;                    if(cq[v]>=n) return false;                    q.push(v);                }            }        }    }    return true;}int main(){    int T_T,cas=1;    scanf("%d",&T_T);    while(T_T--)    {        scanf("%d%d",&n,&m);        init();        double ans=INF;        double low=0,high=0,mid;        for(int i=0;i
      
       1e-3) { mid=(high+low)/2.; if(spfa(mid)==false) { ans=min(ans,mid); high=mid; } else low=mid; } printf("%.2lf\n",ans); } return 0;}
      
     
    
   
  
 


Related Article

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.