zoukankan      html  css  js  c++  java
  • 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。

    #include<bits/stdc++.h>
    using namespace std;
    #define N 20000+1
    struct node{
        int x,y,z;
    }s[N];
    int f[N],n,m,ts,tt;
    bool ans=true;
    bool cmp(node x,node y)
    {
        return x.z<y.z;
    }
    int find(int x)
    {
        if(f[x]==x)return x;
        return f[x]=find(f[x]);
    }
    void unionn(int x,int y)
    {
        int u=find(x);
        int d=find(y);
        if(u!=d)f[u]=d;
        return;
    }
    void kruskal()
    {
        for(int i=1;i<=n;i++)f[i]=i;
        for(int i=1;i<=m;i++)
        {
            int x=s[i].x;
            int y=s[i].y;
            unionn(x,y);
            if(find(ts)==find(tt))
            {
                cout<<s[i].z<<endl;
                ans=false;
                return;
            }
        }
    }
    int main()
    {
        cin>>n>>m>>ts>>tt;
        for(int i=1;i<=m;i++)
            cin>>s[i].x>>s[i].y>>s[i].z;
        sort(s+1,s+m+1,cmp);
        kruskal();
        if(ans)cout<<s[m].z<<endl;
    }
  • 相关阅读:
    错位排序
    不容易系列之(4)——考新郎
    大数乘法
    神、上帝以及老天爷(错位排序)
    学生成绩……
    蛇形矩阵
    topcoder
    进制转换
    问题 1011
    topcoder 针对C程序员的TopCoder C++ (快速掌握)_ixigua—仅有杀毒软件是不够的…… .
  • 原文地址:https://www.cnblogs.com/jzxnl/p/11403545.html
Copyright © 2011-2022 走看看