P1396 营救

题目描述

“咚咚咚……”“查水表!”原来是查水表来了,现在哪里找这么热心上门的查表员啊!小明感动的热泪盈眶,开起了门……

妈妈下班回家,街坊邻居说小明被一群陌生人强行押上了警车!妈妈丰富的经验告诉她小明被带到了t区,而自己在s区。

该市有m条大道连接n个区,一条大道将两个区相连接,每个大道有一个拥挤度。小明的妈妈虽然很着急,但是不愿意拥挤的人潮冲乱了她优雅的步伐。所以请你帮她规划一条从s至t的路线,使得经过道路的拥挤度最大值最小。

输入输出格式

输入格式:

第一行四个数字n,m,s,t。

接下来m行,每行三个数字,分别表示两个区和拥挤度。

(有可能两个区之间有多条大道相连。)

输出格式:

输出题目要求的拥挤度。

输入输出样例

输入样例#1: 复制
3 3 1 3							
1 2 2
2 3 1
1 3 3
输出样例#1: 复制
2

说明

数据范围

30% n<=10

60% n<=100

100% n<=10000,m<=2n,拥挤度<=10000

题目保证1<=s,t<=n且s<>t,保证可以从s区出发到t区。

样例解释:

小明的妈妈要从1号点去3号点,最优路线为1->2->3。

spfa最短路的变式。

朴素spfa的dis[i]记录权值最小值的和,变为f[i]记录到i点的所有路径中最大边权的最小值

有点dp的感觉啦  f[v]=min(f[v],max(f[u],w))

反正,换汤不换药

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define inf 2147483647
const ll INF = 0x3f3f3f3f3f3f3f3fll;
#define ri register int
template <class T> inline T min(T a, T b, T c)
{
    return min(min(a, b), c);
}
template <class T> inline T max(T a, T b, T c)
{
    return max(max(a, b), c);
}
template <class T> inline T min(T a, T b, T c, T d)
{
    return min(min(a, b), min(c, d));
}
template <class T> inline T max(T a, T b, T c, T d)
{
    return max(max(a, b), max(c, d));
}
#define pi acos(-1)
#define me(x, y) memset(x, y, sizeof(x));
#define For(i, a, b) for (int i = a; i <= b; i++)
#define FFor(i, a, b) for (int i = a; i >= b; i--)
#define mp make_pair
#define pb push_back
const int maxn = 100005;
#define mod 100003
const int N=200005;

// name*******************************
struct edge
{
    int to,next,w;
} e[N];
int Head[N];
int tot=0;
int f[N];//1到i点路径的最大值中的最小值
queue<int>que;
int vis[N];
int n,m,s,t;
// function******************************
void add(int u,int v,int w)
{
    e[++tot].to=v;
    e[tot].w=w;
    e[tot].next=Head[u];
    Head[u]=tot;
}
void spfa(int x)
{
    me(vis,0);
    me(f,127);
    que.push(x);
    f[x]=0;
    vis[x]=1;
    while(!que.empty())
    {
        int u=que.front();
        que.pop();
        vis[u]=0;
        for(int p=Head[u]; p; p=e[p].next)
        {
            int v=e[p].to;
            int w=e[p].w;
            if(f[v]>max(f[u],w))
            {
                f[v]=max(f[u],w);
                if(!vis[v])
                {
                    vis[v]=1;
                    que.push(v);
                }
            }
        }
    }
}

//***************************************
int main()
{
    freopen("test.txt", "r", stdin);
    cin>>n>>m>>s>>t;
    For(i,1,m)
    {
        int a,b,c;
        cin>>a>>b>>c;
        add(a,b,c);
        add(b,a,c);
    }
    spfa(s);
    cout<<f[t];

    return 0;
}
原文地址:https://www.cnblogs.com/planche/p/8698658.html