When am I going to learn to write without looking at the puzzle ....
Sort all the edges from small to large, for the current edge as the maximum value, and then inverted for, update the answer. If the answer is not updated, there is impossible.
This is the second consecutive channel did not make out and check the set.
Some God Ben said with SPFA faster ... Not verified and not verified.
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#define MAXV 505
#define Maxe 5005
using namespace Std;
struct Edge
{
int u,v,w,nxt;
}e[maxe];
int s,t,n,m,g[maxv],x,y,z,father[maxv],nume=0;
int ans1=0,ans2=0;
Double ans=1234567;
void Addedge (int u,int v,int W)
{
E[++nume].u=u;
E[nume].v=v;
E[nume].w=w;
E[nume].nxt=g[u];
G[u]=nume;
}
BOOL CMP (Edge X,edge y)
{
Return x.w<y.w;
}
int getfather (int x)
{
if (X!=father[x])
Father[x]=getfather (Father[x]);
return father[x];
}
void Unionn (int x,int y)
{
X=getfather (x);
Y=getfather (y);
if (x!=y)
Father[x]=y;
}
void work (int x)
{
int flag=0;
for (int i=1;i<=n;i++)
Father[i]=i;
int maxmax,minmin;
MAXMAX=E[X].W;
for (int i=x;i>=1;i--)
{
int R1=getfather (E[I].U), R2=getfather (E[I].V);
if (R1!=R2) unionn (E[I].U,E[I].V);
if (Getfather (s) ==getfather (t))
{
MINMIN=E[I].W;
flag=1;
Break
}
}
if (flag==1)
{
Double k=double (Maxmax)/minmin;
if (K<ans)
{
Ans1=maxmax;
Ans2=minmin;
Ans=k;
}
}
}
int gcd (int a,int b)
{
if (b==0) return A;
return gcd (B,A%B);
}
int main ()
{
memset (G,0,sizeof (g));
scanf ("%d%d", &n,&m);
for (int i=1;i<=m;i++)
{
scanf ("%d%d%d", &x,&y,&z);
Addedge (x, y, z);
}
scanf ("%d%d", &s,&t);
Sort (e+1,e+m+1,cmp);
for (int i=1;i<=m;i++)
Work (i);
if ((ans1==0) && (ans2==0))
printf ("impossible\n");
Else
{
int P=GCD (ANS1,ANS2);
ans1=ans1/p;
ans2=ans2/p;
if (ans2!=1)
printf ("%d/%d", ans1,ans2);
else printf ("%d", ans1);
}
return 0;
}
Codevs 1001 Comfortable Route