COGS 315. [POJ3255] Floor tile roadblocks

Source: Internet
Author: User

315. [POJ3255] Floor tile roadblocks

★ ¡ï Input File: block.in output file: block.out Simple comparison
Time limit: 1 s memory limit: MB

Description

Bessie have moved to a small farm and sometimes enjoys returning to visit one of hers best friends. She does not want-to get-to-her-old home too quickly, because she likes the scenery along the. She has decided to take the second-shortest rather than the shortest path. She knows there must be some second-shortest path.

The countryside consists of R (1≤ R ≤100,000) bidirectional roads, each linking both of the N (1≤ N ≤5000) intersections, conveniently numbered 1.. n. Bessie starts at intersection 1, and she friend (the destination) is at intersection N.

The Second-shortest path may share roads with any of the shortest paths, and it could backtrack i.e., use the same road or I Ntersection more than once. The second-shortest path is the shortest path whose length is longer than the shortest path (s) (i.e., if EST paths exist, the second-shortest path is the one whose length are longer than those but no longer than any other path).

Bessie moved to a small farm to live, but she enjoyed the occasional visit to a good friend of hers. Bessie liked the scenery along the way, so she wanted to go as slowly as possible. So she decided to take a short circuit instead of the shortest way.

Suburbs contain R (1<=r<=100,000) Two-way road, they are connected to the N (1<=n<=5000) intersection (node), in order to facilitate us to mark them as 1. N. Bessie departs from node 1th, her good friend (destination) at node N.

The short-circuit and the shortest-circuit can be partially coincident, the secondary short-circuit may also go through the same path or the same node multiple times. The short-circuit is the shortest path in a short circuit. When there are multiple shortest circuits present, the secondary short circuit is the least of all other paths.

Input

Line 1:two space-separated integers: N and R

Lines 2.. R+1:each line contains three space-separated integers: a, B, and D that describe A ro Ad that connects intersections A and B and have length D (1≤ d ≤5000)

Input:

Line 1th: Two integers separated by a space N and R

2nd.. R+1 lines: Each line consists of three integers, a, B, and D, separated by spaces. The length of the path used to describe the connection between Node A and Node B is D (1<=d<=5000).

Output

Line 1:the length of the second shortest path between Node 1 and node N

Output:

First line: An integer representing the length of the secondary short circuit from node 1 to node n.

Sample Input

4 41 2 1002 4 2002 3 2503 4 100

Sample Output

450

Hint

3 (length 100+250+100=450), 4 (length 100+200=300) and 1, 2, routes:1, 2

Tips:

The route from node 1 to node n in the sample is 1->2->4 (len=100+200=300 (Shortest path)) and 1->2->3->4 (100+250+100=450 (short circuit)) respectively.

Translation BYKZFFFFFFFF

Idea: secondary short circuit board

#include <iostream>#include<queue>#include<cstdio>#include<cstring>#include<algorithm>#defineINF 0x3f3f3f3f#defineMAXN 400000using namespacestd;structnond{intg,f,to; BOOL operator< (ConstNond &r)Const {        if(r.f==f)returnr.g<G; Else returnr.f<F; }}tmp,opt;intN,m,p,s,t,cnt,tot,tot1,tot2;intDIS[MAXN],VIS[MAXN],DIS2[MAXN],VIS2[MAXN];intTO[MAXN],NET[MAXN],CAP[MAXN],HEAD[MAXN];intTO1[MAXN],NET1[MAXN],CAP1[MAXN],HEAD1[MAXN];voidAddintUintVintW) {to[++tot]=v;net[tot]=head[u];cap[tot]=w;head[u]=tot; to[++tot]=u;net[tot]=head[v];cap[tot]=w;head[v]=tot; to1[++tot1]=v;net1[tot1]=head1[u];cap1[tot1]=w;head1[u]=tot1; to1[++tot1]=u;net1[tot1]=head1[v];cap1[tot1]=w;head1[v]=tot1;}voidSPFA (ints) {Queue<int>que1;  for(intI=0; i<=n;i++) dis[i]=INF;    Que1.push (s); Vis[s]=1;d is[s]=0;  while(!Que1.empty ()) {        intnow=Que1.front ();        Que1.pop (); Vis[now]=0;  for(intI=head1[now];i;i=Net1[i])if(dis[to1[i]]>dis[now]+Cap1[i]) {Dis[to1[i]]=dis[now]+Cap1[i]; if(!Vis[to1[i]]) {Vis[to1[i]]=1;                Que1.push (To1[i]); }            }    }}intAstar (intKK) {Priority_queue<nond>que; if(s==t) kk++; if(Dis[s]==inf)return-1; TMP.G=0; Tmp.to=s; TMP.F=Dis[s];    Que.push (TMP);  while(!Que.empty ()) {tmp=Que.top ();        Que.pop (); if(tmp.to==t) cnt++; if(CNT==KK)returnTMP.G;  for(intI=head[tmp.to];i;i=Net[i]) {opt.to=To[i]; OPT.G=tmp.g+Cap[i]; OPT.F=opt.g+Dis[to[i]];        Que.push (opt); }    }    return-1;}intMain () {Freopen ("block.in","R", stdin); Freopen ("Block.out","W", stdout); scanf ("%d%d",&n,&m);  for(intI=1; i<=m;i++){        intA,b,c; scanf ("%d%d%d",&a,&b,&c);    Add (A,B,C); } s=1; t=N;    SPFA (t);  for(intI=1; i<=n;i++) {CNT=0; intans=Astar (i); if(ans!=Dis[s]) {printf ("%d", ans); return 0; }    }}

COGS 315. [POJ3255] Floor tile roadblocks

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.