zoukankan      html  css  js  c++  java
  • 【codevs1519】 过路费

    题目描述 Description

        在某个遥远的国家里,有 n个城市。编号为 1,2,3,…,n。这个国家的政府修建了m 条双向道路,每条道路连接着两个城市。政府规定从城市 S 到城市T需要收取的过路费为所经过城市之间道路长度的最大值。如:A到B长度为 2,B到C 长度为3,那么开车从 A经过 B到C 需要上交的过路费为 3。
        佳佳是个做生意的人,需要经常开车从任意一个城市到另外一个城市,因此他需要频繁地上交过路费,由于忙于做生意,所以他无时间来寻找交过路费最低的行驶路线。然而, 当他交的过路费越多他的心情就变得越糟糕。 作为秘书的你,需要每次根据老板的起止城市,提供给他从开始城市到达目的城市,最少需要上交多少过路费。

    输入描述 Input Description

        第一行是两个整数 n 和m,分别表示城市的个数以及道路的条数。 
        接下来 m 行,每行包含三个整数 a,b,w(1≤a,b≤n,0≤w≤10^9),表示a与b之间有一条长度为 w的道路。
        接着有一行为一个整数 q,表示佳佳发出的询问个数。 
        再接下来 q行,每一行包含两个整数 S,T(1≤S,T≤n,S≠T), 表示开始城市S 和目的城市T。

    输出描述 Output Description

        输出共q行,每行一个整数,分别表示每个询问需要上交的最少过路费用。输入数据保证所有的城市都是连通的。

    样例输入 Sample Input

    4 5 
    1 2 10 
    1 3 20 
    1 4 100 
    2 4 30 
    3 4 10 

    1 4 
    4 1

    样例输出 Sample Output

    20 
    20

    数据范围及提示 Data Size & Hint

    对于 30%的数据,满足 1≤ n≤1000,1≤m≤10000,1≤q≤100; 
    对于 50%的数据,满足 1≤ n≤10000,1≤m≤10000,1≤q≤10000; 
    对于 100%的数据,满足 1≤ n≤10000,1≤m≤100000,1≤q≤10000;

    题解:

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<cmath>
    #include<algorithm>
    using namespace std;
    const int maxn=10000+5;
    const int maxm=200000+5;
    int read()
    {
        int x=0,f=1; char ch=getchar();
        while(ch<'0'||ch>'9'){if(ch=='-') f=-1; ch=getchar();}
        while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
        return x*f;
    }
    int n,m,q,num;
    int father[maxn],dep[maxn],nxt[maxm],head[maxn],f[maxn],dis[maxn];
    bool vis[maxn];
    struct node
    {
        int from,to,dist;
        bool operator < (const node& j) const {
            return dist<j.dist;
        }
    }e[maxm],c[maxm];
    void add(int from,int to,int dist)
    {
        e[++num].from=from;
        e[num].to=to;
        e[num].dist=dist;
        nxt[num]=head[from];
        head[from]=num;
    }
    int find(int x)
    {
        if(x!=father[x]) father[x]=find(father[x]);
            return father[x];
    }
    void dfs(int x)
    {
        vis[x]=1;
        for(int i=head[x];i;i=nxt[i])
        {
            int to=e[i].to;
            if(!vis[to])
            {
                f[to]=x;
                dep[to]=dep[x]+1;
                dis[to]=e[i].dist;
                dfs(to);
            }
        }
    }
    int lca(int x,int y)
    {
        int maxx=0;
        if(dep[x]>dep[y]) swap(x,y);
        while(dep[x]<dep[y])
        {
            maxx=max(maxx,dis[y]);
            y=f[y];
        }
        while(x!=y)
        {
            maxx=max(maxx,dis[x]);
            maxx=max(maxx,dis[y]);
            x=f[x];
            y=f[y];
        }
        return maxx;
    }
        
    int main()
    {
        n=read();m=read();
        for(int i=1;i<=n;i++) father[i]=i;
        for(int i=1;i<=m;i++)
        {c[i].from=read();c[i].to=read();c[i].dist=read();}
        sort(c+1,c+1+m);
        for(int i=1;i<=m;i++)
        {
            int x=find(c[i].from);
            int y=find(c[i].to);
            if(x!=y)
            {
                father[x]=y;
                add(c[i].from,c[i].to,c[i].dist);
                add(c[i].to,c[i].from,c[i].dist);
            }
        }
        dfs(1);
        q=read();
        for(int i=1;i<=q;i++)
        {
            int a,b;
            a=read();b=read();
            printf("%d
    ",lca(a,b));
        }
        return 0;
    }
    欢迎转载,转载请注明出处!
  • 相关阅读:
    装饰
    统一软件开发过程之2:用例文本书写
    统一软件开发过程之1:创建领域模型
    工厂方法
    volatile
    中介者
    建造者
    C#委托,事件与回调函数
    控件资源嵌入
    装饰
  • 原文地址:https://www.cnblogs.com/huihao/p/7150379.html
Copyright © 2011-2022 走看看