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
  • 相关阅读:
    R 读取xls/xlsx文件
    网页免费转换为可编辑的PDF
    Python: NumPy, Pandas学习资料
    鱼油资料
    Activity的四种启动模式和onNewIntent()
    Android Service、IntentService,Service和组件间通信
    Activity生命周期
    Node.js学习起步
    Android 技能图谱学习路线
    Blog
  • 原文地址:https://www.cnblogs.com/jihe/p/5349161.html
Copyright © 2011-2022 走看看