POJ 3268 Silver Cow Party Shortest path-dijkstra algorithm optimization.

Source: Internet
Author: User

POJ 3268 Silver Cow Party

Description

One cow from each of N Farms (1≤ n ≤1000) conveniently numbered 1.. N is going to attend the big Cow party to being held at Farm #x (1≤ XN). A total of m (1≤ m ≤100,000) unidirectional (one-way roads connects pairs of farms; road I requires ti (1≤ ti ≤100) units of time to traverse.

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

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

Input

Line 1:three space-separated integers, respectively: N, M, and X
Lines 2.. M+1:line I+1 describes road I with three space-separated integers: Ai, Bi, a nd Ti. The described road runs from farm Ai to farm Bi, requiring Ti time units to traverse.

Output

Line 1:one integer:the maximum of the 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 ten time units.< /c0>Title Description:A herd of cattle from 1~n Farm to the X farm to participate in the party, farm and Farm Road is one-way , between N Farms have M road, give a, B, t means from farm A to farm B need t time. Each cow chooses the shortest route and asks how much time it takes for the cows to go back and forth on the longest journey.
Idea Description:Test instructions is not difficult to understand that the shortest distance from farm X to the rest of the farm is the shortest distance from the rest of the farm to the X farm, and the shortest distance from farm X to the rest of the farm is once Dijkstra, butthe shortest distance from the remaining farms to X farms, Requires n times DIjkstraCan be obtained, the amount of observation data can be learned that ordinarydIjkstra The algorithm is difficult to meet the requirements becausenormal D-ijkstra algorithm The time complexity is O (n^2), plus n+1 calls, according to the amount of data must time out, so the code belownormal D-ijkstra algorithm is optimized to make the time complexity O (n*n*log N) to meet the requirements of the topic. The main implementation is to sort the edges by the priority queue, so that the time spent searching for the smallest side is first, as detailed in the following code: Code implementation:
1#include <Set>2#include <map>3#include <stack>4#include <stdio.h>5#include <vector>6#include <utility>7#include <string.h>8#include <queue>9#include <iterator>Ten#include <stdlib.h> One#include <math.h> A#include <iostream> -#include <algorithm> - using namespacestd; the intINF =0x3f3f3f3f; - intn,m,x; - structEdge - { +     intu; -     intv; +     intCost ; A} m[100050];//the information that stores the original edge; at structN - { -     intE//weights for each edge; -     intto;//end point; - }; -Vector<n> v[ -];//store information for each edge, subscript as the starting point; in intdis[ -];//the shortest distance from the starting point to each end point; - voidDijkstraints) to { +priority_queue<pair<int,int>,vector<pair<int,int> >,greater<pair<int,int> > >q;//The priority queue is used to sort the inner edges from small to large; -      for(inti =1; i<=1199; i++) v[i].clear ();//vector empty; theFill (dis,dis+ -, INF);//initialized to the maximum value; * N n2; $      for(inti =1; i<=m;i++)//adding the initial edge to the vector;Panax Notoginseng     { -N2.E =M[i].cost; theN2.to =m[i].v; + V[m[i].u].push_back (n2); A     } theDis[s] =0; +     //optimization and implementation: -Q.push (pair<int,int> (0, s)); $      while(!q.empty ()) $     { -pair<int,int> P =q.top (); - Q.pop (); the         intV1 =P.second; -         if(Dis[v1]<p.first)Continue;Wuyi          for(inti =0; I<v[v1].size (); i++) the         { -N N1 =V[v1][i]; Wu             if(dis[n1.to]>dis[v1]+n1.e) -             { Aboutdis[n1.to]=dis[v1]+n1.e; $Q.push (pair<int,int>(dis[n1.to],n1.to)); -             } -         } -     } A } + intMain () the { -     intsum[ -]; $      while(~SCANF (" %d%d%d",&n,&m,&x)) the     { thememset (SUM,0,sizeof(sum)); the          for(inti =1; i<=m;i++) scanf (" %d%d%d",&m[i].u,&m[i].v,&m[i].cost); the Dijkstra (x); -          for(inti =1; i<=n;i++) sum[i]+=Dis[i]; in          for(inti =1; i<=n;i++) the         { the Dijkstra (i); AboutSum[i] + =Dis[x]; the         } the         intMax1 =0; the          for(inti =1; i<=n;i++)if(sum[i]>max1) Max1 =Sum[i]; +printf"%d\n", max1); -     } the     return 0;Bayi}

This article is a personal essay, if there are inappropriate, hope that the big boys more advice.
If you can provide small help to Bo friends, it is a great honor.

POJ 3268 Silver Cow Party Shortest path-dijkstra algorithm optimization.

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.