# BZOJ1050旅游

Description

Input

Output

4 2
1 2 1
3 4 2
1 4

3 3
1 2 10
1 2 5
2 3 8
1 3

3 2
1 2 2
2 3 4
1 3

IMPOSSIBLE

5/4

2

1<  N < = 500
1 < = x, y < = N，0 < v < 30000，x ≠ y
0 < M < =5000

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;

struct node{
int x,y,w;
}e[5001];

int n,m;
int s,t;
int fa[501];

bool cmp(node a,node b)
{
return a.w<b.w;
}

int find(int x)
{
return fa[x]==x?x:fa[x]=find(fa[x]);
}

int gcd(int a,int b)
{
int r;
while(b>0)
{
r=a%b;
a=b;
b=r;
}
return a;
}

int main()
{

cin >> n >> m;
for (int i=1; i<=m; i++) cin >> e[i].x >> e[i].y >> e[i].w;
cin >> s >> t;
sort(e+1,e+m+1,cmp);
int f=1;
int ansmx=0,ansmn=0;
int mx=0,mn=0;
while (f<m)
{
mx=0; mn=0;
for (int i=1; i<=n; i++) fa[i]=i;
while (f<=m)
{
int fx=find(e[f].x),fy=find(e[f].y);
fa[fx]=fy;
if (find(s)==find(t)) { mx=e[f].w; break; }
f++;
}
if (mx==0)
if (!ansmx) { cout << "IMPOSSIBLE" << endl; return 0; }//找不到从s到t的路
else break;
for (int i=1; i<=n; i++) fa[i]=i;//初始化
while (f>=0)
{
int fx=find(e[f].x),fy=find(e[f].y);
fa[fy]=fx;
if (find(s)==find(t)) { mn=e[f].w; break; }
f--;
}
if (mn==0)
if (!ansmn) { cout << "IMPOSSIBLE" << endl; return 0; }
else break;
f++;//从下一条边开始枚举
int r=gcd(mx,mn);
mx/=r; mn/=r;//约分
if (mx*ansmn<mn*ansmx || (!ansmx && !ansmn)) { ansmx=mx; ansmn=mn; }
}
if (ansmn==1) cout << ansmx;
else cout << ansmx << '/' << ansmn << endl;
return 0;
}

posted @ 2015-04-05 16:10  竹夭公子  阅读(117)  评论(0编辑  收藏