很好的一道题。。

首先把边权排序。然后枚举最小的边,再依次添加不小于该边的边,直到s和t联通。用并查集维护即可。

# include <cstdio>
# include <cstring>
# include <cstdlib>
# include <iostream>
# include <vector>
# include <queue>
# include <stack>
# include <map>
# include <set>
# include <cmath>
# include <algorithm>
using namespace std;
# define lowbit(x) ((x)&(-x))
# define pi acos(-1.0)
# define eps 1e-
# define MOD
# define INF
# define mem(a,b) memset(a,b,sizeof(a))
# define FOR(i,a,n) for(int i=a; i<=n; ++i)
# define FO(i,a,n) for(int i=a; i<n; ++i)
# define bug puts("H");
# define lch p<<,l,mid
# define rch p<<|,mid+,r
# define mp make_pair
# define pb push_back
# define set pabs
typedef pair<int,int> PII;
typedef vector<int> VI;
# pragma comment(linker, "/STACK:1024000000,1024000000")
typedef long long LL;
int Scan() {
int res=, flag=;
char ch;
if((ch=getchar())=='-') flag=;
else if(ch>=''&&ch<='') res=ch-'';
while((ch=getchar())>=''&&ch<='') res=res*+(ch-'');
return flag?-res:res;
}
void Out(int a) {
if(a<) {putchar('-'); a=-a;}
if(a>=) Out(a/);
putchar(a%+'');
}
const int N=;
//Code begin...
typedef struct{int u, v, w;}Node;
Node edge[];
int fa[N]; bool comp(Node a, Node b){return a.w<b.w;}
int find(int x)
{
int s, temp;
for (s=x; fa[s]>=; s=fa[s]) ;
while (s!=x) temp=fa[x], fa[x]=s, x=temp;
return s;
}
void union_set(int x, int y)
{
int temp=fa[x]+fa[y];
if (fa[x]>fa[y]) fa[x]=y, fa[y]=temp;
else fa[y]=x, fa[x]=temp;
}
int gcd(int x, int y){return y?gcd(y,x%y):x;}
int main ()
{
int n, m, u, v, mi, ma, s, t, ans[];
double answer=INF;
scanf("%d%d",&n,&m);
FOR(i,,m) scanf("%d%d%d",&edge[i].u,&edge[i].v,&edge[i].w);
scanf("%d%d",&s,&t);
sort(edge+,edge+m+,comp);
FOR(i,,m) {
mem(fa,-);
mi=edge[i].w; ma=;
FOR(j,i,m) {
u=edge[j].u, v=edge[j].v;
if (find(u)!=find(v)) union_set(find(u),find(v));
if (find(s)==find(t)) {ma=edge[j].w; break;}
}
if (ma) if (answer>(double)ma/mi) answer=(double)ma/mi, ans[]=ma/gcd(ma,mi), ans[]=mi/gcd(ma,mi);
}
if (fabs(answer-INF)<eps) puts("IMPOSSIBLE");
else {
if (ans[]==) printf("%d\n",ans[]);
else printf("%d/%d\n",ans[],ans[]);
}
return ;
}

最新文章

  1. 在ubuntu 12.04 x64下编译hadoop2.4
  2. 在 Windows 和 Linux(Gnome) 环境下 从命令界面打开网页的方式
  3. 一行代码实现headView弹簧拉伸效果
  4. 挑逗B少年搞计划10 假设你是愿意用我的心脏层剥离一层~
  5. 【Python 函数对象 命名空间与作用域 闭包函数 装饰器 迭代器 内置函数】
  6. 通过AIDL在两个APP之间Service通信
  7. 对 Undefined 与 Null 的一些理解
  8. Synchronized使用方法
  9. C#轻量级配置文件组件EasyJsonConfig
  10. Linux开机自启配置
  11. 鼠标滑轮事件QWheelEvent
  12. Smali基本语法
  13. sql索引唯一
  14. R语言:变量名称和字符串的转换
  15. Python之输出当前时间
  16. HDU 1392 Surround the Trees(几何 凸包模板)
  17. [翻译] BAFluidView
  18. GO函数总结(转)
  19. 【原创】8. MYSQL++中的Row类型
  20. 实践作业3:白盒测试----简单介绍被测系统DAY4

热门文章

  1. SQl 语句 表的连接
  2. onenet基础通信套件加B300移植
  3. C# 简单工厂
  4. 「题目代码」P1044~P1048(Java)
  5. HTML &lt;head&gt;里面的标签
  6. selenium自动化之显式等待和EC(expected_conditions)模块
  7. SpriteKit手机游戏摇杆JoyStick的使用 -- by iFIERO游戏开发教程
  8. (Python爬虫04)了解通用爬虫和聚焦爬虫,还是理论知识.快速入门可以略过的
  9. POJ 2184 Cow Exhabition
  10. Spring 3整合Quartz 2实现定时任务:动态添加任务