P2502 [HAOI2006]旅行 最小生成树

思路:枚举边集,最小生成树

提交:1次

题解:枚举最长边,添加较小边。

#include<cstdio>
#include<iostream>
#include<algorithm>
#define R register int
using namespace std;
#define ull unsigned long long
#define ll long long
#define pause (for(R i=1;i<=10000000000;++i))
#define In freopen("NOIPAK++.in","r",stdin)
#define Out freopen("out.out","w",stdout)
namespace Fread {
static char B[<<],*S=B,*D=B;
#ifndef JACK
#define getchar() (S==D&&(D=(S=B)+fread(B,1,1<<15,stdin),S==D)?EOF:*S++)
#endif
inline int g() {
R ret=,fix=; register char ch; while(!isdigit(ch=getchar())) fix=ch=='-'?-:fix;
if(ch==EOF) return EOF; do ret=ret*+(ch^); while(isdigit(ch=getchar())); return ret*fix;
} inline bool isempty(const char& ch) {return (ch<=||ch>=);}
inline void gs(char* s) {
register char ch; while(isempty(ch=getchar()));
do *s++=ch; while(!isempty(ch=getchar()));
}
} using Fread::g; using Fread::gs; namespace Luitaryi {
const int N=,M=;
int n,m,s,t,up,dn;
double anss=1E+;
int fa[N];
struct edge { int u,v,w;
inline bool operator < (const edge& that) const{return w<that.w;}
}e[M];
inline int getf(int x) {return x==fa[x]?x:fa[x]=getf(fa[x]);}
inline void main() {
n=g(),m=g();
for(R i=;i<=m;++i) e[i].u=g(),e[i].v=g(),e[i].w=g();
sort(e+,e+m+); s=g(),t=g();
for(R i=;i<=m;++i) { R ans=;//枚举下界,最小的边
for(R j=;j<=n;++j) fa[j]=j;
for(R j=i;j<=m;++j) {//往上枚举,直到两点连通
R uf=getf(e[j].u),vf=getf(e[j].v);
fa[uf]=vf;
if(getf(s)==getf(t)) {ans=j; break;}
} if(i==&&ans==) return (void)printf("IMPOSSIBLE\n");
if(ans==) break; register double tmp=1.0*e[ans].w/e[i].w;
if(tmp<anss) anss=tmp,up=e[ans].w,dn=e[i].w;
} R tmp=__gcd(up,dn); if(tmp==dn) printf("%d\n",up/dn);
else printf("%d/%d\n",up/tmp,dn/tmp);
}
}
signed main() {
Luitaryi::main();
return ;
}

2019.07.20

上一篇:Dynamics 365中开发和注册插件介绍


下一篇:Codeforces Round #468 Div. 1