P1396 营救

#include<cstdio>
#include<iostream>
#include<algorithm>
//#include<queue>
//#include<vector>
//#include<bits/stdc++.h>
#define ll long long
#define ddd printf("-----------------------\n");
using namespace std;
const int maxn=2e4+10 ;


int n,m,s,t,fa[maxn];

struct line{
    int x,y,w;
}a[maxn<<1];
bool cmp(line m,line n){
    return m.w<n.w;
}
int find(int x){
    return fa[x]==x? x:fa[x]=find(fa[x]);
}

int main()
{
    ios::sync_with_stdio(false);
    cin>>n>>m>>s>>t;
    for(int i=1;i<=n;i++) fa[i]=i;
    
    for(int i=1;i<=m;i++){
        cin>>a[i].x>>a[i].y>>a[i].w;
    }
    sort(a+1,a+1+m,cmp);
    for(int i=1;i<=m;i++)
    {
        int f1=find(a[i].x),f2=find(a[i].y);
        if(f2!=f1) fa[f1]=f2;
        if(find(s)==find(t)){
            cout<<a[i].w<<'\n';
            return 0;
        } 
    }
    return 0;
}
/*

#include<bits/stdc++.h>
using namespace std;
inline int gi() {
    int x=0,w=0;char ch=0;
    while(!(ch>='0'&&ch<='9')) {
        if(ch=='-') w=1;
        ch=getchar();
    }
    while(ch>='0'&&ch<='9')
    x=(x<<3)+(x<<1)+(ch^48),ch=getchar();
    return w?-x:x;
}                //????
const int maxn=3e5+1;
const int maxm=1e5+1;
priority_queue< pair<int,int> > q;//???(?????)
int n,m,s,t,head[maxm],tot,dis[maxm],vis[maxm];
struct Edge {
    int next,now,w;
}edge[maxn];        
void make(int from,int to,int t) {
    edge[++tot].next=head[from];
    edge[tot].now=to;
    edge[tot].w=t;
    head[from]=tot;
}                    //???????
void dijkstra() {
    memset(dis,0x3f,sizeof(dis));
    memset(vis,0,sizeof(vis));
    dis[s]=0;
    q.push(make_pair(0,s));
    while(!q.empty()) {
        int x=q.top().second; q.pop();
        if(vis[x]) continue;
        vis[x]=1;
        for(int i=head[x];i;i=edge[i].next) {
            int k=max(dis[x],edge[i].w),r=edge[i].now;
            //?max????
                if(k<dis[r]) {
                    dis[r]=k;
                    q.push(make_pair(-dis[r],r)); 
                    //????????????!!
                }
        }
    }

}
int main()
{
    n=gi(); m=gi(); s=gi(); t=gi();
    for(int i=1,x,y,z;i<=m;i++) {
        x=gi(); y=gi(); z=gi();
        make(x,y,z);
        make(y,x,z);
        //????
    }
    dijkstra();
    printf("%d",dis[t]);
    return 0;
}
*/
/*
#include <cstdio>
#include <cmath>
#include <cstring>
#include <queue>
#include <iostream>
#include <algorithm>
using namespace std;
int h[50001],ne[50001],w[50001],to[50001],en=0;
int n,m,S,T;
int dis[10001];
inline void add(int a,int b,int c)
{ne[en]=h[a];w[en]=c;to[en]=b;h[a]=en++;}
inline bool SPFA(int mid)
{
    memset(dis,0x3f,sizeof dis);
    queue<int>q;
    q.push(S);
    dis[S]=1;
    while (!q.empty())
    {
        int x=q.front();q.pop();
        for (int i=h[x];i>=0;i=ne[i])
        if (w[i]<=mid){
            if (dis[to[i]]==0x3f3f3f3f)
            {
                dis[to[i]]=1;
                q.push(to[i]);
            }
        }
    }
    if (dis[T]==0x3f3f3f3f) return false;
    else return true;
}
int main()
{
    memset(h,-1,sizeof h);
    scanf("%d%d%d%d",&n,&m,&S,&T);
    for (int i=1;i<=m;++i)
    {
        int a,b,c;
        scanf("%d%d%d",&a,&b,&c);
        add(a,b,c);
        add(b,a,c);
    }
    int l=0,r=10000;
    while (l<r)
    {
        int mid=(l+r)/2;
        if (SPFA(mid)) r=mid;
        else l=mid+1;
    }
    printf("%d\n",l);
}
*/

 

posted @ 2023-09-17 16:03  JMXZ  阅读(9)  评论(0)    收藏  举报