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\n",ans+tmp);
        }
    }
    return 0;
}

时间: 2024-08-04 01:06:41

uva 10457 Magic Car的相关文章

UVA 10457 - Magic Car(最小瓶颈路)

UVA 10457 - Magic Car 题目链接 题意:m条路,每条路上必须维持速度v,现在有一辆车,启动能量和结束能量为a, b,途中消耗能量为经过路径最大速度减去最小速度,现在每次循环给定起点终点,问最小能量花费 思路:最小瓶颈路,利用kruskal去搞 代码: #include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int N = 205; con

UVA 10457 - Magic Car【最小瓶颈树】

题目链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&category=600&problem=1398&mosmsg=Submission+received+with+ID+14106648 题意: m条路,每条路上必须维持速度v,现在有一辆车,启动能量和结束能量为a, b,途中消耗能量为经过路径最大速度减去最小速度,现在每次循环给

UVA - 471 Magic Numbers

Description  Magic Numbers  Write a program that finds and displays all pairs ofintegers and such that: neither nor have any digits repeated; and , where N is a given integer; Input and Output The input file consist a integer at the beginning indicat

UVA - 769 Magic of David Copperfield

题意:著名的魔术师大卫科波菲尔喜欢表演下面的魔术:一个N行N列不同图片的矩阵出现在大屏幕上,我们给所有的图片这样命名: 每一个参与的观众被要求将手指放在左上方的图片上(即编号为1的图片),魔术师开始了:魔术师告诉观众在图片上移动k次(移动是把手指放到上下左右相邻的图片上,如果那儿有图片的话),然后他(魔术师)的手微微一指(指向一些图片)并说:"你不在这里",然后--是真的!你的手指没有指向任何一个被删除的图片(指向的图片)然后再来一次,他告诉观众再移动K2次--以此类推.在最后,他删除

uva 10457(最小瓶颈路)

比赛的时候读错题了,赛后非要建最小生成树然后dfs暴搜,有人告诉我不行,我还非要改一遍,改了一年,想明白了,不能保证下限,比如2,3,5能使两个点连同,4,5也能的话,就不对了,想想我也是个铁头娃 #include <iostream> #include <cstring> #include <algorithm> #include <cstdio> #include <queue> using namespace std; const int

Uva 11996 Jewel Magic (Splay)

Jewel Magic Description I am a magician. I have a string of emeralds and pearls. I may insert new jewels in the string, or remove old ones. I may even reverse a consecutive part of the string. At anytime, if you point to two jewels and ask me, what i

UVa 11996 Jewel Magic (splay + Hash + 二分)

题意:给定一个长度为n的01串,你的任务是依次执行如表所示的m条指令: 1 p c 在第p个字符后插入字符,p = 0表示在整个字符串之前插入2 p 删除第p个字符,后面的字符往前移3 p1 p2反转第p1到第p2个字符4 p1 p2输出从p1开始和p2开始的两个后缀的LCP. 析:对于前三个操作,splay 很容易就可以解决,但是对于最后一个操作,却不是那么容易,因为这是动态的,所以我们也要维护一个可以动态的,这就可以用Hash来解决,由于要翻转,所以要维护两个,一个正向的,一个反向的.在操作

UVA 11996 Jewel Magic —— splay、序列的分裂与合并、LCP的哈希算法

1 #include <cstdio> 2 #include <cstdlib> 3 #include <iostream> 4 #include <algorithm> 5 #include <vector> 6 7 using namespace std; 8 9 typedef unsigned long long ull; 10 const int x = 123; 11 const int maxn = 4e5 + 10; 12 13

UVA - 1494 Qin Shi Huang&#39;s National Road System (类次小生成树)

Description During the Warring States Period of ancient China(476 BC to 221 BC), there were seven kingdoms in China -- they were Qi, Chu, Yan, Han, Zhao, Wei and Qin. Ying Zheng was the king of the kingdom Qin. Through 9 years of wars, he finally con