码迷,mamicode.com
首页 > 其他好文 > 详细

POj3268 Silver Cow Party

时间:2015-07-20 18:49:45      阅读:103      评论:0      收藏:0      [点我收藏+]

标签:

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;
}

 

POj3268 Silver Cow Party

标签:

原文地址:http://www.cnblogs.com/qq2424260747/p/4662026.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!