POj3268 Silver Cow Party
http://poj.org/problem?id=3268
题目大意:求到x距离与从x返回和的最大值
从x点到各个点最短路好求,直接用Dijkstar,但从各个点到x点却不好求,只要把路向翻转过来也变成求从x点到各个点,直接用Dijstar
dist[]记录x点到各个点的最短路径距离
#include<stdio.h> #include<stdlib.h> #include<math.h> #include<string.h> #define max(a, b)(a > b ? a : b) #define min(a, b)(a < b ? a : b) #define INF 0xffffff #define N 1010 int G1[N][N], G2[N][N]; bool vis[N]; int dist1[N], dist2[N], n; void Init() { int i, j; memset(vis, false, sizeof(vis)); for(i = 1 ; i <= n ; i++) { dist1[i] = INF; dist2[i] = INF; for(j = 1 ; j <= n ; j++) { G1[i][j] = INF; G2[i][j] = INF; } } } void Dij(int G[][N], int dist[], int x) { int index, Min, i, j; for(i = 1 ; i <= n ; i++) dist[i] = INF; dist[x] = 0; for(i = 1 ; i <= n ; i++) { index = 0; Min = INF; for(j = 1 ; j <= n ; j++) { if(!vis[j] && dist[j] < Min) { Min = dist[j]; index = j; } } vis[index] = true; for(j = 1 ; j <= n ; j++) { if(!vis[j] && dist[j] > dist[index] + G[index][j]) dist[j] = dist[index] + G[index][j]; } } } int main() { int m, x, i, a, b, t, Max; while(scanf("%d%d%d", &n, &m, &x) != EOF) { Init(); for(i = 1 ; i <= m ; i++) { scanf("%d%d%d", &a, &b, &t); G1[a][b] = t; G2[b][a] = t; } Dij(G1, dist1, x); memset(vis, false, sizeof(vis)); Dij(G2, dist2, x); Max = 0; for(i = 1 ; i <= n ; i++) Max = max(Max, dist1[i] + dist2[i]); printf("%d\n", Max); } return 0; }