zoukankan      html  css  js  c++  java
  • 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; }

  • 相关阅读:
    自己的第一个网页
    第一次爬虫测试
    科学计算与可视化
    python自顶向下的设计方法进行体育竞技分析
    python PIL库的相关操作
    python 关于身份证号码的相关操作
    jieba库
    汉诺塔
    Django项目中运行Scrapy项目
    **Django+Echart实现多个饼状图(echart数据格式问题 {value: 135, name: '视频广告'})
  • 原文地址:https://www.cnblogs.com/ruanmowen/p/12727645.html
Copyright © 2011-2022 走看看