Poj 3255 Roadblocks Short Circuit

Source: Internet
Author: User

This is a short circuit. There is a good solution, according to a conclusion, replacing one side in the most short-circuit can certainly get a short circuit.
You only need to enumerate all the edges. For example, assume that the start point is s, the target point is d, and the shortest path is dis (s, d ). For edges (u, v ),
Dis (s, u) + w (u, v) + dis (v, d) is greater than dis (s, d), then this path may be a short circuit. Find the smallest value greater than dis (s, d.
The method is to perform the single-source Multi-endpoint shortest path algorithm twice from s and from d. Then you can enumerate the edge.

This algorithm is understandable. Because the edges in the shortest path are replaced, the path length will only increase or remain unchanged.

The Code is as follows:
# Include <stdio. h>
# Include <string. h>
# Include <algorithm>
# Include <queue>
# Include <vector>
Using namespace std;

Const int MAX_N = 5000 + 10;
Struct Edge
{
Int nE;
Int nDis;
Edge (int e, int d): nE (e), nDis (d ){}
};
Vector <Edge> graph [MAX_N];
Bool bVisit [MAX_N];
Int nSDis [MAX_N];
Int nEDis [MAX_N];

Struct Node
{
Int nN;
Int nDis;

Bool operator <(const Node & node) const
{
Return nDis> node. nDis;
}
};

Int ShortestPath (int nS, int nE, int * nDis, int nN)
{
Priority_queue <Node> pq;
Memset (bVisit, false, sizeof (bVisit ));
For (int I = 1; I <= nN; I ++)
{
NDis [I] = 0x7fffffff;
}
NDis [nS] = 0;
Node head;
Head. nDis = 0, head. nN = nS;
Pq. push (head );

While (pq. empty () = false)
{
Node head = pq. top ();
Pq. pop ();
Int nU = head. nN;
If (bVisit [nU]) continue;
BVisit [nU] = true;

For (int I = 0; I <graph [nU]. size (); ++ I)
{
Int nV = graph [nU] [I]. nE;
Int nLen = head. nDis + graph [nU] [I]. nDis;
If (nLen <nDis [nV])
{
NDis [nV] = nLen;
Node node;
Node. nDis = nLen;
Node. nN = nV;
Pq. push (node );
}
}
}

Return nDis [nE];
}

Int Second (int nS, int nE, int nN)
{
Int nShortest = ShortestPath (nS, nE, nSDis, nN );
ShortestPath (nE, nS, nEDis, nN );

Int nAns = 0x7fffffff;

For (int I = 1; I <= nN; ++ I)
{
For (int j = 0; j <graph [I]. size (); ++ j)
{
Int nU = I;
Int nV = graph [I] [j]. nE;
Int nLen = nSDis [I] + graph [I] [j]. nDis + nEDis [nV];
If (nLen! = NShortest)
{
NAns = min (nAns, nLen );
}
}
}

Return nAns;
}

Int main ()
{
Int nN, nR;
Int nA, nB, nD;

While (scanf ("% d", & nN, & nR) = 2)
{
For (int I = 1; I <= nN; ++ I)
{
Graph [I]. clear ();
}

While (nR --)
{
Scanf ("% d", & nA, & nB, & nD );
Graph [nA]. push_back (Edge (nB, nD ));
Graph [nB]. push_back (Edge (nA, nD ));
} Www.2cto.com
Printf ("% d \ n", Second (1, nN, nN ));
}

Return 0;
}

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.