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 = ; i <= n ; i++)
{
dist1[i] = INF;
dist2[i] = INF;
for(j = ; 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 = ; i <= n ; i++)
dist[i] = INF;
dist[x] = ;
for(i = ; i <= n ; i++)
{
index = ;
Min = INF;
for(j = ; j <= n ; j++)
{
if(!vis[j] && dist[j] < Min)
{
Min = dist[j];
index = j;
}
}
vis[index] = true;
for(j = ; 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 = ; 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 = ;
for(i = ; i <= n ; i++)
Max = max(Max, dist1[i] + dist2[i]);
printf("%d\n", Max);
}
return ;
}
上一篇:PHP in_array不兼容问题


下一篇:quartz部署出现找不到表的情况,错误提示: Table 'heart_beat.QRTZ_LOCKS' doesn't exist