【CCF】地铁修建 改编Dijkstra

【题意】

给定有n个点,m条边的无向图,没有平行边和自环,求从1到n的路径中,最长段的最小值(最短路不再是路径和,而是所有段中的最大值)

【AC】

#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<algorithm>
#include<vector>
#include<queue>

using namespace std;
const int maxn=1e5+2;
const int maxm=2e5+2;
const int inf=0x3f3f3f3f;
int n,m;
struct edge{
    int to;
    int nxt;
    int w;
}e[2*maxm];
struct node{
    int id;
    int d;
    node(int _id,int _d):id(_id),d(_d){}
    bool operator < (const node& a) const{
        return d>a.d;
    }
};
int dis[maxn];
bool vis[maxn];
int head[maxn];
int tot;
void init(){
    memset(head,-1,sizeof(head));
    tot=0;
}
void add(int u,int v,int w){
    e[tot].to=v;
    e[tot].w=w;
    e[tot].nxt=head[u];
    head[u]=tot++;
}

int work(){
    memset(dis,inf,sizeof(dis));
    memset(vis,false,sizeof(vis));
    dis[1]=0;
    priority_queue<node> Q;
    Q.push(node(1,dis[1]));
    while(!Q.empty()){
        node q=Q.top();
        Q.pop();
        int u=q.id;
        if(vis[u]) continue;
        vis[u]=true;
        for(int i=head[u];i!=-1;i=e[i].nxt){
            int v=e[i].to;
            int w=e[i].w;
            int tmp=max(q.d,w);
            if(tmp<dis[v]){
                dis[v]=tmp;
                Q.push(node(v,dis[v]));
            }
        }
    }
    return dis[n];
    
}
int main(){
    while(~scanf("%d%d",&n,&m)){
        init();
        int u,v,w;
        for(int i=1;i<=m;i++){
            scanf("%d%d%d",&u,&v,&w);
            add(u,v,w);
            add(v,u,w);
        }
        int ans=work();
        printf("%d\n",ans);
    }
    return 0;
} 

猜你喜欢

转载自www.cnblogs.com/itcsl/p/9190788.html