HDU 2363 Cycling: Two points + enumeration + limit shortest road, good question

Source: Internet
Author: User
Tags bool continue int size min ming

Link:

http://acm.hdu.edu.cn/showproblem.php?pid=2363

The main effect of the topic:

Xiao Ming walked from home to school to test, the road has various intersections, they have their own altitude. Xiao Ming from home to school on the road, will inevitably go through a different intersection, so will continue to walk down, 忐 disturbed prophecymusic, which makes him very uneasy, will affect his examination of the play. Therefore, he wants to choose a road that is the least undulating to go to school. The so-called "minimum fluctuation" means that the highest point on the road and the lowest point of elevation is the smallest difference.

Under the condition of minimum fluctuation, the shortest distance is also required.

Analysis and Summary:

This question reminds me of a previous one, HDU1598, but it was done with the minimum spanning tree, and the problem only needed to output the minimum error without seeking the shortest path.

In this problem, according to the increase of the height difference, the path number that satisfies the condition obviously is also increasing, so it can be divided into two "height difference".

It's not enough to have a "height difference". Because the "fluctuation value" is equal to the maximum height minus the minimum height, you need to enumerate the minimum height (lower low), get the maximum height (upper) according to the minimum height + "Height difference", and have both low and up conditions to limit the shortest path.

WA has 20 +, because in the shortest way is not ruled out more than the "upper limit" edge.

More Wonderful content: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/

After trying the minimum spanning tree method to do, first find the "minimum difference" and the upper and lower limits, and then find the shortest road, but WA, tangled in ...

Code:

Binary + enumeration + shortest-circuit

#include <iostream> #include <cstdio> #include <cstring> #include <queue> #include <algorit  
    
Hm> using namespace std;  
const int INF = 0x7fffffff;  
const int VN = 120;  
    
const int EN = 10005;  
int n;  
int m;  
int size;  
int HEAD[VN];  
int H[VN];  
int ORDER[VN];  
int D[VN];  int up; upper bound int low;  
    
Lower bound bool INQ[VN];  
    struct edge{int v,next;  
int W;  
    
}e[en];  
    void Addedge (int u,int v,int W) {e[size].v=v;  
    E[size].w=w;  
    E[size].next=head[u];  
head[u]=size++;  
    int SPFA (int src) {memset (inq, 0, sizeof (INQ));  
    for (int i=0; i<=n; ++i) D[i]=inf;  
    d[src]=0;  if (H[src]<low | | h[src]>up) return INF;  
    The starting point does not match the condition directly returns the INF queue<int>q;  
    Q.push (SRC);  while (!q.empty ()) {int U=q.front ();  
        Q.pop ();  if (H[u]<low | | h[u]>up) continue;  
        Exclude conformance and limitation of inq[u] = false; for (int e=head[u]; e!=-1; E=e[e].Next) if (h[e[e].v]>=low&&h[e[e].v]<=up) {//restricted int tmp=d[u]+e[e].w;  
                if (D[E[E].V] > tmp) {D[E[E].V] = tmp;  
                    if (!INQ[E[E].V]) {inq[e[e].v]=true;  
                Q.push (E[E].V);  
}}} return d[n];  
    int main () {int T, u, v;  
    int Len, Min, Max;  
    scanf ("%d", &t);  
        while (t--) {scanf ("%d%d", &n,&m);  
        size=0;  
        Memset (Head,-1, sizeof (head));  
            for (int i=1; i<=n; ++i) {scanf ("%d", &h[i]);  
            Order[i]=h[i];  
            if (h[i]<min) min=h[i];  
        if (H[i]>max) max=h[i];  
            for (int i=0; i<m; ++i) {scanf ("%d%d%d", &u,&v,&len);  
            Addedge (U,v,len);  
        Addedge (V,u,len);  
        Sort (order+1, order+1+n); int left=0, Right=maX-min+1, Mid;  
        int ans, dif=inf, minlen=inf;  
            while (left < right) {//two min. ' height difference ' mid = (left+right) >>1;  
            BOOL Flag=false;  
                for (int i=1; i<=n; ++i) {//Enumerate minimum elevation low=order[i]; Up=order[i]+mid;  
                Get an elevation upper bound int TMP=SPFA (1);  
                    if (tmp!=inf) {flag=true;  
                    ans=tmp;  
                Break  
                } if (flag) {right=mid;  
                    if (mid<dif) {dif=mid;  
                Minlen=ans;  
                else if (mid==dif && ans<minlen) {Minlen=ans;  
            }} else{left=mid+1;  
    } printf ("%d%d\n", DIF, Minlen);  
return 0; }

Author: csdn Blog shuangde800

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.