http://acm.uestc.edu.cn/#/problem/show/914
Fang Teacher Fen ITime limit:3000/1000ms (java/others) Memory limit:65535/65535kb (java/others)SubmitStatus
The teacher in order to open more lectures, so he was a fen! In the morning he was in one of the classrooms, and then each of them went to each of the different n classrooms (and of course every classroom had a single clone). In the evening, each of the different parts of the classroom, merged into a person (no need to go back at the same time). But the road between classrooms is very rugged and one-way. Of course, even if it is a teacher's fen, that is quite powerful, each one will take the path of the least time. The teacher wanted to know the length of time he spent walking round the longest walk. The topic guarantees a way to go.
Input
Enter three integers in the first lineN,m,x(1≤n≤ ,1≤m≤ ,1≤x≤n )。 Indicates that there are n classrooms,m Road,x is the place where the teacher is in the square.
NextmRows, three numbers per line,u,v,TShow from the classroomuto the Classroom v has a one-way edge, which takes time t(1≤t≤).
Output
Outputs an integer that takes time for the longest walk of the clone in a round trip.
Sample Input and output
| Sample Input |
Sample Output |
4 8 21 2 41 3 21 4 72 1 12 3 53 1 23 4 44 2 3 |
10 |
Dijkstra: two times. The first time along, the second time against. Here the storage of the graph is not suitable for the forward star, suitable for the matrix storage, the reverse side of the time is more convenient.
Code:
1#include <fstream>2#include <iostream>3#include <vector>4#include <queue>5#include <cstring>6#include <cstdio>7 8 using namespacestd;9 Ten Const intinf=0x7fffffff; One Const intn=1005; A Const intm=100005; - intn,m,x; - intA[n][n]; the intDis[n],dis2[n]; - BOOLB[n]; -typedef pair<int,int>PII; -priority_queue<pii,vector<pii>,greater<pii> >p; + - voidDijkstra (); + voiddijkstra2 (); A at intMain () - { - //freopen ("d:\\input.in", "R", stdin); - //freopen ("D:\\output.out", "w", stdout); - intT1,t2,t3; -scanf"%d%d%d",&n,&m,&x); in for(intI=0; i<m;i++){ -scanf"%d%d%d",&t1,&t2,&T3); toa[t1][t2]=T3; + } - Dijkstra (); the dijkstra2 (); *t1=0; $ for(intI=1; i<=n;i++) T1=max (t1,dis[i]+dis2[i]);Panax Notoginsengprintf"%d\n", T1); - return 0; the } + voidDijkstra () { A for(intI=1; i<=n;i++) dis[i]=INF; thedis[x]=0; +P.push (Make_pair (0, x)); - while(!P.empty ()) { $PII tp=p.top (); $ P.pop (); - inttx=Tp.second; - if(B[tx])Continue; theb[tx]=1; - for(intI=1; i<=n;i++){Wuyi if(a[tx][i]&& (!B[i])) { the if(dis[i]>dis[tx]+A[tx][i]) { -dis[i]=dis[tx]+A[tx][i]; Wu P.push (Make_pair (dis[i],i)); - } About } $ } - } - } - voidDijkstra2 () { A for(intI=1; i<=n;i++) dis2[i]=INF; +memset (b,0,sizeof(b)); thedis2[x]=0; -P.push (Make_pair (0, x)); $ while(!P.empty ()) { thePII tp=p.top (); the P.pop (); the inttx=Tp.second; the if(B[tx])Continue; -b[tx]=1; in for(intI=1; i<=n;i++){ the if(a[i][tx]&& (!B[i])) { the if(dis2[i]>dis2[tx]+A[i][tx]) { Aboutdis2[i]=dis2[tx]+A[I][TX]; the P.push (Make_pair (dis2[i],i)); the } the } + } - } the}
cdoj914-Fang Teacher Fen I "Dijkstra"