码迷,mamicode.com
首页 > 其他好文 > 详细

luogu P1396 营救

时间:2017-05-18 18:44:54      阅读:170      评论:0      收藏:0      [点我收藏+]

标签:name   数字   clu   turn   生成树   names   ring   int   pre   

题目描述

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

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

kruskal跑一边最小生成树

第一次连到t时就为最路径大权中的最小值

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;

const int N=20006;
int n,m,s,t;
struct node{
    int x,y,z;
    bool operator < (const node &q)const
    {
        return z<q.z;
    }
}a[N];

int fa[N];

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

int main()
{
    
    scanf("%d%d%d%d",&n,&m,&s,&t);
    for(int i=1;i<=m;i++)
    {
        scanf("%d",&a[i].x);
        scanf("%d",&a[i].y);
        scanf("%d",&a[i].z);
    }
    for(int i=1;i<=n;i++)fa[i]=i;
    sort(a+1,a+m+1);
    int k=0;
    int ans=0;
    for(int i=1;i<=m;i++)
    {
        int x1=find(a[i].x);
        int x2=find(a[i].y);
        ans=max(ans,a[i].z);
        if(x1==x2)
        continue;
        fa[x2]=x1; 
        k++;
        if(k==n-1)
        break;
        if(find(s)==find(t))break;
    }
    printf("%d",ans);
    
}

 

 

luogu P1396 营救

标签:name   数字   clu   turn   生成树   names   ring   int   pre   

原文地址:http://www.cnblogs.com/sssy/p/6874748.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!