luogu P2330 [SCOI2005]繁忙的都市

luogu P2330 [SCOI2005]繁忙的都市_第1张图片

分析

找MST中权值最大的边即可

code

#include
using namespace std;
#define loop(i,start,end) for(register int i=start;i<=end;++i)
#define clean(arry,num); memset(arry,num,sizeof(arry));
int n,m,cnt=0;
const int maxn=300+10,maxm=100000+10;
int head[maxn];
int dis[maxn];
bool vis[maxn];
struct node
{
    int e;int w;int nxt;
    node():e(0),w(0),nxt(0){}
    node(int e,int w,int nxt):e(e),w(w),nxt(nxt){}
}edge[maxm<<1];
struct sortnode
{
    int pos;int Dis;
    sortnode():pos(0),Dis(0){}
    sortnode(int pos,int dis):pos(pos),Dis(dis){}
    friend bool operator<(const sortnode &a,const sortnode &b)
    {
        return a.Dis>b.Dis;
    }
};
inline int read()
{
    int _ans=0;bool _neg=false;char _r=getchar();
    while(_r>'9'||_r<'0'){if(_r=='-')_neg=true;_r=getchar();}
    while(_r>='0'&&_r<='9'){_ans=_ans*10+_r-'0';_r=getchar();}
    return (_neg)?-_ans:_ans;
}
inline void addl(int u,int v,int w)
{
    edge[cnt]=node(v,w,head[u]);
    head[u]=cnt++;
}
inline void prim()
{
    clean(dis,0x7f);
    clean(vis,false);
    priority_queue<sortnode>q;
    dis[1]=0;
    q.push(sortnode(1,0));
    int maxx=0;
    int sum=0;
    int count=0;
    //loop(k,1,n)
    while(!q.empty()/*&&count)
    {
        sortnode _f=q.top();q.pop();int mindis=_f.Dis;int _u=_f.pos;
        sum+=mindis;
        if(vis[_u])continue;
        vis[_u]=true;
        maxx=max(mindis,maxx);
        for(int i=head[_u];i!=-1;i=edge[i].nxt)
        {
            int _v=edge[i].e;int _w=edge[i].w;
            if(/*!vis[_v]&&*/dis[_v]>_w)
            {
                dis[_v]=_w;
                q.push(sortnode(_v,dis[_v]));
            }
        }
        count++;
    }
    printf("%d %d",n-1,maxx);
}
int main()
{
    #ifndef ONLINE_JUDGE
    freopen("datain.txt","r",stdin);
    #endif
    n=read(),m=read();
    clean(head,-1);
    loop(i,1,m)
    {
    	int u,v,c;
    	u=read(),v=read(),c=read();
    	addl(u,v,c);
    	addl(v,u,c);
    }
    prim();
    return 0;
}

你可能感兴趣的:(图论算法)