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; }

posted @ 2020-04-18 19:43  人生有味是清欢  阅读(200)  评论(0编辑  收藏  举报