洛谷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。

分析:比较经典的最小生成树的应用.两个点之间的路径最大值的最小值一定在最小生成树上。我们可以在每次加完边之后看看s和t有没有连通,如果连通了,直接输出答案就好了.

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <cmath>

using namespace std;

int n,m,s,t,fa[20010],ans;

struct node
{
       int x,y,z;    
}e[20010];

bool cmp(node a,node b)
{
    return a.z < b.z;
}

int find(int x)
{
    if (x == fa[x])
    return x;
    return fa[x] = find(fa[x]);
}

int main()
{
    scanf("%d%d%d%d",&n,&m,&s,&t);
    for (int i = 1; i <= n; i++)
    fa[i] = i;
    for (int i = 1; i <= m; i++)
    scanf("%d%d%d",&e[i].x,&e[i].y,&e[i].z);
    sort(e + 1,e + 1 + m,cmp);
    for (int i = 1; i <= m; i++)
    {
        int fx = find(e[i].x),fy = find(e[i].y);
        if (fx != fy)
        {
            fa[fx] = fy;
            ans = max(ans,e[i].z);
        }
        if (find(s) == find(t))
        break;
    }
    printf("%d\n",ans);

    return 0;
}

 

posted @ 2017-09-12 21:59  zbtrs  阅读(187)  评论(0编辑  收藏  举报