vijos:p1155 Assembly Position (secondary short circuit)

Source: Internet
Author: User

Describe

Every time there is a big activity, we all have to "get together", whether it is to good radius, or Typhoon shelter, or Tom Xong, we all want to play happily. Remember the heart language and flowers in the dance machine passion and release, remember the hasty shooting skill is so superb, still remember the dog's marksmanship is always ' S ' ... Also can not forget, Fat's song is always let us scream!!
Today is Wildcat's birthday, so think of these also normal, just because it is school day, can't play together. But recall the sweetness of that time is always a kind of happiness ...

But every time a collection comes up with a problem! Wildcat is recognized as "Road Blind", Wildcat own heart is also very clear, every time to go out early, but still often late, this makes everyone very helpless. Later, the wildcat at each door, will consult the flowers of the path, according to the known path, finally can arrive on time.

Now ask the question: give the coordinates of the n points, where the first one is the Wildcat's starting position, the last one is the assembly position for everyone, and the location points are connected. Wildcat from the starting point to reach the rendezvous point, always pick a recent road, if the Wildcat did not find the nearest road, he will go the second near the road. Please help the Wildcat find the second shortest path length.

Format input Format

The first line is two integers n (1<=n<=200) and M, indicating that there are altogether n points and M-paths, the following n lines are two numbers per row xi,yi, ( -500<=xi,yi<=500), representing the coordinates of the I-point, and then the M-line of the lower row of two integers pj,qj, (1<=pj,qj<=n), which means two points are connected.

Output format

Only one row contains a number, the distance for the second shortest route (two decimal places are reserved), and if there are multiple first short paths, the answer is the length of the first shortest path, and if there is no second shortest path, output-1.

Input:

3 3
0 0
1 1
0 2
1 2
1 3
2 3

Output:

2.83

Train of thought: use Dijkstra to find the shortest path, and then remove the edge enumeration in short circuit, and use SPFA to find the quickest way. (Delete Edge enumeration)

#include <cstdio>#include<cstring>#include<queue>#include<vector>#include<cmath>#include<algorithm>using namespacestd;Const intmaxn=205;Const Doubleinf=100000000.0; typedef pair<int,Double>P;structnode{intx, y;} POS[MAXN];structedge{intto ; DoubleW; Edge () {} Edge (intTo,DoubleW) { This->to=to ;  This->w=W; }};intPre[maxn];vector<Edge>MP[MAXN];DoubleDistintXintYintX1,inty1) {    returnsqrt ((x-x1) * (x-x1) + (y-y1) * (yy1));}intn,m;DoubleD[MAXN];voidDijkstraints) {     for(intI=1; i<=n;i++) {D[i]=INF; } D[s]=0; Priority_queue<P,vector<P>,greater<P> >que; Que.push (P (0, s));  while(!Que.empty ()) {P now=que.top (); Que.pop (); intu=Now.second; if(D[u]<now.first)Continue;  for(intI=0; I<mp[u].size (); i++) {Edge e=Mp[u][i]; if(d[e.to]>d[u]+e.w) {d[e.to]=d[u]+E.W; Pre[e.to]=u;            Que.push (P (d[e.to],e.to)); }        }        }}DoubleD1[MAXN];intVIS[MAXN];voidSPFA (intSintUintv) {     for(intI=1; i<=n;i++) {D1[i]=INF; Vis[i]=0; } Queue<int>que;    Que.push (s); D1[s]=0; Vis[s]=1;  while(!Que.empty ()) {        intnow=Que.front (); Que.pop (); Vis[now]=0;  for(intI=0; I<mp[now].size (); i++) {Edge e=Mp[now][i]; if((now==u&&e.to==v) | | (now==v&&e.to==u))Continue; if(d1[e.to]>d1[now]+e.w) {d1[e.to]=d1[now]+E.W; if(!Vis[e.to]) {Vis[e.to]=1;                Que.push (e.to); }            }        }    }}intMain () {scanf ("%d%d",&n,&m);  for(intI=1; i<=n;i++) {scanf ("%d%d",&pos[i].x,&pos[i].y); }     for(intI=1; i<=m;i++)    {        intu,v; scanf ("%d%d",&u,&v); Doublew=Dist (POS[U].X,POS[U].Y,POS[V].X,POS[V].Y);        Mp[u].push_back (Edge (v,w));            Mp[v].push_back (Edge (u,w)); } Dijkstra (1); Doublemn=INF; intPr=N;  while(pre[pr]!=0) {SPFA (1, PR,PRE[PR]); MN=min (mn,d1[n]); PR=PRE[PR]; }        if(Fabs (Mn-inf) <0.000001) printf ("-1\n"); Elseprintf ("%.2f\n", MN); return 0;} 

vijos:p1155 Assembly Position (secondary short circuit)

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.