zoukankan      html  css  js  c++  java
  • uva 10457 Magic Car

    题意:n个点,m条路,每次启动和停止汽车分别需要x,y能量,给出每条路和路上的速度,额外消耗的能量是所有走过路的最大差,每次给出起点和终点,输出最小能量

    分析:启动和终止的能量必须消耗,然后就是求起点和终点之间的最小速度差了,紫书11章有道题是求最小瓶颈树,和这个条件是差不多的,不同是生成一棵树,受这道题的启发,把所有边按照速度排序

    然后枚举起点边,用并查集判断x,y两点是否联通

    #include<algorithm>
    #include<cstdio>
    #include<cstring>
    #include<iostream>
    using namespace std;
    const int maxn=1005;
    
    struct node{
        int u,v,w;
        bool operator <(node& r) const{
            return w<r.w;
        }
    }p[maxn];
    
    int f[maxn];
    
    int find(int x){
        return f[x]==x?x:f[x]=find(f[x]);
    }
    
    int main(){
        int n,m,u,v,w,ans;
        while(~scanf("%d%d",&n,&m)){
            for(int i=0;i<m;i++)
              scanf("%d%d%d",&p[i].u,&p[i].v,&p[i].w);
            sort(p,p+m);
            scanf("%d%d",&u,&v);
            ans=u+v;
            scanf("%d",&w);
            while(w--){
                int tmp=0x3f3f3f3f;
                scanf("%d%d",&u,&v);
                for(int i=0;i<m;i++){
                    for(int j=1;j<=n;j++)f[j]=j;
                    for(int j=i;j<m;j++){
                        int t1=find(p[j].u);
                        int t2=find(p[j].v);
                        if(t1!=t2)f[t1]=t2;
                        if(find(u)==find(v)){
                            tmp=min(tmp,p[j].w-p[i].w);
                            break;
                        }
                    }
                }
                printf("%d
    ",ans+tmp);
            }
        }
        return 0;
    }
    View Code
  • 相关阅读:
    JS 禁止刷新和右键
    报错 避免重复
    CSS 总结
    CSS BUG 总结
    安装 mrtg
    人人网 网站接入总结
    PHPcms 把盛大登陆换成人人网登陆
    HTML 相同name 传递一个数组
    file_get_contents无法请求https连接的解决方法
    现货黄金白银上阻力位和压力位的确定和应用
  • 原文地址:https://www.cnblogs.com/jihe/p/5349161.html
Copyright © 2011-2022 走看看