//#include
#include
#include
#include
#include
#include
#include
const int maxn=2e5;
int u[maxn],v[maxn],w[maxn],p[maxn],r[maxn];
int n,m,cnt;
map
int cmp(const int i,const int j){return w[i]>w[j];}
int find(int x){return p[x]==x?x:p[x]=find(p[x]);}
int kruscal(int t1,int t2)
{
int ans=;
rep(i,,maxn)p[i]=r[i]=i;
sort(r,r+m,cmp);
rep(i,,m){
int e=r[i];
int x=find(u[e]);
int y=find(v[e]);
if(x!=y){
p[x]=y;
if(find(t1)==find(t2)){
ans=w[e];
return ans;
}
}
}
return w[r[m]];
}
void Init()
{
MAP.clear();
cnt=;
}
int main()
{
int cas=;
while(cin>>n>>m,n&&m){
Init();
char s1[],s2[];
rep(i,,m){
scanf("%s%s%d",s1,s2,&w[i]);
if(!MAP.count(s1))MAP[s1]=cnt++;
if(!MAP.count(s2))MAP[s2]=cnt++;
u[i]=MAP[s1];
v[i]=MAP[s2];
// debug3(MAP[s1],MAP[s2],w[i]);
}
scanf("%s%s",s1,s2);
printf("Scenario #%d\n", ++cas);
printf("%d tons\n\n", kruscal(MAP[s1],MAP[s2]));
// printf("Scenario #%d\n%d tons\n\n",++cas,kruscal(MAP[s1],MAP[s2]));
}
return ;
}
手机扫一扫
移动阅读更方便
你可能感兴趣的文章