UVA 10816 Travel in Desert 最短路+二分

--------------------

Description

There is a group of adventurers who like to travel in the desert. Everyone knows travelling in desert can be very dangerous. That's why they plan their trip carefully every time. There are a lot of factors to consider before they make their final decision.

One of the most important factors is the weather. It is undesirable to travel under extremely high temperature. They always try to avoid going to the hottest place. However, it is unavoidable sometimes as it might be on the only way to the destination. To decide where to go, they will pick a route that the highest temperature is minimized. If more than one route satisfy this criterion, they will choose the shortest one.

There are several oases in the desert where they can take a rest. That means they are travelling from oasis to oasis before reaching the destination. They know the lengths and the temperatures of the paths between oases. You are to write a program and plan the route for them.

Input

Input consists of several test cases. Your program must process all of them.

The first line contains two integers N and E (1 ≤ N ≤ 100; 1 ≤ E ≤ 10000) where N represents the number of oasis and E represents the number of paths between them. Next line contains two distinct integers S and T (1 ≤ ST ≤ N) representing the starting point and the destination respectively. The following E lines are the information the group gathered. Each line contains 2 integers X, Y and 2 real numbers R and D (1 ≤ XY ≤ N; 20 ≤ R ≤ 50; 0 < D ≤ 40). It means there is a path between X and Y, with length D km and highest temperature oC. Each real number has exactly one digit after the decimal point. There might be more than one path between a pair of oases.

Output

Print two lines for each test case. The first line should give the route you find, and the second should contain its length and maximum temperature.

Sample Input

6 9
1 6
1 2 37.1 10.2
2 3 40.5 20.7
3 4 42.8 19.0
3 1 38.3 15.8
4 5 39.7 11.1
6 3 36.0 22.5
5 6 43.9 10.2
2 6 44.2 15.2
4 6 34.2 17.4

Sample Output

1 3 6
38.3 38.3


--------------------

选一条温度最低且长度最短的路线。

二分温度,跑最短路即可。

--------------------

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;
const int maxn=211;
const int maxm=21111;
const int INF=0x3f3f3f3f;
const double OO=1e100;
const double eps=1e-6;
struct EdgeNode{
    int to;
    double w;
    double T;
    int next;
};

struct HeapNode{
    int d,u;
    bool operator<(const HeapNode& rhs) const{
        return d>rhs.d;
    }
};

struct Dijkstra{
    EdgeNode edges[maxm];
    int head[maxn];
    int edge,n;
    void init(int n){
        this->n=n;
        memset(head,-1,sizeof(head));
        edge=0;
    }
    void addedges(int u,int v,double w,double t){
        edges[edge].w=w,edges[edge].T=t,edges[edge].to=v,edges[edge].next=head[u],head[u]=edge++;
    }
    bool done[maxn];
    double dis[maxn];
    int pre[maxn];
    bool dijkstra(int s,int t,double c){
        priority_queue<HeapNode>que;
        for (int i=0;i<=n;i++) dis[i]=OO;
        dis[s]=0;
        memset(done,0,sizeof(done));
        que.push((HeapNode){0,s});
        while (!que.empty()){
            HeapNode x=que.top();
            que.pop();
            int u=x.u;
            if (done[u]) continue;
            done[u]=true;
            for (int i=head[u];i!=-1;i=edges[i].next){
                int v=edges[i].to;
                double w=edges[i].w;
                double tm=edges[i].T;
                if (tm<=c&&dis[v]>dis[u]+w){
                    dis[v]=dis[u]+w;
                    pre[v]=u;
                    que.push((HeapNode){dis[v],v});
                }
            }
        }
        if (dis[t]<OO) return true;
        return false;
    }
    void output(int s,int t){
        if (s!=t) output(s,pre[t]);
        else{
            printf("%d",s);
            return;
        }
        printf(" %d",t);
    }
}solver;
int n,m;
int s,t;
int main()
{
    while (~scanf("%d%d",&n,&m)){
        double l=OO;
        double r=0;
        scanf("%d%d",&s,&t);
        solver.init(n);
        for (int i=0;i<m;i++){
            int x,y;
            double w,tm;
            scanf("%d%d%lf%lf",&x,&y,&tm,&w);
            solver.addedges(x,y,w,tm);
            solver.addedges(y,x,w,tm);
            l=min(l,tm);
            r=max(r,tm);
        }
        while (r-l>eps){
            double mid=(l+r)/2;
            if (solver.dijkstra(s,t,mid)){
                r=mid;
            }
            else{
                l=mid;
            }
        }
        solver.dijkstra(s,t,r);
        solver.output(s,t);
        printf("\n");
        printf("%0.1f %0.1f\n",solver.dis[t],r);
    }
    return 0;
}


--------------------

--------------------

--------------------

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值