Graph theory (dual graph): COGS 470. [NOI2010] Altitude

Source: Internet
Author: User

470. [NOI2010] Altitude

★★★☆ input file: altitude.in output file: altitude.out simple comparison
Time limit: 2 s memory limit: MB

Elevation

"Problem description"

YT City is a well-planned city, and the city is divided into nxn areas by the east-west and north-South Main roads. For simplicity, the YT city can be viewed as a square, and each area can also be viewed as a square. Thus, the YT city includes (n+1) x (n+1) intersection and 2NX (n+1) Two-Way road (abbreviated to the road), each two-way road connecting the main road on the two adjacent intersections. A map of the city of YT (n = 2), the city is divided into 2x2 areas, including 3x3 intersections and 12 bidirectional roads.

Small Z as the mayor of the city, he was based on statistical information on the daily peak of the work of YT city in two directions per road traffic, that is, during the peak period along the way through the road of the number of people. Each intersection has a different altitude value, YT city residents think that climbing is a very tiring thing, every height of the climb up H, you need to consume H's strength. If it's downhill, you don't need to expend energy. So if the end of a road is at an altitude minus the starting altitude at H (Note that H is probably negative), then the physical strength of a person passing through this passage is max{0, H} (here Max{a, b} for a, b two values larger).

Small z also measured the intersection of the city's northwest corner at an altitude of 0, the intersection of the southeast corner at an altitude of 1 (as shown), but the altitude of the other intersections is unknown. Little Z wants to know in the best case (i.e. you can arbitrarily assume the altitude of the other intersection), the total force and the minimum value of all people climbing during the peak hours of each day.

"Input Format"

The first line contains an integer n, meaning as shown above.

Next 4n (n + 1) lines, each line contains a non-negative integer that represents the person traffic information for each direction of each road. Input order: The number of n (n + 1) indicates all traffic from west to east, then N (n + 1) number represents all traffic from north to south, the number of n (n + 1) represents all traffic from east to west, and finally the number of n (n + 1) represents all people flow from south to north direction. For each direction, the input order is given from the north to the south, if the north-south direction is in the same order from west to east (see sample Input).

"Output Format"

Contains only one number, which represents the overall force (i.e., the total strength and minimum) that is consumed by all the people climbing during the peak hours of each day during the most desirable times, rounded to an integer.

"Sample Input"

1

1

2

3

4

5

6

7

8

"Sample Output"

3

"Sample description"

See the sample data.

Ideally a bit of elevation as shown.

"Data Size"

For 20% data: n≤3;

For 50% data: n≤15;

For 80% data: n≤40;

For 100% of data: 1≤n≤500,0≤ traffic ≤1,000,000 and all traffic is an integer.

Prompted

Altitude is not necessarily an integer.

"Run Time"

2 seconds.

"Run Empty Limit"

512M.

  
The problem is converted into a dual graph, run Dijkstra +heap (I do not know why, SPFA+SLF optimization can not be too).

  

1#include <iostream>2#include <cstring>3#include <cstdio>4 using namespacestd;5 Const intmaxn=250010;6 Const intmaxm=1100010;7 intCNT,FIR[MAXN],NXT[MAXM],TO[MAXM],VAL[MAXM];8 voidAddedge (intAintBintd) {9nxt[++cnt]=fir[a];fir[a]=cnt;to[cnt]=b;val[cnt]=D;Ten } One intS,t,n; A intDIS[MAXN]; - structdata{ -     intDis,node; the }; - structnode{ - Data HEAP[MAXN]; -     intCNT; +     voidInsert (Data x) { -         intp=++CNT; +          while(p!=1){ A             if(heap[p>>1].dis<=x.dis) Break; atHeap[p]=heap[p>>1]; -p>>=1; -         } -heap[p]=x; -     } -     voidDelete () { in         intp=1, A, b; -Data x=heap[cnt--]; to          while(p*2<=CNT) { +a=p<<1; b=a|1; -             if(b>cnt| | Heap[b].dis>heap[a].dis) b=A; the             if(Heap[b].dis>=x.dis) Break; *heap[p]=Heap[b]; $p=b;Panax Notoginseng         } -heap[p]=x; the     } + }q; A   the intSolve () { +memset (DIS,127,sizeof(DIS)); -dis[s]=0; Q.insert (Data) {dis[s],s}); $      while(true){ $         intnode=q.heap[1].node;q.delete (); -         if(node==t) Break; -          for(intI=fir[node];i;i=Nxt[i]) the             if(dis[to[i]]>dis[node]+Val[i]) { -dis[to[i]]=dis[node]+Val[i];Wuyi Q.insert (Data) {dis[to[i]],to[i]}); the             } -     } Wu     returnDis[t]; - } About   $ intMain () { -Freopen ("altitude.in","R", stdin); -Freopen ("Altitude.out","W", stdout); -scanf"%d",&n); As=0; t=n*n+1; +     inta,b,d; the      for(intI=1; i<=n+1; i++) -          for(intj=1; j<=n;j++){ $scanf"%d",&d); thea=i==n+1? S: (I-1) *n+J; theb=i==1? T: (I-2) *n+J; the Addedge (a,b,d); the         } -          in      for(intI=1; i<=n;i++) the          for(intj=1; j<=n+1; j + +){ thescanf"%d",&d); Abouta=j==1? S: (I-1) *n+j-1; theb=j==n+1? T: (I-1) *n+J; the Addedge (a,b,d); the         } +      -      for(intI=1; i<=n+1; i++) the          for(intj=1; j<=n;j++){Bayiscanf"%d",&d); thea=i==n+1? S: (I-1) *n+J; theb=i==1? T: (I-2) *n+J; - Addedge (b,a,d); -         } the          the      for(intI=1; i<=n;i++) the          for(intj=1; j<=n+1; j + +){ thescanf"%d",&d); -a=j==1? S: (I-1) *n+j-1; theb=j==n+1? T: (I-1) *n+J; the Addedge (b,a,d); the         }94              theprintf"%d\n", Solve ()); the     return 0; the}

Graph theory (dual graph): COGS 470. [NOI2010] Altitude

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.