POJ3268 (Dijkstra)

Source: Internet
Author: User
Silver Cow Party
Time Limit:2000 MS   Memory Limit:65536 K
Total Submissions:13103   Accepted:5883
Question link: http://poj.org/problem? Id = 3268

Description

One cow from eachNFarms (1 ≤N≤ 1000) conveniently numbered 1 ..NIs going to attend the big cow party to be held at farm #X(1 ≤XN). A totalM(1 ≤MLess than or equal to 100,000) unidirectional (one-way roads connects pairs of farms; roadIRequiresTi(1 ≤Ti≤ 100) units of time to traverse.

Each cow must walk to the party and, when the party is over, return to her farm. each cow is lazy and thus picks an optimal route with the shortest time. A cow's return route might be different from her original route to the party since roads are one-way.

Of all the cows, what is the longest amount of time a cow must spend walking to the party and back?

Input

Line 1: Three space-separated integers, respectively: NM, And X 
Lines 2 .. M+ 1: Line I+ 1 describes road IWith three space-separated integers: AiBi, And Ti. The described road runs from farm AiTo farm Bi, Requiring TiTime units to traverse.

Output

Line 1: One integer: the maximum of time any one cow must walk.

Sample Input

4 8 21 2 41 3 21 4 72 1 12 3 53 1 23 4 44 2 3

Sample Output

10

Hint

Cow 4 proceeds directly to the party (3 units) and returns via farms 1 and 3 (7 units), for a total of 10 time units.


Solution: The question is about n farms. Each farm has a ox, and now there are m trails (single source Road). We need to have a party on farm x, and other cows will attend, in addition, when I return to my farm, it is stipulated that the ox is very lazy, so it must be the shortest path. The maximum time required by the Ox. First, it is well constructed for dis functions from x to other points, but it is a little difficult from other points to x, because this is not a single source, it is a multi-source. This is where we can reverse the edge and find the shortest path from x to other points. This is a single source and a template can be set. You can find a good way from the internet. You can reverse all the edges to replace the rows and columns of g.

Complete code:
# Include <functional> # include <algorithm> # include <iostream> # include <fstream> # include <sstream> # include <iomanip> # include <numeric> # include <cstring> # include <climits> # include <cassert> # include <complex> # include <cstdio> # include <string> # include <vector> # include <bitset> # include <queue> # include <stack> # include <cmath> # include <ctime> # include <list> # include <set> # include <map> using namespace std; # p Ragma comment (linker, "/STACK: 102400000,102400000") typedef long LL; typedef double DB; typedef unsigned uint; typedef unsigned long uLL;/** Constant List .. ** // {const int MOD = int (1e9) + 7; const int INF = 0x3f3f3f3f; const ll inff = 0x3f3f3f3f3f3fll; const db eps = 1e-9; const db oo = 1e20; const db pi = acos (-1.0); // M_PI; const int maxn = 1111; int n, m, x; int g [maxn] [maxn]; int dis [maxn ]; Int undis [maxn]; int vis [maxn]; void init () {for (int I = 1; I <= n; I ++) {for (int j = 1; j <= n; j ++) {if (I = j) g [I] [j] = 0; else g [I] [j] = INF ;}} int dijkstra () {for (int I = 1; I <= n; I ++) {dis [I] = g [x] [I]; undis [I] = g [I] [x];} memset (vis, 0, sizeof (vis )); for (int I = 1; I <= n; I ++) {int mark =-1; int mindis = INF; for (int j = 1; j <= n; j ++) {if (! Vis [j] & dis [j] <mindis) {mindis = dis [j]; mark = j ;}} vis [mark] = 1; for (int j = 1; j <= n; j ++) {if (! Vis [j]) {dis [j] = min (dis [j], dis [mark] + g [mark] [j]) ;}} memset (vis, 0, sizeof (vis); for (int I = 1; I <= n; I ++) {int mark =-1; int mindis = INF; for (int j = 1; j <= n; j ++) {if (! Vis [j] & undis [j] <mindis) {mindis = undis [j]; mark = j ;}} vis [mark] = 1; for (int j = 1; j <= n; j ++) {if (! Vis [j]) {undis [j] = min (undis [j], undis [mark] + g [j] [mark]) ;}} int maxx =-INF; for (int I = 1; I <= n; I ++) if (dis [I] + undis [I]> maxx) maxx = dis [I] + undis [I]; return maxx;} int main () {# ifdef DoubleQ freopen ("in.txt", "r", stdin ); # endif while (~ Scanf ("% d", & n, & m, & x) {init (); int x, y, k; for (int I = 0; I <m; I ++) {scanf ("% d", & x, & y, & k ); g [x] [y] = k;} printf ("% d \ n", dijkstra ());}}


POJ3268 (Dijkstra)

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.