• P1396 营救+最短路


    算法

    最短路+变式

    思路

    我们仔细思考一下,对于任意一个点,到它的最大拥挤度的最小值肯定是与它相邻的一个点的此值和他们之间的边权值取一个max,然后在所有max中取一个min!

    这样我们就可以想到只要把板子里的松弛稍微修改一下,即只需把原本的求和改为取max就ok了!

    另,注意此题应构无向图!!

    代码

    #include <cstdio>
    #include <algorithm>
    #include <cstring>
    #include <queue>
    using namespace std;
    
    const int M = 30001;
    inline int read() {
        int ret;bool flag=0;char c;
        while((c=getchar())<'0'||c>'9')flag^=!(c^'-');ret=c^48;
        while((c=getchar())>='0'&&c<='9') ret=(ret<<3)+(ret<<1)+(c^48);
        return flag?-ret:ret;
    }
    
    queue <int> Q;
    int fir[M>>1],nex[M],go[M],val[M];
    int dis[M>>1],vis[M>>1];
    int n,m,a,b,tot;
    
    inline void add_edge(int x,int y,int z)
    {
        nex[++tot] = fir[x],fir[x] = tot,go[tot] = y,val[tot] = z;
    }
    
    
    int main(void)
    {
        n = read();m = read();a = read();b = read();
        for(int i = 1;i <= m; i++)
        {
            int x,y,z;
            x = read();y = read();z = read();
            add_edge(x,y,z);
            add_edge(y,x,z);
        }
        for(int i = 1;i <= n; i++) dis[i] = 1e9  + 7;
        dis[a] = 0;
        Q.push(a);
        vis[a] = 1;
        while(!Q.empty())
        {
            int Now = Q.front();
            Q.pop();
            int Go;
            vis[Now] = 0;
            for(int i = fir[Now];i,Go = go[i];i=nex[i])
            {
                if(dis[Go] > max(dis[Now],val[i]))
                {
                    dis[Go] = max(dis[Now],val[i]);
                    if(!vis[Go]) Q.push(Go),vis[Go]=1;
                }
            }
        }
        if(dis[b] == 1e9+7) dis[b] = -1;
        printf("%d",dis[b]);
        return 0;
    }
    

      

      

    #include <cstdio>#include <algorithm>#include <cstring>#include <queue>usingnamespace std; constint M = 30001; inlineintread() { int ret;bool flag=0;char c; while((c=getchar())<'0'||c>'9')flag^=!(c^'-');ret=c^48; while((c=getchar())>='0'&&c<='9') ret=(ret<<3)+(ret<<1)+(c^48); return flag?-ret:ret; } queue <int> Q; int fir[M>>1],nex[M],go[M],val[M]; int dis[M>>1],vis[M>>1]; int n,m,a,b,tot; inlinevoid add_edge(int x,int y,int z) { nex[++tot] = fir[x],fir[x] = tot,go[tot] = y,val[tot] = z; } int main(void) { n = read();m = read();a = read();b = read(); for(int i = 1;i <= m; i++) { int x,y,z; x = read();y = read();z = read(); add_edge(x,y,z); add_edge(y,x,z); } for(int i = 1;i <= n; i++) dis[i] = 1e9 + 7; dis[a] = 0; Q.push(a); vis[a] = 1; while(!Q.empty()) { int Now = Q.front(); Q.pop(); int Go; vis[Now] = 0; for(int i = fir[Now];i,Go = go[i];i=nex[i]) { if(dis[Go] > max(dis[Now],val[i])) { dis[Go] = max(dis[Now],val[i]); if(!vis[Go]) Q.push(Go),vis[Go]=1; } } } if(dis[b] == 1e9+7) dis[b] = -1; printf("%d",dis[b]); return0; }

  • 相关阅读:
    sprintf函数%u输入long long int型数值异常
    关于smarty模板display函数的$compile_id 参数的意义
    打log的时候如果少写一个%d,cgi会core掉
    c++标准库的源码和SIG实现是什么关系
    2010
    jquery 中jsonp原理最简说明
    1月17日stl string阅读笔记
    Moss母版页制作详解(一)
    Moss中的权限操作
    动态添加和删除表格行
  • 原文地址:https://www.cnblogs.com/ruanmowen/p/12727645.html
Copyright © 2020-2023  润新知