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;
}


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

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

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

posted on 2013-09-28 20:51  电子幼体  阅读(167)  评论(0编辑  收藏  举报

导航