题干:

One cow from each of N farms (1 ≤ N ≤ 1000) conveniently numbered 1..N is going to attend the big cow party to be held at farm #X (1 ≤ X ≤ N). 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 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 i with three space-separated integers:AiBi, and Ti. The described road runs from farm Ai to 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 2
1 2 4
1 3 2
1 4 7
2 1 1
2 3 5
3 1 2
3 4 4
4 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.

题目大意:

从出发点到达目的地X,再从X返回到出发点的最短路径中的最大值(因为出发点没有固定,也就是可以:1->X->1, 2->X->2等)。

解题报告:

   跑两遍最短路,求一个x到各个点的,再求转置矩阵然后跑一边x到各个点的,也就是原矩阵的各个点到x点的。加起来维护最大值即可。

一个题解:所以我们要枚举所有的可能性,找出其中的最大值。巧妙地运用dijkstra算法,双向求出两次X->m的最短路径长然后相加即得到了m->X->m的最短路径。

AC代码:

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#include<iostream>
using namespace std;
const int MAX = 100000 + 5;
const int INF = 0x3f3f3f3f ;
struct Node {
	int to;
	int w;
	int ne;
} e[MAX];
int a[100000 + 5],b[100000 + 5],w[100000 + 5];
struct point {
	int pos;
	int c;
	point(){}//没有此构造函数不能写  node t  这样
	point(int pos,int c):pos(pos),c(c){}//可以写node(pos,cost)这样

	bool operator <(const point & b) const {
		return c>b.c;
	}
};

int head[MAX];
int vis[MAX];
int cnt = 0;
int maze[1005][1005];
int n,m;
int dis1[MAX],dis2[MAX];//表示从出发点开始到该点的最短距离。 
void add(int u,int v,int w) {
	e[cnt].to = v;
	e[cnt].w = w;
	e[cnt].ne = head[u];
	head[u] = cnt;
	cnt++;
}
void Dijkstra1(int u) {
	priority_queue<point> pq; 
	dis1[u] = 0;
	point cur = point(u,0);
	pq.push(cur);
	while(!pq.empty()) {
		point now = pq.top();pq.pop();
		vis[now.pos] = 1;
		for(int i = head[now.pos]; i!=-1; i=e[i].ne) {
			if(  dis1[e[i].to] > dis1[now.pos] + e[i].w ) {
				dis1[e[i].to] = dis1[now.pos] + e[i].w;
				pq.push(point(e[i].to,dis1[e[i].to] ) );	
			}
		} 
	}
} 
void Dijkstra2(int u) {
	priority_queue<point> pq; 
	dis2[u] = 0;
	point cur = point(u,0);
	pq.push(cur);
	while(!pq.empty()) {
		point now = pq.top();pq.pop();
		vis[now.pos] = 1;	
		for(int i = head[now.pos]; i!=-1; i=e[i].ne) {
			if(  dis2[e[i].to] > dis2[now.pos] + e[i].w ) {
				dis2[e[i].to] = dis2[now.pos] + e[i].w;
				pq.push(point(e[i].to,dis2[e[i].to] ) );	
			}
		} 
	}
} 
void init1() {
	cnt = 0;
	memset(head,-1,sizeof(head) );
	memset(dis1,INF,sizeof(dis1) ) ;
	memset(maze,INF,sizeof(maze) );
	memset(vis,0,sizeof(vis) ) ;
}
void init2() {
	cnt = 0;
	memset(head,-1,sizeof(head) );
	memset(dis2,INF,sizeof(dis2) ) ;
	memset(maze,INF,sizeof(maze) );
	memset(vis,0,sizeof(vis) ) ;
}
int main()
{
	
	int x;
	while(~scanf("%d %d %d",&n,&m,&x) ) {
		init1();
		
		for(int i = 1; i<=m; i++) {
			scanf("%d%d%d",&a[i],&b[i],&w[i]);
			if(w[i]<maze[a[i] ][b[i] ]) {
				maze[a[i]][b[i]] = w[i];
				add(a[i],b[i],w[i]);
			}
		}
		Dijkstra1(x);
		init2();
		for(int i = 1; i<=m; i++) {
			if(w[i]<maze[b[i] ][a[i] ]) {
				maze[b[i]][a[i]] = w[i];
				add(b[i],a[i],w[i]);
			}
		}
		Dijkstra2(x);
		int maxx = 0;
		for(int i = 1; i<=n; i++) {
			maxx = max(maxx,dis1[i] + dis2[i]);
		}
//		printf("::::::::\n");
		printf("%d\n",maxx);
	}
	
	return 0 ;
 } 

总结:

   其实我这里写麻烦了 ,如果用邻接矩阵保存图的话,直接求转置矩阵然后再最短路一发就行了。如果用邻接表这样,就得先保存输入数据然后跑一边后再初始化然后再读入一遍再跑一遍。