最大生成树+map实现技巧
阅读原文时间:2023年07月10日阅读:1

POJ2263

//#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define pw(x) (1ll << (x)) #define sz(x) ((int)(x).size()) #define all(x) (x).begin(),(x).end() #define rep(i,l,r) for(int i=(l);i<(r);i++) #define per(i,r,l) for(int i=(r);i>=(l);i--)
#define FOR(i,l,r) for(int i=(l);i<=(r);i++) #define debug1(a) cout << #a << " = " << a << endl; #define debug2(a,b) cout << #a << " = " << a << endl;\ cout << #b << " = " << b << endl; #define debug3(a,b,c) cout << #a << " = " << a << endl;\ cout << #b << " = " << b << endl;\ cout << #c << " = " << c << endl; #define debug4(a,b,c,d)\ cout << #a << " = " << a << endl;\ cout << #b << " = " << b << endl;\ cout << #c << " = " << c << endl;\ cout << #d << " = " << d << endl; #define eps 1e-9 #define PIE acos(-1) #define cl(a,b) memset(a,b,sizeof(a)) #define fastio ios::sync_with_stdio(false);cin.tie(0); #define lson l , mid , ls #define rson mid + 1 , r , rs #define ls (rt<<1) #define rs (ls|1) #define INF 0x3f3f3f3f #define lowbit(x) (x&(-x)) #define sqr(a) a*a #pragma GCC optimize(2) using namespace std; typedef double db; typedef long long ll; typedef vector vi;
typedef pair pii;

const int maxn=2e5;
int u[maxn],v[maxn],w[maxn],p[maxn],r[maxn];
int n,m,cnt;
mapMAP;

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 ;
}