P1396 营救 洛谷

https://www.luogu.org/problem/show?pid=1396

题目描述

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

妈妈下班回家,街坊邻居说小明被一群陌生人强行押上了警车!妈妈丰富的经验告诉她小明被带到了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。

#include <algorithm>
#include <iostream>
#include <cstdio>

using namespace std;

int n,m,s,t,x,y,z,tot,ans;
int fa[100015];
struct node
{
    int u,v,w;
}e[10000*5];

void add(int a,int b,int c)
{
    tot++;
    e[tot].u=a;
    e[tot].v=b;
    e[tot].w=c;
}

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

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

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++)
    {
        cin>>x>>y>>z;
        add(x,y,z);
    }
    sort(e+1,e+tot+1,cmp);
    
    for(int i=1;i<=tot;i++)
    {
        int fx=find(e[i].u),fy=find(e[i].v);
        if(fx!=fy)
        {
            fa[fx]=fy;
            ans=e[i].w;
        }
        int ans_x=find(s),ans_y=find(t);
        if(ans_x==ans_y)
        {
            printf("%d",ans);
            return 0;
        }
    }
    return 0;
}

 

posted @ 2017-03-11 09:32  Aptal丶  阅读(184)  评论(0编辑  收藏  举报