(intermediate) UVA (最小瓶颈生成树) 10457 - Magic Car

Problem E

Magic Car

Input: standard input

Output: standard output

Time Limit: 5 seconds

 

ACM ( Association of Car Modernization) has recently developed a new car, "MAGIC CAR". It uses solar energy. The car has some interesting characteristics : 

  • It uses up constant amount of energy at the start for any initial speed.

  • It also uses up constant energy when stops.

  • If it changes speed to a value which is less than already achieved least speed or greater than already achieved most speed, it uses up some energy. In both cases the energy is equal to the absolute difference of the current speed and previously achieved least or most speed.

  • The loss of energy does not depend on the distance the car covered (Really magic!!!).

    Mr. Oberoy has such a magic car. So far he has used the car quite intelligently so that minimum energy is used up. This was easy for him as he could take any speed on any road. But recently, TCD(Transport Control Department) has decided that there will be a fixed speed for each road in the city and everybody must maintain the speed. Mr. Oberoy is in problem now. Can you help him so that he can optimally use the car ?

    Input

    Each dataset starts with two positive integer, N (2<=N<=200) denoting the number of junctions and M (1<=M<=1000) denoting the number of roads in Mr. Oberoy's city. Each junction is identified by a unique integer from 1 to N. In next few lines there will be road descriptions. A road is described by three positive integers which are start, end junctions and the fixed speed of that road. There may be more than one roads between two junctions. Roads are bidirectional. In the next line there will be two positive integers which are the used up energy during start and stop of the magic car. Next line will contain an integer K (1<=K<=5) indicating the number of queries. Each of following K lines will contain two integers, the source and destination junction of Mr. Oberoy. Source and destination will not be same. Input is terminated by EOF.


    Output

    For each dataset print the minimum possible used up energy for each query of Mr. Oberoy. It is guranteed that the destination is always reachable from source.

     

    Sample Input

    4 41 2 22 3 41 4 13 4 25 521 31 2

    Sample Output

    1110

Author : Md. Kamruzzaman

The Real Programmers' Contest-2



题意:你有一辆很magic的车-_- ,如果你的驾驶速度比以前的最大速度要大,就要消耗相应的差值的能量,如果速度比以前的最小速度要小,也要消耗相应的差值的能量。然后有一个无向图,边的权值代表你必须要以这样的速度经过这条边,问从s,到t的最小能量消耗。


思路:这道题目乍看像一道最短路的题目。。。我确实这样去尝试了。。肯定不是那么好做的。状态要用三维来表示,200*1000*1000,肯定超时啦。。。。于是思考过后发现,这道题目要做的其实是找出一条从s到t的路径,使得路径中的最大权值减去最小权值的值最小。 于是怎么办呢? 我想到了最小瓶颈生成树(最大边最小的最小生成树),我们枚举每一个下界,然后用Kruskal找最小生成树,如果s,t被合并到同一个集合,那么这条让他们合并的这条边就是s到t的最大权值。那么我们不妨让答案为最大权值减去当前的下界(这个下界可能不在s到t的路径中,但是总有一天能有一个下界在s到t里面的)。枚举完后答案就出来了。复杂度是O(m*mlog(m)) 恩!不会超时的。


代码:

#include<iostream>
#include<cstdio>
#include<string.h>
#include<vector>
#include<algorithm>
#include<queue>
using namespace std;
const int maxn=200+5,maxm=1000+5;
const int inf=1e8;
struct Edge
{
    int u,v;
    int w;
    Edge(int u=-1,int v=-1,int w=-1)
    :u(u),v(v),w(w) { }
    bool operator<(const Edge&e) const
    {
        return w < e.w;
    }
};

int p[maxn];

int find(int x)
{
    if(x==p[x]) return x;
    return p[x]=find(p[x]);
}

int n,m;
Edge edges[maxm];

void input()
{
    for(int i=0;i<m;++i)
    {
        int u,v,w;
        scanf("%d%d%d",&u,&v,&w);
        edges[i]=Edge(u,v,w);
    }
    sort(edges,edges+m);
}

int s,t;
int Kruskal(int minw)
{
    for(int i=1;i<=n;++i) p[i]=i;
    for(int i=minw;i<m;++i)
    {
        int u=find(edges[i].u),v=find(edges[i].v);
        if(u==v) continue;
        p[u]=v;
        if(find(s)==find(t)) return edges[i].w;
    }
    return -1;
}

void solve()
{
    int c1,c2;
    scanf("%d%d",&c1,&c2);
    int K;scanf("%d",&K);
    while(K--)
    {
        int ans=inf;
        scanf("%d%d",&s,&t);
        for(int i=0;i<m;++i)
        {
            int ret=Kruskal(i);
            if(ret==-1) break;
            ans=min(ans,ret-edges[i].w);
        }
        printf("%d\n",ans+c1+c2);
    }
}


int main()
{
    while(scanf("%d%d",&n,&m)==2)
    {
        input();
        solve();
    }
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值