zhber 有好多做过的题没写下来，如果我还能记得就补吧

【样例输入1】
4 2
1 2 1
3 4 2
1 4

【样例输入2】
3 3
1 2 10
1 2 5
2 3 8
1 3

【样例输入3】
3 2
1 2 2
2 3 4
1 3

## Sample Output

【样例输出1】
IMPOSSIBLE

【样例输出2】
5/4
【样例输出3】
2

【数据范围】
1< N < = 500
1 < = x, y < = N，0 < v < 30000，x ≠ y
0 < M < =5000

#include<cstdio>
#include<algorithm>
#include<cstdlib>
using namespace std;
struct node{
int from,to;
int path;
}data[5001];
int fa[501];

bool cmp(const node &a,const node &b)
{return a.path < b.path;}

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

int gcd(int a,int b)
{
if (b == 0) return a;
return gcd(b,a%b);
}

int min(int a,int b)
{return a<b?a:b;}

void quit()
{
printf("IMPOSSIBLE");
exit(0);
}
int main()
{
int n,m,s,t;
int savel = 0,saver = 0;
bool noans = 1;
scanf("%d%d",&n,&m);
for (int i=1;i<=m;i++)
scanf("%d%d%d",&data[i].from,&data[i].to,&data[i].path);
sort(data+1,data+m+1,cmp);
scanf("%d%d",&s,&t);
int start = 1;
while (start <= m)
{
int l = 0,r = 0,j = 0;
for (int i=1;i<=n;i++) fa[i] = i;
for (j=start;j<=m;j++)
{
int x = find(data[j].from);
int y = find(data[j].to);
fa[x] = y;
if (find(s) == find(t))
{
r = data[j].path;
break;
}
}
if (r == 0)
{
if (noans) quit();
break;
}
for (int i=1;i<=n;i++) fa[i] = i;
for (;j>=1;j--)
{
int x = find(data[j].from);
int y = find(data[j].to);
fa[x] = y;
if (find(s) == find(t))
{
l = data[j].path;
break;
}
}

start = j+1;
if (l == 0)
{
if (noans) quit();
break;
}
int d = gcd(r,l);
r /= d;
l /= d;
if (saver == 0 && savel == 0 )
{
noans = 0;
saver = r;
savel = l;
}else
if (saver*l > savel*r)
{
noans = 0;
savel = l;saver = r;
}
}

if (noans) quit();
else if (savel == 1) printf("%d",saver);
else printf("%d/%d",saver,savel);
} 

posted on 2014-07-31 21:10  zhber  阅读(90)  评论(0编辑  收藏